Docker
Posted By Sebastian

Docker & WordPress


WordPress Containerization With Docker

Subscribe On YouTube

Setting up a local WordPress installation involves multiple steps and sometimes can be very cumbersome. Usually you need to setup a local web server (w.g. Apache), configure the server to be able to execute PHP code, and setup a MySQL database. Of course you can use pre-build bundles like MAMP for MacOS or XAMPP for Windows to get all those components on your system. However the most easiest way to setup a local WordPress environment is to use containerization with Docker. If you ‘re new to Docker also checkout my Docker Beginner’s Guide:

Docker Beginner’s Guide Part 1: Images & Containers
Docker Beginner’s Guide Part 2: Services

In this tutorial you’ll learn how to make use of Docker Images, Containers and Services to setup a complete local WordPress environment. This environment can then be used for testing, programming, or training purposes. E.g. you can use that testing environment to setup a copy of your production WordPress site and test any critical updates of WordPress itself, plugins etc.


If you like CodingTheSmartWay, then consider supporting us via Patreon. With your help we’re able to release developer tutorial more often. Thanks a lot!


Setting Up A docker-compose.yml file

The WordPress environment we’re setting up will consists of three Docker services:

  1. A service for providing and serving WordPress
  2. A service for the MySQL database
  3. A service for providing and serving phpMyAdmin

The services will be described and specified in a docker-compose.yml file which needs to be created first within an empty directory:

$ mkdir wp-docker

$ cd wp-docker

$ touch docker-compose.yml

Now you should have an empty docker-compose.yml file inside the folder wp-docker. Open the file in your favorite editor and start inserting the following code:

version: '3'

services:
  db:
    image: mysql:8
    container_name: mysql
    restart: always
    command: "--default-authentication-plugin=mysql_native_password"
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wpdb
      MYSQL_USER: user
      MYSQL_PASSWORD: password

Inside the services section we’ve added the first service definition which is named db. This service is associated with the mysql image and therefore providing the needed MySQL database instance.

Due to the fact that we’re using MySQL 8 it is also important to add the following line of code:

command: "--default-authentication-plugin=mysql_native_password"

By adding this command option we’re specifying that the database should be run with native password authentication enables.

Furthermore we need to set the following environment variables: MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD.

Adding The WordPress Service

Next let’s add a service for WordPress to the docker.compose.yml file:

services:
  [...]
  wordpress:
    image: wordpress:4.9.8
    container_name: wordpress
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wpdb
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
    ports:
      - 8080:80
      - 443:443

Here we’re using the wordpress image. You can specify to use any version of WordPress. In the example from above WP version 4.9.8 has been selected. This is the last version before the WordPress 5 release, so that we’re able to use that environment to install a backup of any website running on that version and then test if the update to version 5 is working without problems and errors.

The volumes property is used to mount to container directory /var/www/html/wp-content to the local folder wp-content. Everything which is inserted into the local folder will be made available in the corresponding container folder. If you want to install a backup of any existing website you can just copy the content of the wp-content folder from your production WP installation to the local wp-content folder to make it available to that WorkPress instance.

Furthermore we’re again using the environment section to set values for four environment variables: WORDPRESS_DB_HOST, WORDPRESS_DB_NAME, WORDPRESS_DB_USER, and WORDPRESS_DB_PASSWORD.

The value which is assigned to WORDPRESS_DB_HOST needs to be the identifier of the database service (db). The value of the environment variable WORDPRESS_DB_NAME needs to correspond to the value which has been assigned to the environment variable MYSQL_DATABASE of the database service.

Furthermore the environment variables WORDPRESS_DB_USER and WORDPRESS_DB_PASSWORD needs to contain the credentials of the database (previously set via MYSQL_USER and WORDPRESS_DB_PASSWORD in the db service).

Finally we’re making sure to map internal port 80 to external port 8080, so that WordPress will be accessible via http://localhost:8080.

Adding The phpMyAdmin Service

As a third service we’re adding phpMyAdmin to our Docker setup. To do so add the following service definition to docker-compose.yml:

services:
  [...]
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 3333:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORT: password

Here we’re making use of the phpmyadmin/phpmyadmin image (https://hub.docker.com/r/phpmyadmin/phpmyadmin). phpMyAdmin is a PHP-based web interface for the MySQL database. Making phpMyAdmin available will let us import and export content from the MySQL-database easily.

In order to enable database access for phpMyAdmin we need to set two environment variables for this service:

  • PMA_HOST: needs to be set to the name of the database service (db in our case)
  • MYSQL_ROOT_PASSWORD: needs to be set to the MySQL root password (which is just password in our case)

The internal port 80 (on which phpMyAdmin is exposed by default is mapped to the external port 3333 so that we should be able to access the phpMyAdmin web user interface by using URL http://localhost:3333 later on.

The Complete docker-compose.yml File:

Again, here you can see the complete code of docker-compose.yml:

version: '3'

services:
  wordpress:
    image: wordpress:4.9.8
    container_name: wordpress
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wpdb
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
    ports:
      - 8080:80
      - 443:443

  db:
    image: mysql:8
    container_name: mysql
    restart: always
    command: "--default-authentication-plugin=mysql_native_password"
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wpdb
      MYSQL_USER: user
      MYSQL_PASSWORD: password

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 3333:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORT: password

Starting All Services

With the docker-compose.yml file in place we’re now ready to start up all containers with just one command:

$ docker-compose up

You’ll received outputs from all services directly on the command line. Once everything is started up successfully you should be able to see the first step of the WordPress installation procedure when accessing URL http://localhost:8080 in the browser:

Choose a language and click on button Continue to proceed with the next step of the installation procedure:

Now you need to choose a site title, username, password, and an email address which should be associated with your WordPress account. Having finished this steps takes you to the WordPress login screen:

Here you should be able to sign in with the previously chosen username and password. You’ll then be redirected to the WordPress Dashboard:

By using URL http://localhost:3333 you should also be able to access phpMyAdmin. You’re able to sign in to the phpMyAdmin user interface by using username root and password password:

On the left side you can see the overview of available databases. The last entry in the list is the database wpdb which has been created to contain the WordPress tables. By clicking on the + Icon you can expand the database entry and see which tables are available:

From here you can use phpMyAdmin to get further insights into the tables, view and alter data or just use the import functionality to import the database content from your production WordPress database:

You can also use command line option -d to tun the command in detached mode (in the background):

$ docker-compose up -d

In case you’re running everything in detached mode you can stop services by running the following command:

$ docker-compose stop

Once you have stopped services you’re able to restarting everything again by using option start of the docker-compose command:

$ docker-compose start

You can stop and remove services and corresponding containers which have been created by docker-compose up you can use the following command:

$ docker-compose down

By adding option –volumes to the command all the volumnes which have been created are deleted as well from the local system:

$ docker-compose down --volumes

Conclusion

A local WordPress test environment is great for

  • Testing out new versions of WordPress
  • Setting up a local WordPress development environment
  • Testing critical updates for your production site

In this tutorial you’ve learned how you can use Docker and containerization to setup a local WordPress installation very easily.

Top 3 Docker Online Courses


Using and writing about best practices and latest technologies in web design & development is my passion.