Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions docker/auto_ssl_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash

# ------------------------------------------------------------------------------
# Developer Details
# ------------------------------------------------------------------------------
# Name: Uday Kumar
# Contact: uday.kumar@bridgeconn.com
# Date: 21 Nov 2023
# Description: This script sets up and manages Let's Encrypt SSL, including automatic certificate renewal.
# ------------------------------------------------------------------------------


# Load environment variables
if [ -f prod.env ]; then
source prod.env
else
echo "prod.env file not found"
exit 1
fi

# Configuration variables
NGINX_CONTAINER_NAME="docker-web-server-with-cert-1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to not hardcode this and get this from the env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This we can take from env for sure

SSL_CERTS_DIR="/certbot/conf"
WEBROOT_DIR="/certbot/www"

# Generate SSL certificates
generate_certificates() {
echo "Generating SSL certificates for $DOMAIN..."

docker run --rm -it \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does this run docker within docker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also checked that the current approach isn't ideal. If it's not suitable, I'm working on creating a single Docker solution, though it's taking some time.

After your comment, I tried but I am getting the same issue as certificate not creating.

-v "$(pwd)${SSL_CERTS_DIR}:/etc/letsencrypt" \
-p 80:80 \
certbot/certbot certonly --standalone \
--email "${CERTBOT_EMAIL}" --agree-tos --no-eff-email \
-d "${VACHAN_DOMAIN}" --non-interactive --verbose

if [ $? -ne 0 ]; then
echo "Error: Failed to generate SSL certificates."
exit 1
fi
}

# Renew SSL certificates
renew_certificates() {
echo "Renewing SSL certificates for $DOMAIN..."

docker run --rm \
-v "$(pwd)${SSL_CERTS_DIR}:/etc/letsencrypt" \
certbot/certbot renew --non-interactive --verbose

if [ $? -ne 0 ]; then
echo "Error: Failed to renew SSL certificates."
exit 1
fi
}

# Restart Nginx container
start_nginx_container() {
echo "Starting Nginx container: ${NGINX_CONTAINER_NAME}..."
docker restart "${NGINX_CONTAINER_NAME}" || { echo "Failed to start Nginx container"; exit 1; }
}


# Execute the functions
if [ ! -e "${SSL_CERTS_DIR}/live/${DOMAIN}/fullchain.pem" ]; then
generate_certificates
start_nginx_container
else
renew_certificates
fi


echo "Script completed successfully."
18 changes: 11 additions & 7 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,29 @@ services:
volumes:
- ./nginx/prod/app.conf.template:/etc/nginx/templates/default.conf.template:ro
- ./certbot/www:/var/www/certbot/:ro
- ./certbot/conf/:/etc/nginx/ssl/:ro
- ./certbot/conf:/etc/nginx/certs/:ro
- logs-vol:/var/log/nginx/
environment:
- VACHAN_DOMAIN=${VACHAN_DOMAIN}
- VACHAN_DOMAIN=${VACHAN_DOMAIN}
profiles:
- deployment
networks:
- VE-network

certbot:
image: certbot/certbot:latest
image: certbot/certbot
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
- ./certbot/conf:/etc/letsencrypt
- ./auto_ssl_script.sh:/auto_ssl_script.sh
profiles:
- deployment
entrypoint: /bin/sh -c '
while :; do
/auto_ssl_script.sh;
sleep 80d;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to use the ofelia-scheduler which is already used for scheduled backups for this task?

done;'
networks:
- VE-network

- VE-network

ofelia-scheduler:
image: mcuadros/ofelia:v0.3.7
Expand Down
4 changes: 2 additions & 2 deletions docker/nginx/prod/app.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ server {
proxy_connect_timeout 300;
proxy_send_timeout 300;

ssl_certificate /etc/nginx/ssl/live/${VACHAN_DOMAIN}/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/${VACHAN_DOMAIN}/privkey.pem;
ssl_certificate /etc/nginx/certs/live/${VACHAN_DOMAIN}/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/live/${VACHAN_DOMAIN}/privkey.pem;


location /graphql/ {
Expand Down