Deploy Wordpress using Docker Compose

 

In this project, I was assigned to deploy wordpress by using docker-compose. The compose file must contain two services (wordpress and database) and the service must be able to communicate each other.

Table of contents

1. Docker and Docker Compose Installation

First, install docker with the script that docker provided to us in this link.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

get docker

After that, add current user to docker group, exit the current session, and reconnect to SSH (this need to be done).

sudo usermod -aG docker ubuntu

usermod docker

Create new directory for the project and install docker-compose with apt.

sudo apt install docker-compose -yqqq

2. Configuring Docker Compose

Create new compose file named docker-compose.yml with two service (wordpress and mysql).

version: '3'

services:
   db:
       image: mysql:8.0
       volumes:
        - db_data:/var/lib/mysql
       restart: always
       environment:
          MYSQL_ROOT_PASSWORD: password-adser-wp123
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wp-adser
          MYSQL_PASSWORD: wp-adser123
       ports:
        - "3306:3306"

   wordpress:
       depends_on:
        - db
       image: wordpress:latest
       restart: always
       environment:
          WORDPRESS_DB_HOST: db
          WORDPRESS_DB_USER: wp-adser
          WORDPRESS_DB_PASSWORD: wp-adser123
          WORDPRESS_DB_NAME: wordpress
       ports:
        - "80:80"

volumes:
    db_data: {}

As you can see, in this config file, first we create db service with mysql:8.0 image. Then, we define the shared volume to save mysql data and restart policy to always. We also configuring some environment variable for wordpress application and define the port (3306 on host is forwarded to 3306 port on mysql docker container). On the wordpress service we define depends_on value to make sure that db is created earlier before wordpress container. The environment variable is set to follow what we defined earlier. We can also separate our env declaration to .env file and define env_file: .env on the services.

Save the file and start docker-compose with the command,

docker-compose up -d

To check if the container has been created successfully, you can use ps command in docker-compose or just list the active container on docker.

docker-compose ps   # see the docker-compose process
docker container ls # to see the active container

docker compose

3. Finish Your Wordpress Setup

Finish your wordpress setup.

wp setup

Voila! You now have wordpress site running on docker!

wp index