Cloud

How to install WordPress with Docker

Deploying containers with Docker isn't nearly as complicated as you might think. Jack Wallen walks you through the process of installing and deploying WordPress, with the help of Docker.

dockerhero.jpg
Image: Jack Wallen

Docker is an incredibly powerful and flexible tool that allows you to serve up containerized applications such that they are encapsulated and much easier to deploy. I want to demonstrate how easy this is by installing WordPress via Docker.

I will be working on the Ubuntu 16.04 Server platform and will assume you already have Docker up and running. If you don't have Docker up and running, see "How to install Docker on Ubuntu 16.04" to find out how to install the world's most popular container platform.

Let's get to the installation.

Must-Read Cloud

Survey: Has your company implemented a hybrid cloud solution?

Fill out this five minute survey and tell us about your company's cloud use.

Install and set up the database container

Since WordPress relies upon a database, we'll install MariaDB via Docker and then create the necessary database. The first thing that must be done is to open up a terminal window and pull the latest MariaDB container with the command:

sudo docker pull mariadb

Once the container has been successfully pulled, you must first create a few directories to house the WordPress data. This is done with the following commands (we'll create the directory in /opt).

sudo mkdir /opt/wordpress
​sudo mkdir -p /opt/wordpress/database
​sudo mkdir -p /opt/wordpress/html

Next we create the MariaDB container with the command:

docker run -e MYSQL_ROOT_PASSWORD=XXX -e MYSQL_USER=wpuser -e MYSQL_PASSWORD=XXX -e MYSQL_DATABASE=wordpress_db -v /opt/wordpress/database:/var/lib/mysql --name wordpressdb -d mariadb

Where XXX are strong passwords to be used for the MySQL root user and the MySQL wpuser account.

The above command will create the wpuser account and create the WordPress database, wordpress_db

Next we need to find out what IP address is associated with our new container. Issue the command:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' wordpressdb

The command will reply with the IP address for your container (Figure A).

Figure A

Figure A

Finding the IP address of your MariaDB docker container.

You can now connect to MySQL on that address with the command:

mysql -u wpuser -h IP_ADDRESS -p

Where IP_ADDRESS is the actual address for the container. You'll be prompted for the password you created for the wpuser; enter that and the MySQL prompt will appear. Exit out of that prompt by typing exit; and you're ready to move on.

Install WordPress

Now we must pull down the latest version of WordPress. This is done with the following command:

docker pull wordpress:latest

Allow that pull to complete and then create the new container with the command:

docker run -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=XXX -e WORDPRESS_DB_NAME=wordpress_db -p 8081:80 -v /opt/wordpress/html:/var/www/html --link wordpressdb:mysql --name wpcontainer -d wordpress

Where XXX is the password for the wpuser you created earlier.

At this point you can now point a browser to http://SERVER_IP:8081 and begin the standard WordPress installation. Where SERVER_IP is the IP address of the server hosting docker, not the container address. Congratulations, you've just deployed your WordPress container.

The simplicity of containers

As you can see, containers are actually quite simple to deploy (even with the added complexity of having to connect with a database server). With a few commands you can have a containerized WordPress deployment up and running, thanks to Docker.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox