Skip to content

Commit

Permalink
feat(backend): Add all-in-one Docker container for production deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
barnslig committed Dec 10, 2021
1 parent 698a82e commit c2c453d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
.env
app/node_modules
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM node:14-alpine AS app

COPY app /app
WORKDIR /app
RUN yarn
ENV API_ROOT=/api
RUN yarn build

FROM python:3.9-slim

RUN pip install pipenv gunicorn
WORKDIR /usr/src/app
COPY backend .
RUN mkdir /data
RUN pipenv install --deploy --system

RUN apt update && apt install -y \
nginx-light \
supervisor \
&& rm -rf /var/lib/apt/lists/*

COPY --from=app /app/dist /var/www/html
COPY docker /

WORKDIR /usr/src/app/dpt_app
RUN python manage.py collectstatic --no-input

EXPOSE 80

CMD ["/usr/bin/supervisord", "--nodaemon", "-c", "/etc/supervisor/supervisord.conf"]
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,31 @@ mdbook serve
## App Frontend

See [app/README.md](app/README.md).

## App Backend

See [backend/README.md](backend/README.md).

## Production Setup

The production setup is done using a single Docker container.

To build the Docker container, execute:

```
$ docker build -t abc-dpt .
```

To run the Docker container, execute:

```
$ docker run --rm -p 8000:80 -v data:/data abc-dpt
```

You can now access the app at [localhost:8000](http://localhost:8000).

To persist the database and media files, make sure to provide a volume for `/data`.

The web server is exposed at port 80.

The behavior of the container can be changed by overriding environment variables. Check out the example `.env` file for a list of variables: [.env.example](backend/dpt_app/.env.example).
25 changes: 25 additions & 0 deletions docker/etc/nginx/sites-enabled/default
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;

location / {
try_files $uri $uri/ /index.html;
}

location ~ ^/(api|admin) {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /static {
alias /usr/src/app/dpt_app/staticfiles;
}

location /media {
alias /data/media;
}
}
6 changes: 6 additions & 0 deletions docker/etc/supervisor/conf.d/dpt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[program:dpt-backend]
command = /usr/src/app/dpt_app/run.sh
directory = /usr/src/app/dpt_app

[program:nginx]
command = /usr/sbin/nginx -g 'daemon off;'
4 changes: 4 additions & 0 deletions docker/usr/src/app/dpt_app/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

./manage.py migrate
gunicorn -b [::]:8000 dpt_app.wsgi:application

0 comments on commit c2c453d

Please sign in to comment.