Skip to content

Commit e4d83e1

Browse files
feat: deployment actions for running the service (#33)
* feat: add Docker configuration and deployment workflow * fix: correct casing in Dockerfile FROM instruction * fix: remove temporary branch from deployment workflow * fix: update ssh-action version and improve Dockerfile pip install command
1 parent 0cfedac commit e4d83e1

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

.dockerignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.DS_Store
8+
**/__pycache__
9+
**/.venv
10+
**/.classpath
11+
**/.dockerignore
12+
**/.env
13+
**/.git
14+
**/.gitignore
15+
**/.project
16+
**/.settings
17+
**/.toolstarget
18+
**/.vs
19+
**/.vscode
20+
**/*.*proj.user
21+
**/*.dbmdl
22+
**/*.jfm
23+
**/bin
24+
**/charts
25+
**/docker-compose*
26+
**/compose.y*ml
27+
**/Dockerfile*
28+
**/node_modules
29+
**/npm-debug.log
30+
**/obj
31+
**/secrets.dev.yaml
32+
**/values.dev.yaml
33+
LICENSE
34+
README.md

.github/workflows/deploy.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Deploy to EC2
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
9+
jobs:
10+
build-and-push:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 10
13+
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'staging' }}
14+
15+
steps:
16+
- name: Checkout Repository
17+
uses: actions/checkout@v4
18+
19+
- name: Login to Docker Hub
20+
uses: docker/login-action@v3
21+
with:
22+
username: ${{ secrets.DOCKERHUB_USERNAME }}
23+
password: ${{ secrets.DOCKERHUB_TOKEN }}
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v3
27+
28+
- name: Build and push
29+
uses: docker/build-push-action@v5
30+
with:
31+
context: .
32+
file: production.Dockerfile
33+
platforms: linux/arm64
34+
push: true
35+
tags: |
36+
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:${{ github.sha }}
37+
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest
38+
cache-from: type=gha
39+
cache-to: type=gha,mode=max
40+
41+
- name: Deploy to EC2
42+
uses: appleboy/ssh-action@v1
43+
with:
44+
host: ${{ secrets.AWS_EC2_HOST }}
45+
username: ${{ secrets.AWS_EC2_USERNAME }}
46+
key: ${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }}
47+
script: |
48+
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest
49+
docker stop ${{ github.event.repository.name }}-${{ vars.ENV }} || true
50+
docker rm ${{ github.event.repository.name }}-${{ vars.ENV }} || true
51+
docker run -d -p ${{ vars.PORT }}:8000 \
52+
--name ${{ github.event.repository.name }}-${{ vars.ENV }} \
53+
--network=${{ vars.DOCKER_NETWORK }} \
54+
-e DB_NAME="${{ secrets.DB_NAME }}" \
55+
-e MONGODB_URI="${{ secrets.MONGODB_URI }}" \
56+
-e ALLOWED_HOSTS="${{ vars.ALLOWED_HOSTS }}" \
57+
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}

README.Docker.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Docker Deployment Guide
2+
3+
### Building and running your application
4+
5+
When you're ready, start your application by running:
6+
`docker compose up --build`.
7+
8+
Your application will be available at http://localhost:8000.
9+
10+
### Deploying your application to the cloud
11+
12+
First, build your image, e.g.: `docker build -t myapp .`.
13+
If your cloud uses a different CPU architecture than your development
14+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
15+
you'll want to build the image for that platform, e.g.:
16+
`docker build --platform=linux/amd64 -t myapp .`.
17+
18+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
19+
20+
Consult Docker’s [getting started guide](https://docs.docker.com/go/get-started-sharing/) for more detail on building and pushing.
21+
22+
### References
23+
* [Docker's Python guide](https://docs.docker.com/language/python/)

production.Dockerfile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# syntax=docker/dockerfile:1
2+
3+
# Comments are provided throughout this file to help you get started.
4+
# If you need more help, visit the Dockerfile reference guide at
5+
# https://docs.docker.com/go/dockerfile-reference/
6+
7+
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
8+
9+
ARG PYTHON_VERSION=3.12.0
10+
FROM python:${PYTHON_VERSION}-slim AS base
11+
12+
# Prevents Python from writing pyc files.
13+
ENV PYTHONDONTWRITEBYTECODE=1
14+
15+
# Keeps Python from buffering stdout and stderr to avoid situations where
16+
# the application crashes without emitting any logs due to buffering.
17+
ENV PYTHONUNBUFFERED=1
18+
19+
# Set Django settings module
20+
ENV DJANGO_SETTINGS_MODULE=todo_project.settings.production
21+
ENV ENV=PRODUCTION
22+
23+
WORKDIR /app
24+
25+
# Install CA certificates needed for TLS connections to MongoDB Atlas
26+
RUN apt-get update && apt-get install -y --no-install-recommends \
27+
ca-certificates \
28+
&& rm -rf /var/lib/apt/lists/*
29+
30+
# Create a non-privileged user that the app will run under.
31+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
32+
ARG UID=10001
33+
RUN adduser \
34+
--disabled-password \
35+
--gecos "" \
36+
--home "/nonexistent" \
37+
--shell "/sbin/nologin" \
38+
--no-create-home \
39+
--uid "${UID}" \
40+
appuser
41+
42+
# Download dependencies as a separate step to take advantage of Docker's caching.
43+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
44+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
45+
# into this layer.
46+
RUN python -m pip install --no-cache-dir -r requirements.txt
47+
48+
# Switch to the non-privileged user to run the application.
49+
USER appuser
50+
51+
# Copy the source code into the container.
52+
COPY . .
53+
54+
# Expose the port that the application listens on.
55+
EXPOSE 8000
56+
57+
# Run the application.
58+
CMD ["gunicorn", "todo_project.wsgi", "--bind", "0.0.0.0:8000"]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Django==5.1.5
77
djangorestframework==3.15.2
88
dnspython==2.7.0
99
filelock==3.16.1
10+
gunicorn==23.0.0
1011
identify==2.6.1
1112
nodeenv==1.9.1
1213
platformdirs==4.3.6

0 commit comments

Comments
 (0)