Installing WordPress with Docker in Ubuntu/Debian and CentOS. 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, run-time, system tools, and libraries. This allows anyone to package an application with its dependencies into a standardized building block.
# Debian and Ubuntu sudo apt-get update # CentOS sudo yum update
Let’s check if we have curl installed. Related Articles
Curl comes pre-installed with most Linux distribution, but if it isn’t installed check manually.
# Debian and Ubuntu sudo apt-get install curl # CentOS sudo yum install curl
Use this command to download and install Docker.
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.
If the command does not work immediately, restart the Docker service with the following and try to run the hello-world app again.
sudo systemctl restart docker
Docker should now be installed and working correctly. Continue on below with the rest of the WordPress setup.
MariaDB in a container
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
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.
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
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 80:80 – Tells Docker to pass connections from your server’s HTTP port to the containers internal port 80.
- -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.
docker run -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress
If you get an error linking your server’s public IP address to the WordPress container’s internal address, remove the failed 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 systemctl restart docker docker start wordpressdb
Then try creating the WordPress container again.