How to quickly launch your wordpress with docker-compose.yml?

What you will have after reading this post:

  • Running website on the internet with your domain
  • Get a SSL certificate for your website with no cost

Re-prerequisites

  • Docker compose basic knowledge-base
  • Docker and docker-compose installed
  • Familiar with some basic linux commands
  • A server and it’s SSH connection
  • DNS configuration knowledge

Create a folder and a file see the image below

install_dependencies.sh with the content below

#!/bin/sh
set +e

IFS=$(printf '\n\t')

# Docker
yes | sudo apt-get remove docker docker-engine docker.io containerd runc
yes | sudo apt-get autoremove
sudo apt-get update
yes | sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
sudo apt-get update
yes | sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo usermod -aG docker $USER
newgrp docker
sudo systemctl start docker
sudo systemctl enable docker
printf '\nDocker installed and started successfully\n\n'
# Docker Compose
if ! [ -e "/usr/local/bin/docker-compose" ]; then
  sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  sudo chmod +x /usr/local/bin/docker-compose
fi

printf '\nDocker Compose installed successfully\n\n'

Add an execute permission chmod +x install_dependencies.sh

Create wordpress folder contains these files and folders

activate-domain.sh

sudo docker run -d -p 443:443 -p 80:80 \
-v ~/wordpress/letsencrypt:/etc/letsencrypt \
-v ~/wordpress/nginx:/etc/nginx/conf.d \
--restart=unless-stopped \
--network=wordpress_default \
--name=nginx-proxy nginx

create-ssl.sh

mkdir -p ~/wordpress/letsencrypt
docker run -it --rm -p 80:80 -v ~/wordpress/letsencrypt:/etc/letsencrypt certbot/certbot certonly --standalone -d yourdomain.com

deactivate-domain.sh

sudo docker container stop nginx-proxy && sudo docker container prune

docker-compose.yml

version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - ./database:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ptttlts
      MYSQL_DATABASE: shop
      MYSQL_USER: shop
      MYSQL_PASSWORD: ptttlts

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - ./html:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: shop
      WORDPRESS_DB_PASSWORD: ptttlts
      WORDPRESS_DB_NAME: shop

Run the command below to have nginx folder which later contains default.conf

mkdir -p nginx

Create file named default.conf with the following content

upstream wordpress {
    server server-ip-address:8000;
}

map $http_upgrade $connection_upgrade {
    default Upgrade;
    ''      close;
}

server {
    listen 443 ssl http2;
    server_name yourdomain.com;
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://wordpress;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 900s;
    }
}

server {
    listen 80;
    server_name yourdomain.com;
    return 301 https://$server_name$request_uri;
}

Create a .sh file to automate starting your website at server boot name it whatever you want and execute it

#!/bin/bash
SERVICE_NAME=yourdomain.com
SERVICE_FILE_PATH="./$SERVICE_NAME.service"
echo "[Unit]" >$SERVICE_FILE_PATH
echo "Description=Start project service" >>$SERVICE_FILE_PATH
echo "After=network.target" >>$SERVICE_FILE_PATH
echo "[Service]" >>$SERVICE_FILE_PATH
echo "WorkingDirectory=$(pwd)" >>$SERVICE_FILE_PATH
echo "ExecStart=$(find /usr/ -name "docker-compose") up" >>$SERVICE_FILE_PATH
echo "Restart=on-failure" >>$SERVICE_FILE_PATH
echo "StandardOutput=syslog" >>$SERVICE_FILE_PATH
echo "StandardError=syslog" >>$SERVICE_FILE_PATH
echo "[Install]" >>$SERVICE_FILE_PATH
echo "WantedBy=multi-user.target" >>$SERVICE_FILE_PATH
#Copying service file to /ets/systemd/system
sudo cp $SERVICE_FILE_PATH /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start $SERVICE_NAME.service
sudo systemctl enable $SERVICE_NAME.service

Step by step

Step 1: Run install_dependencies.sh to make sure we have docker and docker-compose installed on our system

Step 2: Create other folders and files following the above instruction

Step 3: Run create-ssl.sh

Step 4: Run activate-domain.sh

Step 5: Whenever you want to stop the domain just need to run deactivate-domain.sh