Posted on Leave a comment

Docker and WordPress / MySql

From a basic understanding of Docker, it becomes clear that it is extremely useful to house your WordPress installation inside a Docker container. Once completed your WordPress instance becomes highly portable and scalable. Additionally, there are other hidden benefits.



This article presumes you have a working copy of Docker installed.

Getting the WordPress and MySql Images

docker pull mysql
docker pull wordpress

Method 1 – Linking Containers

Create the MySql container

For this exercise, we will use environment variables at the command line. Keep in mind this will expose the MySql password to anyone who can read the history of the command line.

docker run --name mcp-mysql -e MYSQL_ROOT_PASSWORD=agoodpassword -d mysql

To add a user for the WordPress sight ‘exec’ a bash shell inside the MySQL container using the following command:

docker exec -it mcp-mysql mysql -u root -p

When prompted enter the password you provided above. You will be entered directly into a MySQL shell.

Creating a MySql user for the WordPress site.

CREATE USER 'mcp'@'%' IDENTIFIED WITH mysql_native_password BY 'agoodpassword';
GRANT ALL ON *.* TO 'mcp'@'%';

You will most likely have to alter the authentication method for the user that accesses MySql from WordPress:

ALTER USER 'mcp'@'%' IDENTIFIED WITH mysql_native_password
BY 'agoodpassword';  

Create the AL container and link it to the MySql container

Now you can run the docker container for WordPress.

docker run --name mcp-wordpress --link mcp-mysql -p 8082:80 -e WORDPRESS_DB_HOST=mcp-mysql:3306 -e WORDPRESS_DB_USER=mcp -e WORDPRESS_DB_PASSWORD=agoodpassword -e WORDPRESS_DB_NAME=wordpress -e WORDPRESS_TABLE_PREFIX=wp_ wordpress

NOTE: You can choose a different port than 8082. If you have no web server running on the host machine you can exclude the port option.

Check to see if both containers are running:

docker ps

Get the IP Address of each container

docker inspect mcp-wordpress | grep IPAddress

You can now browse to your WordPress container at localhost:8082, from the host machine.mcp-WordPress. If the host machine has something running on the same port, use the IP Address from above for the WordPress container.

Method 2 – Using Docker Compose

The newer approach is to define both containers using a config file and docker-compose.

Install docker-compose.

Official Documentation – The folks over at have provided a well-written set of instructions on using docker-compose to install WordPress alongside MySQL. If instructions do not work for you, head over there and get it from the source.

The Configuration File

Using your preferred editor create a file and call it docker-compose.yml ( the name is arbitrary, you can call it anything just end it with .yml )

version: '3.3'
      restart: always
         MYSQL_ROOT_PASSWORD: agoodpassword
         MYSQL_DATABASE: anewdb
         MYSQL_USER: wp_dbuser
         MYSQL_PASSWORD: paswordfordbuser
         - db
        image: wordpress
          - "8083:80"
        restart: always
          MYSQL_ROOT_PASSWORD: agoodpassword
          MYSQL_DATABASE: anewdb
          MYSQL_USER: wp_dbuser
          MYSQL_PASSWORD: paswordfordbuser`

Leave a Reply

Your email address will not be published. Required fields are marked *