In this article, we are going to show you how to install WordPress site in a Docker container.

Why Docker?

Why Docker, you may ask? The answer is simplicity. In Docker, your application is containerized within one image. There is no need to do complicated installation and configuration to get it running. Not to mention, it is also faster and easier for you to migrate/upgrade to another server. Say your WordPress becomes very popular and you need to upgrade your existing server? It is easy to do so with Docker, since everything is already containerized. If you develop something using Docker, then after deployment, it works everywhere.

Docker Installation

In this article, we are going to install a Docker engine inside Ubuntu server. Before installing anything, if you have an older version of Docker installed in your system, remove them. Run this command in your terminal to remove all the older versions of Docker. Now let’s start our installation: If this hello-world Docker image runs without any error, then congratulations. You have successfully installed Docker on your Ubuntu Linux machine.

Docker Desktop Installation

If you are a desktop Linux user and want to install Docker for your development purpose, then the Docker desktop should be the preferred candidate compared to the Docker engine. Docker Desktop provides a unified experience and platform agnostic way to build, run and push Docker images. 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 needs to 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 multi-fold. Some of them are described below. First make sure that your system support KVM, qemu, systemd-init, and app indicator. You must have at least 4GB of RAM on your system. To launch the Docker desktop, search on the application menu and launch the application like a normal one. There you can see a welcome guide. Follow the guide to know more about Docker desktop for Linux and how to use it. You can also make containers and images with the help of the Docker desktop application.

Services We Need for WordPress

We need mainly 3 components to run WordPress inside Docker containers. First, we need WordPress itself, then we need a MySQL database and then we need a storage space or volume. WordPress stores all of its user-generated data inside a MySQL database. Therefore the MySQL database takes a crucial role in our WordPress installation. The storage or volume is needed when you want to install any new themes or plugins. These theme or plugin files will be stored inside the volume space.

Using a Docker-Compose File

As our application needs more than one service, it is best to use Docker-compose. Docker compose is Docker tool that initiates and maintains Docker containers and establishes a relationship between them. In the above docker-compose file, we first define WordPress and DB (using MySQL database) as a service. Then WordPress depends on DB to save its data. We use the latest WordPress image to build a WordPress container. The WordPress container reserves port 80. It 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. Inside the database service, we use MySQL version 5.7 image to make our container. Then we give all the necessary credentials to talk with our WordPress container. At last, we define our volumes. There all the WordPress files and database data will be stored. If you are running this setup inside a server, then you have to regularly backup this volume.

Running WordPress Inside Docker

Now create a folder or directory and move this docker-compose.yml file inside the directory. Then run this command. After this command, Docker will pull all the images we describe inside our docker-compose.yml file and make a container from it. The first run can take a long time as docker have to download all the files from DockerHub. After things are set up, when you load your server IP address in the browser, you should see the WordPress installation screen. Congratulations! You have successfully run WordPress in a Docker container. Do note that this doesn’t mean it is ready for production use yet – you still need to setup a SSL certificate. If you want to know more about Docker, you can follow this beginner friendly article. Docker desktops can be stopped easily with the help of a GUI (graphical user interface) app. To stop the Docker engine, run the following command. sudo systemctl stop docker docker.socket containerdsudo systemctl disable docker docker.socket containerd