Deploying WordPress in Docker Containers

Leave your reply

Learn how to run a WordPress installation in Docker containers, both manually and using Docker Compose. WordPress developers will find it useful to deploy WordPress in Docker containers. Docker allows you to test on multiple WordPress configurations, and launch a fresh WordPress installation with a few simple commands.

Requirements

  • A server running CentOS 7 or Ubuntu 14.04
  • Web services installed and running.
  • Docker installed and running.
  • Basic knowledge of Docker usage and commands.
  • Optional: A basic understanding of Docker Compose.

Running WordPress in Docker Containers

A successful WordPress installation has three elements:

  • The WordPress software

  • A MySQL or MariaDB database

  • The final installation steps, which are completed in a browser

For the following examples, the WordPress and MySQL/MariaDB components will run in separate linked containers. The container running the WordPress software will be mapped to a port on the host, which will allow you to access it in a browser.

Run a MySQL/MariaDB Container

First, run a container named my-db with the root password of mysql-password. You can use either MySQL or MariaDB, which is a drop-in replacement for MySQL.

Note: Be sure to change db-password to a secure password.

MySQL

Launch a container with the command:

sudo docker run --name my-db -e MYSQL_ROOT_PASSWORD=db-password -d mysql

MariaDB

Launch a container with the command:

sudo docker run --name my-db -e MYSQL_ROOT_PASSWORD=db-password -d mariadb

Run a WordPress Container

Next, run a container from the official WordPress image which is mapped to the host port 8080 and linked to the database container.

Two notes:

  • If you have a firewall, you may need to add access to port 8080.
  • If you already have another service running on port 8080, you can choose a different port on the host.

The command will vary slightly depending on whether you are using MySQL or MariaDB:

MySQL

Launch a WordPress container with the command:

sudo docker run --name my-wordpress -p 8080:80 --link my-db:mysql -d wordpress  

MariaDB

Launch a WordPress container with the command:

sudo docker run --name my-wordpress -p 8080:80 --link my-db:mariadb -d wordpress 

There are many other environment variables you can add to this command if you want to override the defaults, including:

  • -e WORDPRESS_DB_HOST=[hostname] The default is the IP address and port of the linked MySQL/MariaDB container. You can use this variable to access a MySQL/MariaDB database on another server.
  • -e WORDPRESS_DB_USER=[username] The default is root.
  • -e WORDPRESS_DB_PASSWORD=[password] The default is the MYSQL_ROOT_PASSWORD environment variable of the linked MySQL/MariaDB container.
  • -e WORDPRESS_DB_NAME=[name] The default is "wordpress".

Finish the Installation in a Browser

For the final installation steps, you will need to access the WordPress container from a browser.

In the previous example we mapped port 8080 on the host to port 80 (web services) on the container. This allows you to access the container in a browser using either the IP address or the URL of the server:

Visit the URL in a browser, choose your install language, then click Continue.

WordPress

On the next page, fill out the fields.

WordPress
  • Site Title: Fill out the title of your website.
  • Username: This will be the main administrative username for your site. Note: for security reasons, we recommend that you do NOT use "Admin" or your website's name or URL for this username.
  • Password: Make a note of this password before you continue.
  • Your Email: This will be the email address for the main administrative username.

Then click the Install WordPress button to finalize the installation.

Using Docker Compose to run WordPress

It is easy to launch WordPress installations with Docker Compose. For information on installing and using Docker Compose, see our article Launch and Orchestrate Docker Containers With Docker Compose.

Create the YAML File

First, create a directory for your project and move into it:

sudo mkdir wordpress
cd wordpress

Create a YAML file named docker-compose.yml with the command:

sudo nano docker-compose.yml

The contents of the file will vary slightly depending on whether you are using MySQL or MariaDB:

MySQL

Put the following into the file:

wordpress:
  image: wordpress
  links:
    - wordpress_db:mysql
  ports:
    - 8080:80

wordpress_db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: db-password

Note: Be sure to change db-password to a secure password.

Save and exit the file.

MariaDB

Put the following into the file:

wordpress:
  image: wordpress
  links:
    - wordpress_db:mariadb
  ports:
    - 8080:80

wordpress_db:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: db-password

Note: Be sure to change db-password to a secure password.

Save and exit the file.

Launch the Containers

Next, use Docker Compose to launch these containers with the command:

sudo docker-compose up -d

You can verify that the containers were created by using the command:

sudo docker-compose ps