backend(django-rest)frontend(react)database(postgres)proxy(nginx)
Since the project uses a proxy it's necessary to set the .env with
ENVIRONMENT="local"
otherwise the project will fail due to a missing SSL certificate.
Create .env file:
POSTGRES_DB="<db>"
POSTGRES_USER="<user>"
POSTGRES_PASSWORD="<password>"
POSTGRES_PORT="<default:5432>"
BACKEND_SECRET_KEY="<django-insecure-...>"
BACKEND_PORT="<default:8000>"
PROXY_PORT="<default:80>"
Build containers, run them:
docker compose build
docker compose up -d
The proxy configuration is done by the /proxy docker container. There are three nginx.conf[environment].template that have the specific configuration for all the environments ('local', 'development', 'production').
This can be configured by the ENVIRONMENT env variable.
By default the dev and prod example redirect to use SSL for the server meaning a certificate has to be configured. This can be done using the certbot image that is included in the docker compose.
Prerequisites
- The DNS has to have a A record for the domain
domain.netpointing to the server - Access to the server
Steps
Important
This example uses domain.net as the default domain to create the certs for, REPLACE WITH THE CORRECT DOMAIN BEFORE RUNNING THE STEPS BELOW!
- Replace the
nginx.conf[environment].templaterelevant to the environment you are deploying to with the following content. At this moment we want to serve the acme challenge url so the certbot can resolve the certificate.
worker_processes 8;
user root;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {}
http {
include mime.types;
server {
listen 80;
server_name $SERVER_HOST;
location /static/ {
alias /static/;
autoindex on;
access_log off;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
}
- Run the following commands from the root of the repository
docker compose build
docker compose up -d
docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ --dry-run -d domain.net
- if the dry-run completes successfully then execute the following command, this will create the certificate files in the machine
docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ -d domain.net
- Restore
nginx.conf[environment].templateto default values and run
docker compose build
docker compose up -d
Certificate renewal
To renew the certifcate run
docker compose run --rm certbot renew
docker compose restart