I used three images in my docker compose yaml file, They are mysql, wordpress and certbot.

1. certbot from Let’s Encrypt

certbot is image from Let’s Encrypt and it is free. The docker compose syntax looks as below.

    image: certbot/certbot
    container_name: certbot
    volumes:
      - ./certbotdata:/etc/letsencrypt
      - ./webroot:/var/www/html
    command: certonly --webroot --webroot-path=/xxx/xxx/xxxx --email john.doe@gmail.com --agree-tos --no-eff-email --force-renewal -d mysite1.com -d www.mysite1.com 

When containers are running. certbot container runs only once to generate your certificates. They can be found in ./certbotdate/live/mysite1.com/*. You can use them for Apache2 in WordPress container.

2. WordPress

Once containers are up. Log into container shell to get two configuration files.

docker exec -it <container id or name> /bin/bash

one for HTTP/80 and another for HTTPS/443. They can be found /etc/apache2/sites-available and copy contents of them. After that you can create those two files in your local volume directory. Then, you can edit them as you need. You will need to change only two lines one for cert and another for private key. Then, you can add them into your docker compose volume like.

volumes:
  ........
  - ./apache_conf:/etc/apache2/sites-available
  - ./certbotdata/live/waterloobae.com/cert.pem:/etc/ssl/certs/cert.pem
  - ./certbotdata/live/waterloobae.com/privkey.pem:/etc/ssl/certs/privkey.pem 

3. Enabling SSL

WordPress docker does not come up with SSL enabled. So, you will need to enable it with Dockerfile. MySQL and certbot can containerize from images. However, WordPress has to start with Dockerfile like.

FROM wordpress:latest
RUN apt-get update && \
apt-get install -y --no-install-recommends ssl-cert && \
rm -r /var/lib/apt/lists/*
RUN a2enmod ssl && \
a2ensite default-ssl

I hope I’ve covered everything you need to know. Happy Dockering!