WordPress is the most popular content management system (CMS) in the world. Over 40% of the web runs on WordPress. Docker is a containerization system used to run your software in an isolated environment. The concept of containerization is not new to the world of software development. But Docker makes it very developer friendly.
In this article, we will show you how to install a WordPress site in a Docker container.
Why Docker, you ask me?
The answer is simplicity. In Docker, your application is containerized into an image. There is no need to do complicated installation and configuration to make it work.
Not to mention that it’s also faster and easier for you to migrate/upgrade to another server. Say your WordPress is getting really popular and you need to upgrade your existing server? It’s easy to do with Docker, since everything is already containerized. If you develop something using Docker, after deployment it works everywhere.
In this article, we are going to install a Docker engine in Ubuntu server.
Before installing anything, if an older version of Docker is installed on your system, remove it. Run this command in your terminal to remove all older versions of Docker.
sudo apt autoremove docker docker-engine docker.io containerd runc
Now let’s start our installation:
- Update your local package indexes by running apt-get update and install dependencies for the Docker engine.
sudo apt update sudo apt install ca-certificates curl gnupg lsb-release
- Add the official Docker GPG key.
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Configure the repository.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Install the latest version of the Docker engine.
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Verify the Docker installation by running a hello world image.
sudo docker run hello-world
If this hello-world Docker image runs without any errors, then congratulations. You have successfully installed Docker on your Ubuntu Linux machine.
Installing the Docker desktop
If you are a Linux desktop user and want to install Docker for development purposes, Docker desktop should be the preferred candidate over Docker engine. Docker Desktop provides a unified experience and a platform-independent way to build, run, and push Docker images.
The Docker desktop in Linux runs inside a virtual machine, which is quite opposite to the shared kernel model of the Docker engine. This is why your CPU must support virtualization technology and Linux KVM for Docker Desktop to work. You can check the virtualization status in your UEFI/BIOS settings. Docker Desktop is supported in major Linux distributions like Ubuntu, Debian, and Fedora.
The need to use a virtual machine for the Docker desktop is manifold. Some of them are described below.
- Using a virtual machine gives Docker consistent performance and experiences across all platforms (Windows, Linux, macOS, Raspberry Pi, etc.).
- Since users stick to the long-term support version of their distro, they don’t get the latest kernel features. Using a virtual machine solves this problem. Now Docker can select its kernel version and can work with the latest Linux kernel.
- Using a virtual machine also provides security to the user. As we know, anyone can upload images to Docker hub. Sometimes images can contain malicious code that can exploit your desktop. If you extract such malicious images in your machine then those images can destroy your machine.
- As the Docker desktop runs inside a virtual machine, malicious images cannot affect the host machine. This makes the Docker desktop an irresistible candidate for trying out new Docker images.
First make sure your system supports KVM, qemu, systemd-init and application flag. You must have at least 4 GB of RAM on your system.
- Uninstall any previous version of Docker-desktop installed on your system
sudo apt remove docker-desktop
- For a full cleanup and purge of all configuration files, run the following command
rm -r $HOME/.docker/desktop sudo rm /usr/local/bin/com.docker.cli sudo apt purge docker-desktop
- Configure the Docker package repository by running the following commands.
sudo apt update sudo apt install ca-certificates curl gnupg lsb-release sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Now go to this release page and download the latest
- Install it
.debpackage using dpkg.
sudo apt update sudo apt install ./docker-desktop-<version>-<arch>.deb
To launch the Docker desktop, find the application menu and launch the application like a normal application. There you will find a welcome guide. Follow the guide to learn more about the Docker desktop for Linux and how to use it. You can also create containers and images using the Docker desktop app.
Services we need for WordPress
We mainly need 3 components to run WordPress in Docker containers. First, we need WordPress itself, then we need a MySQL database, then we need storage space or volume.
WordPress stores all of its user-generated data in a MySQL database. Therefore, the MySQL database plays a crucial role in our WordPress installation.
Storage or volume is needed when you want to install new themes or plugins. These theme or plugin files will be stored in the volume space.
Using a Docker-Compose file
As our application needs more than one service, it is better to use Docker-compose. Docker compose is a Docker tool that initiates and manages Docker containers and establishes a relationship between them.
- Create a new docker-compose.yml file.
sudo nano docker-compose.yml
- From the previous section, we learned that we mainly need 3 services. WordPress itself, a MySQL database, and volume space. Therefore, our docker-compose file will look like this. You can copy this docker-compose.yml file if you don’t need any further customization.
version: '3.3' services: wordpress: depends_on: - db image: wordpress:latest volumes: - wordpress_files:/var/www/html ports: - "80:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: my_wordpress_db_password db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: my_db_root_password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: my_wordpress_db_password volumes: wordpress_files: db_data:
In the docker-compose file above, we first define WordPress and DB (using MySQL database) as a service. Next, WordPress depends on DB to save its data. We use the latest WordPress image to create a WordPress container. The WordPress container reserves port 80. This is the default port for web services. Therefore, when you type your http://domain-name.com URL in your browser, this WordPress container serves you with a response.
In the database service, we use the MySQL version 5.7 image to create our container. Next, we give all the credentials needed to talk with our WordPress container.
Finally, we define our volumes. There, all WordPress files and database data will be stored. If you are running this setup inside a server, you should regularly back up this volume.
Run WordPress in Docker
Now create a folder or directory and move this docker-compose.yml file into the directory. Then run this command.
After this command, Docker will extract all the images we describe in our docker-compose.yml file and make it a container. The first run can take a long time because Docker has to download all files from DockerHub. Once things are set up, when you load your server’s IP address into the browser, you should see the WordPress installation screen.
Congratulations! You have successfully run WordPress in a Docker container. Note that this doesn’t mean it’s ready for production use yet – you still need to configure an SSL certificate.
If you want to learn more about Docker, you can follow this beginner-friendly article.
Frequently Asked Questions
Can I run Docker-desktop concurrently with Docker Engine?
Yes, you can run Docker-desktop engine and Docker simultaneously on your machine. But when both services need to access the same system resources, such as network ports or volumes, it creates a conflict between them. It is therefore advisable to stop a service before starting one.
Docker desktops can be shut down easily using a GUI (graphical user interface) application. To stop the Docker engine, run the following command.
sudo systemctl stop docker docker.socket containerd
sudo systemctl disable docker docker.socket containerd
How to start WordPress site automatically after reboot?
If you have shut down your server for maintenance or to update some things, you can configure it to automatically start WordPress after every reboot using Docker. Just add
restart: always setting under each service in your docker-compose. You can see this parameter that we added in the docker-compose.yml file.
Can I use Nginx with Docker-compose and WordPress?
Yes, you can use Nginx or any other type of server to serve WordPress content using Docker. Nginx can also be used as a reverse proxy. You can distribute your traffic easily by installing Nginx with Docker and WordPress.
Was this article helpful?
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox