Obtaining and deploying an SSL certificate for Nginx running inside a Docker container involves several steps. Here's a guide to help you set up Certbot for an Nginx container using Docker:
It's recommended to use Docker Compose to manage multi-container applications. Here's an example docker-compose.yml that starts both Nginx and Certbot:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbotIn your Nginx configuration (nginx.conf), you need to set up an HTTP server block to respond to the Certbot challenges. Create an nginx.conf file with the following content:
http {
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location ~ /.well-known/acme-challenge/ {
allow all;
root /var/www/certbot;
}
}
# Additional configurations and server blocks
}Replace yourdomain.com and www.yourdomain.com with your domain names.
With the services defined and Nginx configured to respond to the Certbot challenges, start the Nginx container:
docker-compose up -d nginxThen, request the SSL certificates:
docker-compose run --rm certbot certonly --webroot -w /var/www/certbot -d yourdomain.com -d www.yourdomain.comIf successful, the certificates will be stored in the ./data/certbot/conf directory on your host.
Now, update your Nginx configuration (nginx.conf) to use the SSL certificates:
http {
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Additional SSL configurations and server blocks
}
# ...
}With the updated configurations, restart the Nginx container:
docker-compose down
docker-compose up -d nginx
docker-compose up -d certbotLet's Encrypt certificates are valid for 90 days, so you need to set up an automatic renewal mechanism. This can be achieved by running the Certbot renewal command regularly using a cron job or other scheduling mechanisms.