How to Install WordPress with Docker Compose (Step by Step)

Docker is an open-source containerization platform that helps developers to set up a local development environment. It enables you to package applications into containers. However, if you are using more than one container for your application, you have to create several Docker files. Managing and monitoring all those containers is a time-consuming process. This is the place where Docker Compose comes into the picture.

Docker Compose is a tool used to defines and runs a multi-container Docker application. This will solve your problem by allowing you to use a YAML file to manage multiple containers at once. You can set any number of containers, define volumes, images, builds then use a single command to run and manage all containers. Here is a nice guide on how to install Docker Compose AWS.

Docker Compose WordPress

Most applications are built from several components. In this case, Docker Compose will allow you to define and run all these components from a single file. WordPress is a good example of a stack application. WordPress consists of a web server container, database container, and phpMyAdmin container. You can create a simple YAML file to bundle these WordPress components. Then use Docker Compose to build, run, and inter-connect all WordPress containers to host a WordPress CMS. You should also read install wordpress on aws ubuntu.

In this post, we will demonstrate how to install WordPress CMS using Docker Compose on Ubuntu 20.04 server.

Step 1: Installing Docker Engine

Before starting, Docker must be installed on your system. If you are installing Docker for the first time, you will need to set up the Docker repository in your system.

First, install required dependencies using the following command:

					apt-get install apt-transport-https curl ca-certificates curl software-properties-common -y

Next, add Docker’s GPG key using the command below:

					curl -fsSL | apt-key add -

Next, add the Docker repository to the APT source file.

					add-apt-repository "deb [arch=amd64] focal stable"

Finally, install the latest version of Docker using the command below:

					apt-get install docker-ce -y

Once the Docker is installed, verify the Docker version using the following command:

					docker --version

Sample output:

					Docker version 20.10.8, build 3967b7d

Step 2: Installing Docker Compose

You also need to install Docker Compose in your system. The latest version of Docker Compose is not included in the Ubuntu default repository. So you will need to download it from the Git Hub download page.

Run the following command to download the current stable release of Docker Compose:

					curl -L -o /usr/local/bin/docker-compose

Next, set the executable permission to the downloaded binary:

					chmod +x /usr/local/bin/docker-compose

Next, verify the Docker Compose version using the command below:

					docker-compose --version

Sample output:

					docker-compose version 1.29.2, build 5becea4c

Step 3: Setting Up YML File

Next, you will need to create a docker-compose.yml file to define, WordPress container, Database container, Volume, and Environment. This will tell Docker how to configure and start the WordPress and Database container.

First, create a WordPress directory to hold your docker-compose.yml file:

					mkdir wordpress

Next, create a docker-compose.yml file using the following command:

					nano wordpress/docker-compose.yml

Add the following code to define Docker Compose version, wordpress and database service, volumes and environment:

					version: "3"
    image: mysql
    restart: always
      MYSQL_ROOT_PASSWORD: wppassword
      MYSQL_DATABASE: wpdb
      MYSQL_USER: wpuser
      MYSQL_PASSWORD: wppassword
      - mysql:/var/lib/mysql

      - database
    image: wordpress:latest
    restart: always
      - "80:80"
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_USER: wpuser
      WORDPRESS_DB_PASSWORD: wppassword
  mysql: {}

Save and close the file when you are finished.

A brief explanation of the above YML file is shown below:

  • database: Define the database service.
  • image: Tell Docker Compose what image to pull to create the container.
  • environment: Define environment variables to store database credentials.
  • volumes: Define the data directory for WordPress and MySQL.
  • ports: Used to exposes port 80 to access the WordPress application.
  • dependes_on: Define the dependent container or service.

At this point, the Docker Compose file is ready to deploy the WordPress container.

Step 4: Building WordPress Container

After creating a docker-compose.yml file, you will need to start WordPress and Database container.

First, change the directory to wordpress:

					cd wordpress

Next, start both containers using the pre-defined values:

					docker-compose up -d

This command will download WordPress and MySQL image from the Docker repository starts both containers in the background and leaves them running.

					Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd
Status: Downloaded newer image for mysql:latest
Creating wordpress_database_1 ... done
Creating wordpress_wordpress_1 ... done


You can check both container images downloaded by the dokcer-compose.yml file using the command below:

					docker-compose images

Sample output:

					      Container         Repository    Tag       Image Id       Size  
wordpress_database_1    mysql        latest   c60d96bd2b77   513.8 MB
wordpress_wordpress_1   wordpress    latest   baf5889057ff   550.7 MB

Next, verify the running container with the following command:

					docker-compose ps

Sample output:

					        Name                       Command               State                Ports              
wordpress_database_1 mysqld      Up      3306/tcp, 33060/tcp             
wordpress_wordpress_1 apach ...   Up>80/tcp,:::80->80/tcp

To check the WordPress container logs, run:

					docker-compose logs wordpress

Sample output:

					wordpress_1 | [Thu Aug 12 16:07:57.200672 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.22 configured -- resuming normal operations
wordpress_1 | [Thu Aug 12 16:07:57.200907 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

To check the Database container logs, run:

					docker-compose logs database

Sample output:

					database_1   | 2021-08-12T16:37:59.123898Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
database_1   | 2021-08-12T16:37:59.124030Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: ''  port: 3306  MySQL Community Server - GPL.

Step 5: Getting Access to WordPress Website

At this point, your WordPress site is ready. Now, open your web browser and type the URL http://your-server-ip. You will be redirected to the WordPress language selection screen:

WordPress Docker Container

Select your preferred language and click on the Continue button. You should see the WordPress site configuration screen:

WordPress Docker Compose

Provide your website name, admin username, password, email, and click on Install WordPress. Once the installation is completed, you should see the following screen:

WordPress Installation Success Screen

Click on Log in button. You will be redirected to the WordPress login screen:

WordPress Login Screen

Provide your WordPress admin username, password and click on Login button. You will be redirected to the WordPress dashboard as shown below:

Setup WordPress Docker

Step 6: Updating WordPress Container

If your WordPress and MySQL Docker images have changed since the container was created, you can update them by stopping and recreating all containers again.

First, stop all containers using the following command:

					cd wordpress
docker-compose down

Next, download the latest version of WordPress and MySQL image using the following command:

					docker-compose pull

Next, create and launch both container using the following command:

					docker-compose up -d


Your WordPress website is now up and running in a containerization environment. I hope you have now enough understanding of how Docker Compose works and helps you to set up a WordPress website within a minute.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

3 2 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x