Build secured wordpress webpage with docker compose (containers, letsencrypt, SSL, https)

With this docker-compose.yml file we can create single wordpress instance that has SSL encription done via Letsencrypt.

To make it run in your VM you will need to install docker and docker compose and modify few environmental variables plus change path to certificates depending on where you storing them. But other than that it should be running smoothly.

Keep in mind that Letsencrypt is expecting DNS record to be already pointing to your VM.

Also when you are experimenting with SSL authentication via Letsencrypt and you have misconfiguration somewhere, after several tries Letsencrypt blocks you for few days. So you would need to retry later with fixed configuration.

Letsencrypt container is renewing certificates when they expire/before they expire so the process is fully automatic.

(base) root@ubuntu-19-04:~/docker_project# cat docker-compose.yml
version: "2"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /etc/nginx/vhost.d
      - /home/coil/certs:/etc/nginx/certs:ro
      - /usr/share/nginx/html
    restart: always 
    networks:
      - proxy_net
    labels:
      - com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
#-----------------------------------------------------   
  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: encrypt
    depends_on:
      - nginx-proxy
    volumes:
      - /home/coil/certs:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro
    volumes_from:
      - nginx-proxy
    restart: always 
    networks:
      - proxy_net
#-----------------------------------------------------   
  certs:
    image: tutum/apache-php
    container_name: certs
    environment:
      - VIRTUAL_HOST=data-ops.info,www.data-ops.info
      - LETSENCRYPT_HOST=data-ops.info,www.data-ops.info
      - LETSENCRYPT_EMAIL=michal.vasulka@gmail.com
    restart: always 
    networks:
      - cert_net
#-----------------------------------------------------      
  wordpress_webapp_0:
    image: wordpress
    container_name: wordpress_webapp_0
    links:
     - mariadb_webapp_0:mysql
    environment:
     - WORDPRESS_DB_PASSWORD=password
     - VIRTUAL_HOST=data-ops.info,www.data-ops.info
    ports:
     - "3000:80"
    depends_on:
      - nginx-proxy
      - letsencrypt    
      - mariadb_webapp_0
    volumes:
     - ./html_webapp_0:/var/www/html
    restart: always
    networks:
      - proxy_net

  mariadb_webapp_0:
    image: mariadb
    environment:
     - MYSQL_ROOT_PASSWORD=password
     - MYSQL_DATABASE_8=wordpress_webapp_0
    ports:
     - "3306:3306"
    depends_on:
     - nginx-proxy
     - letsencrypt      
    volumes:
     - ./database_webapp_0:/var/lib/mysql      
    restart: always
    networks:
      - proxy_net      

#----------------------------------------------------- 
networks:
  cert_net:
    driver: bridge
  proxy_net:
    driver: bridge
(base) root@ubuntu-19-04:~/docker_project# 

Check if all containers are running:

(base) root@ubuntu-19-04:~/docker_project# docker ps
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                      NAMES
0bf1e6ebb6af        wordpress                                "docker-entrypoint.s…"   4 months ago        Up 28 minutes       0.0.0.0:3000->80/tcp                       wordpress_webapp_0
3afe5d0d21fd        mariadb                                  "docker-entrypoint.s…"   4 months ago        Up 28 minutes       0.0.0.0:3306->3306/tcp                     docker_project_mariadb_webapp_0_1
afc0230b5494        jrcs/letsencrypt-nginx-proxy-companion   "/bin/bash /app/entr…"   4 months ago        Up 28 minutes                                                  encrypt
098ec248d2d4        tutum/apache-php                         "/run.sh"                4 months ago        Up 28 minutes       80/tcp                                     certs
75d26b4a44bb        jwilder/nginx-proxy                      "/app/docker-entrypo…"   4 months ago        Up 28 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   proxy
(base) root@ubuntu-19-04:~/docker_project# 

To check functionality just access your VM via web browser on port 80.

Source:
I have modified existing solution to suit my needs, can't find the source article/github repo at the moment.