WordPress is one of the most popular content management software (CMS) due to its multitude of features and ease of use. However setting up a new web host environment can be time consuming especially if you need to do it often. Simplifying the installation process to a few fast commands greatly reduce the time and effort required, this is where Docker comes in. Installing WordPress with Docker is a breeze, read ahead to find out more.
Docker is a container platform that allows simple and fast software installations on any system and OS. It wraps the piece of software in a complete file system that includes everything it needs to run such as code, runtime, system tools and libraries. This allows anyone to package an application with its dependencies into a standardized building block.
Installing Docker itself is already easy. Firstly run the usual update command for your system to make sure you have the latest source lists.
# Debian and Ubuntu sudo apt-get update # CentOS sudo yum update
Check that you have the curl command line utility.
It comes preinstalled with most Linux distributions, but if it can not be found, install it manually with the appropriate command for your OS.
# Debian and Ubuntu sudo apt-get install curl # CentOS sudo yum install curl
Use the command below to download and install Docker. The process requires root privileges so you will be asked for your sudo password on any non-root user.
curl -fsSL https://get.docker.com/ | sh
Towards the end of the installation process you will see a suggestion to add your username to the Docker users group. Doing this allows you to run Docker commands without needing to invoke sudo every time.
sudo usermod -aG docker <username>
Log out and back in again after adding yourself to the Docker users group before continuing.
You can check that the installation was successful with the following test program:
docker run hello-world
You should see an output similar to the example below.
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world ... Hello from Docker. This message shows that your installation appears to be working correctly. ...
If the command does not work immediately, restart the Docker service with the following and try to run the hello-world app again.
sudo service docker restart
Docker should now be installed and working correctly. Continue on below with the rest of the WordPress setup.
MariaDB in a container
Before installing WordPress with Docker you will need to have somewhere to store the data. MariaDB is a community-developed relational database management system and a drop-in replacement for MySQL. It is officially available on Docker and provides easy instructions with up to date images.
Start of by making a new directory where you wish to store the files for WordPress and MariaDB for example in your home directory.
mkdir ~/wordpress && cd ~/wordpress
Downloading and installing a new MariaDB container can all be performed with a single command. Before jumping in check the required parameters.
MariaDB Environment variables, these are marked in the Docker command with -e:
- -e MYSQL_ROOT_PASSWORD= Set your own password here.
- -e MYSQL_DATABASE= Creates and names a new database e.g. wordpress.
- --name wordpressdb – Names the container.
- -v “$PWD/database”:/var/lib/mysql – Creates a data directory linked to the container storage to ensure data persistence.
- -d – Tells Docker to run the container in daemon.
- mariadb:latest – Finally defines what to install and which version.
Then run the command below while replacing the <password> with your own.
docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
... Status: Downloaded newer image for mariadb:latest 23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54
If Docker was successful at creating the container, you should see a code at the end of the output similar to the example above. You can confirm that the MariaDB container is running by using the following command:
Check the status for your MariaDB install, it should show “Up” and the time it has been running like in the example output below.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14649c5b7e9a mariadb:latest "/docker-entrypoint.s" 12 seconds ago Up 12 seconds 3306/tcp mariadb
Other useful commands for working with containers are start, stop and remove.
docker start <container name> docker stop <container name> docker rm <container name>
You can find out more about available commands and options to specific commands.
docker --help docker <command> --help
Full command-line documentation is also available over at Docker support page.
WordPress with Docker
Applications in containers run isolated from one an other in the userspace of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. To allow applications within different containers to work with one another Docker supports container linking.
WordPress is also made officially available on Docker Hub, pull the image using with the command below. When the version to download is not specified Docker will fetch the latest available.
docker pull wordpress
WordPress container also takes environment variables and Docker parameters:
- -e WORDPRESS_DB_PASSWORD= Set the same database password here.
- --name wordpress – Gives the container a name.
- --link wordpressdb:mysql – Links the WordPress container with the MariaDB container so that the applications can interact.
- -p <server public IP>:80:80 – Tells Docker to pass connections from your server’s HTTP port to the containers internal port 80. Replace <server public IP> with the public IP address of your server.
- -v “$PWD/html”:/var/www/html – Sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed.
- -d – Makes the container run on background
- wordpress – Tells Docker what to install. Uses the package downloaded earlier with the docker pull wordpress -command.
Run the command below while replacing the <password> and <server public IP> as appropriate to your cloud server.
docker run -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p <server public IP>:80:80 -v "$PWD/html":/var/www/html -d wordpress
Then open your server’s domain name or IP address in a web browser to test the installation. You should be redirected to the initial WordPress setup page at http://<domain>/wp-admin/install.php. Go through the setup wizard and you are done.
If you get an error linking your server’s public IP address to the WordPress container’s internal address, remove the problematic container using the following command:
docker rm wordpress
Restart Docker and the database container, also make sure no other service is already bound to the port 80.
sudo service docker restart docker start wordpressdb
Then try creating the WordPress container again.
Congratulations, you should now have a simple WordPress with Docker running in a container and an easy way to configure an other one if needed. Before continuing on building your new WordPress site, make sure to pay attention to the security on your server. To find out more, check out our article for How To Secure Your Linux Cloud Server.