How to Set Up WordPress Using Docker in Ubuntu

Docker Wordpress Installation

WordPress is the world’s most popular content management system (CMS). More than 40% of the web runs on WordPress. Docker is a containerizing system used to run your software in an isolated environment. The concept of containerization is not new in the software development world. But Docker makes it very developer friendly.

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.

Also read: How to Copy/Move a Docker Container to Another Host

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.

sudo apt autoremove docker docker-engine docker.io containerd runc

Now let’s start our installation:

  1. 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
  1. Add official GPG key of Docker.
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Set up 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
  1. 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
  1. Verify the Docker installation by running a hello world image.
sudo docker run hello-world

If this hello-world Docker image runs without any error, then congratulations. You have successfully installed Docker on your Ubuntu Linux machine.

Also read: How to Install Docker in Linux

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

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.

  1. Using a Virtual machine gives docker desktop consistent performance and experiences across all platforms (Windows, Linux. macOS, Raspberry Pi, etc).
  2. As users stick to the long-term support release version of their distribution, they don’t get the benefits of the latest kernel features. Using a VM solves this issue. Now Docker can select its Kernel version and can work with the latest Linux kernel.
  3. Using a virtual machine also provide security to the user. As we know anyone can upload images to the Docker hub. Sometimes images may contain some malicious code that can exploit your desktop. If you pull such malicious images to your machine, then these images can destroy your machine.
  4. As Docker desktop work inside a virtual machine, malicious images can’t affect the host machine. This makes the Docker desktop an irresistible candidate to try out new Docker images.

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.

  1. Uninstall any previous version of Docker-desktop installed in your system
sudo apt remove docker-desktop
  1. For a complete cleanup and purge of all the config files run the following command
rm -r $HOME/.docker/desktop
sudo rm /usr/local/bin/com.docker.cli
sudo apt purge docker-desktop
  1. Setup 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
  1. Now go to this release page and download the latest .deb package of Docker-desktop.
  2. Install the .deb package using dpkg.
sudo apt update
sudo apt install ./docker-desktop-<version>-<arch>.deb

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.

  1. Create a new docker-compose.yml file.
sudo nano docker-compose.yml
  1. From the previous section, we have learned that we need mainly 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 other 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 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.

Also read: How to Set Up WordPress for Local Development in Linux

Running WordPress Inside Docker

Now create a folder or directory and move this docker-compose.yml file inside the directory. Then run this command.

docker-compose up -d

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.

Also read: 6 of the Best WordPress Hosting Services for Your WordPress Site

Frequently Asked Questions

Can I run Docker-desktop simultaneously with the Docker engine?

Yes, you can run the Docker-desktop and Docker engine simultaneously on your machine. But when both services need to access the same system resources like network ports or volumes, then it creates a conflict between them. Therefore it is advisable to stop one service before starting one.

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 containerd
sudo systemctl disable docker docker.socket containerd

How to auto-start WordPress site after reboot?

If you stopped your server for maintenance or to update some stuff, you can configure it to autostart WordPress after every reboot using Docker. Just add restart: always parameter under every service of your docker-compose file. You can see this parameter we have added on 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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Hrishikesh Pathak

Developer and writer. Write about linux and web.