Install WordPress with docker-compose for dummies

What you get

A WordPress with MySQL database which can be accessed through phpmyadmin. With minor changes you can host multiple WordPress blogs on the same domain. All this is possible thanks to docker and docker-compose. If you are in a rush and you need to get the job done then go to the “Quick start” section otherwise you can look at the “docker-compose.yml explanation” part.

About docker-compose

Docker is amazing, but with docker-compose everything is even easier. Why? Instead of running long command line scripts you defined everything (image, containers, links between containers, ports and volumes) in a docker-compose.yml file.

Quick start

Start server and install docker

If you don’t have time to understand what all this scripts, this is the place to go, see here more detailed information at “docker-compose.yml explanation”.
Start a server or a virtualized server instance and install:

  • Any Unix OS;
  • Install docker and docker-compose;

Install wordpress

The following script will install wordpress, mysql, phpmyadmin and nginx. Create a file named docker-compose.yml and put the following code. Will create a folder \wordpress which will contain the wp-content folder (images, themes and plugins).

Important before you copy: Remember to replace the {Wordpress domain} for your domain or subdomain and {MySQL password} for a random password.

Example of {Wordpress domain}: hackhat.com, projs.hackhat.com.
(Generate a random password here)
File name: docker-compose.yml.

web:
    image: wordpress
    links:
     - mysql
    environment:
     - WORDPRESS_DB_PASSWORD={MySQL password}
     - VIRTUAL_HOST={Wordpress domain}
    ports:
     - "127.0.0.1:8081:80"
    working_dir: /var/www/html
    volumes:
     - /wordpress/wp-content/:/var/www/html/wp-content
mysql:
    image: mysql:5.7
    ports:
     - "127.0.0.1:3306:3306"
    environment:
     - MYSQL_ROOT_PASSWORD={MySQL password}
     - MYSQL_DATABASE=wordpress
phpmyadmin:
  image: corbinu/docker-phpmyadmin
  links:
    - mysql
  ports:
    - 8080:80
  environment:
    MYSQL_USERNAME: root
    MYSQL_ROOT_PASSWORD: {MySQL password}
nginx:
    image: jwilder/nginx-proxy
    ports:
     - 80:80
    volumes:
     - /var/run/docker.sock:/tmp/docker.sock:ro

After this create a directory \wordpress and add 777 permissions to it (sudo chmod -R 777 \wordpress). This step is necessary in order to update your plugins and other WordPress stuff.
Now run this magic command and wait docker-compose up -d. This will install wordpress, mysql, phpmyadmin, nginx and will link them together (links wordpress to mysql and phpmyadmin to mysql).

Docker-compose.yml explanation

First level

If you notice the first level (web, mysql, phpmyadmin and nginx) are container names, not image names, therefore you can name them as you wish. Each container contains more keys inside which describe them better.

WordPress (web)

Image

image: wordpress

The first is the web container which has the official WordPress docker image.

MySQL link

Has a link to mysql:

links:
 - mysql

This is the short hand and is equivalent to:

links:
 - mysql:mysql

Environment variables

Has 2 environment variables

environment:
 - WORDPRESS_DB_PASSWORD={MySQL password}
 - VIRTUAL_HOST={Wordpress domain}

The first is necessary in order to know the password of MySQL, without this you cannot connect to the database, therefore nothing would work. The second line is a special environment that the nginx container will look for. When you set the VIRTUAL_HOST on a container, it will associate that container to a certain domain. For example adding – VIRTUAL_HOST=hackhat.com it will make that container accessible from hackhat.com (of course in your domain you have to point to the server IP). In this way you can have multiple WordPress instances in the same server with different domains.

Ports

ports:
 - "127.0.0.1:8081:80"

It means that whatever is inside the docker container at localhost:80 will be put in the host to the ip 127.0.0.1 and at the port 8081. (by default localhost is 127.0.0.1). In this particular case it doesn’t really matter where you export because nginx takes care of making it accessible from a domain.

Volumes

volumes:
 - /wordpress/wp-content/:/var/www/html/wp-content

The above line means that whatever will be in the container at the /var/www/html/wp-content location will be saved on the host machine at this location /wordpress/wp-content/. Basically allows you to see from the host a folder from within the container. Is useful to make backups and also if you delete the container will not delete the data, read more in the official guide Manage data in containers.

MySQL DataBase (mysql)

I will make a shorter summary here, but it follows the same rules as the previous container with the exception of - MYSQL_DATABASE=wordpress which is used to create a database when the image startups. Read more about it on the official MySQL docker image.
The only thing that can look weird is image: mysql:5.7 which means it will install the version 5.7 of MySQL.
An improvement could be to save the database to a external volume.

Phpmyadmin (phpmyadmin)

Phpmyadmin image and again, nothing new here, the only different thing is the:

ports:
 - 8080:80

Which means that it will export localhost:80 from the container to the host at localhost:8080.

NGINX (nginx)

Again nothing much more than the Automated Nginx reverse proxy for docker containers.

Conclusions

I’m pretty happy with the docker-compose because I don’t have to maintain all those messy command line codes. The only thing that I don’t like is that is not straightforward to get started, for example just by reading the WordPress docker read-me, is really hard to figure out how to setup WordPress. The same is for other docker images.
I can see that the future will be in this area because now that I’ve setup all these links and container, I can do it again in no time as many times as needed.

Read more about programming, server and marketing at hackhat.com

Share on Twitter if you found it useful: