Using WordPress docker container

WordPress is, arguably, the most popular CMS in existence. It is used extensively in website development.

Also hugely used in development is Docker - a container management system. Docker enables developers to quickly spin up development environments to test out their products and push them to production. All manner of docker images have been created to facilitate this ecology.

Here, we're going to document how to get a working WordPress site hosted on a Docker container connected to a MySQL database also hosted on the same Docker host.

Steps

Setup MySQL docker container

# docker run --name mydb-1 -e MYSQL_ROOT_PASSWORD='my-secret-password' -p 3306:3306 -d mysql:latest

This will create a generic mysql database. You will need to create your database and user afterwards.

Or, you can specify a user, password, database in 1 command.

# docker run --name mydb-1 -e MYSQL_USER='myuser' -e MYSQL_PASSWORD='my-secret-password' -e MYSQL_DATABASE='wpdb1' -p 3306:3306 -d mysql:latest

This will:

  • download and run the latest mysql server image
  • it will be named: mydb-1
  • create root password for mysql
  • create database: wpdb1
  • create user: myuser
  • assign password: my-secret-password
  • publish 3306 port
  • run in daemon/detached mode

Setup WordPress docker container

# docker run --name mywp-site -p 8080:80 -d wordpress

This will download the wordpress image and launch a container using that image.

From here, you browse to the site: http://localhost:8080 and the installer will be launched.

It will ask you for the database details that you specified above.

If all goes well, you will have a working WordPress site.

Make the connection