How to install WordPress on Ubuntu using Docker

How to install WordPress on Ubuntu using Docker

Installing WordPress on an Ubuntu virtual private server (VPS) commonly requires LAMP (Linux, Apache, MySQL, PHP). However, you can also set up the platform using Docker, a containerization tool that packages applications and their dependencies together.

Using Docker, WordPress installation becomes simpler, as it runs from pre-built images containing all necessary components. This approach offers several benefits: it’s modular, easy to update and scale, and quick to redeploy should you need a new instance.

In this article, we’ll explain how to install WordPress on Ubuntu using Docker. You’ll learn each step, from preparing prerequisites to managing WordPress as a containerized content management system (CMS).

Download free docker cheat sheet

Prerequisites for installing WordPress with Docker

Before installing WordPress on Docker, make sure you have a VPS running a recent version of Ubuntu, such as 22.04 or later, to avoid compatibility issues.

Your server should also offer full root access to prevent permission errors when modifying system files. Additionally, point a domain name to your VPS so your WordPress website will be accessible to the public.

If you don’t have a VPS, we suggest getting a Hostinger Docker VPS plan. Since Docker and WordPress are relatively lightweight, our KVM 1 plan – with a single-core CPU, 4 GB of RAM, and 50 GB of storage – should be sufficient. You can upgrade to a higher-tier plan if you need more resources as your site grows.

With Hostinger, you can connect to your VPS as root directly from your web browser, making the installation process more efficient.

If you encounter difficulties during setup, our Kodee AI assistant is available to guide you through managing your WordPress instance. Simply type your questions or describe your needs, and Kodee will write a step-by-step guide for you.

How to install WordPress with Docker Compose

Follow these steps to install WordPress using Docker. Before proceeding, access your VPS command-line interface (CLI) using the PuTTY SSH client or terminal. Alternatively, use Hostinger Browser terminal if you need a simpler solution.

1. Install Docker and Docker Compose

Hostinger users can install Docker and Docker Compose in a few clicks without commands using the preconfigured VPS template. This method is simpler and suitable for beginners.

Warning! Installing a new VPS template will wipe your data. Back up important files before proceeding.

To do so, simply open your VPS management page on hPanel and navigate to OS & PanelOperating System. Select Ubuntu 22.04 with Docker and click Change OS.

If you want more flexibility when configuring the tool, install Docker and Docker Compose using commands.

Now, verify if Docker and Docker Compose are configured correctly using the following commands:

docker --version
docker-compose --version

If these tools work properly, your CLI will output their version number:

2. Set up your project directory

Create a new project directory to house all your Docker and WordPress files using this command:

mkdir wordpress

By default, the command will create the wordpress folder in your current user’s home directory. You can create it in another location or use a different name.

Then, open the folder using the command below:

cd wordpress

Once inside, you can start setting up Docker and WordPress.

3. Create a Docker Compose file

The Docker Compose file provides information about software components to install in your container. In this case, it specifies the configuration for WordPress and the database application.

To create a Docker Compose file, run the following command:

sudo nano docker-compose.yml

Paste the following configuration. Replace the username, password, and database name with your desired value.

version: "3" 
services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MySQLRootPassword
      MYSQL_DATABASE: MySQLDatabaseName
      MYSQL_USER: MySQLUsername
      MYSQL_PASSWORD: MySQLUserPassword

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    ports:
      - "80:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: MySQLUsername
      WORDPRESS_DB_PASSWORD: MySQLUserPassword
      WORDPRESS_DB_NAME: MySQLDatabaseName
    volumes:
      - "./:/var/www/html"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      PMA_USER: MySQLUsername
      PMA_PASSWORD: MySQLUserPassword

volumes:
  mysql: {}

Save the changes and return to the main shell by pressing Ctrl + XYEnter.

4. Start the Docker container

Run Docker Compose using this command to install and launch WordPress in a container. Remember that it only works when you are inside the project directory:

docker-compose up -d

Docker Compose will pull images from Docker Hub as specified in your YAML file, configure the container, and set up the network connection. Depending on your internet speed, this setup process might take a few minutes.

Once finished, you should see the Done status and return to the main shell.

5. Access the WordPress installation

With your Docker container up and running, you can now access the new WordPress installation through your web browser. Since your domain name is already pointed to the VPS, simply enter it in the browser’s address bar.

This will take you to the WordPress setup wizard. Choose your preferred language and click Continue.

Next, fill in the necessary information about your site, including the site title, username, password, and email address. Once complete, hit Install WordPress.

After that, click Log In to enter the WordPress dashboard. Use the credentials you just created to access your admin panel.

6. Customize your Docker configuration

For production environments, you’ll need additional configurations so that your WordPress site is secure and optimized for performance. Below are key customizations to deploy WordPress using Docker:

Using environment files

To protect sensitive data, avoid hardcoding credentials like database usernames and passwords in your Docker Compose file. Instead, store these values in an environment file (.env).

In your wordpress directory, create a .env file:

nano .env

Define your WordPress database credentials in the .env file, replacing placeholders with your own values:

MYSQL_ROOT_PASSWORD=MySQLRootPassword
MYSQL_DATABASE=MySQLDatabaseName
MYSQL_USER=MySQLUsername
MYSQL_PASSWORD=MySQLUserPassword

Save the file, then update the docker-compose.yml file to use these environment variables:

db:
  image: mysql:latest
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    MYSQL_DATABASE: ${MYSQL_DATABASE}
    MYSQL_USER: ${MYSQL_USER}
    MYSQL_PASSWORD: ${MYSQL_PASSWORD}

Save the changes and restart Docker Compose to apply the new configurations:

docker-compose up -d

Obtaining SSL certificates

An SSL certificate is important for protecting user data and improving search engine rankings. To enable automatic SSL certificates, add an NGINX proxy with Let’s Encrypt in your docker-compose.yml file:

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  letsencrypt-nginx-proxy-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /etc/nginx/certs
      - /etc/nginx/vhost.d
      - /usr/share/nginx/html

Next, configure the wordpress service to include SSL labels. Replace your_email and your_domain.com with your actual credentials.

wordpress:
  image: wordpress:latest
  labels:
    - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
    - "VIRTUAL_HOST=your_domain.com"
    - "LETSENCRYPT_HOST=your_domain.com"
    - "LETSENCRYPT_EMAIL=your_email@your_domain.com"

Here’s the final docker-compose.yml content after using environment variables and adding SSL support with NGINX:

version: "3"
services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    ports:
      - "80:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
    volumes:
      - "./:/var/www/html"
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
      - "VIRTUAL_HOST=your_domain.com"
      - "LETSENCRYPT_HOST=your_domain.com"
      - "LETSENCRYPT_EMAIL=your_email@example.com"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      PMA_USER: ${MYSQL_USER}
      PMA_PASSWORD: ${MYSQL_PASSWORD}

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  letsencrypt-nginx-proxy-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /etc/nginx/certs
      - /etc/nginx/vhost.d
      - /usr/share/nginx/html

volumes:
  mysql: {}

Don’t forget to save these changes and restart Docker Compose afterward:

docker-compose up -d

7. Manage and scale WordPress with Docker

After running WordPress in Docker for a while, scale your setup to optimize its performance and regularly back up your files to maintain data integrity. Here’s how:

Scaling WordPress containers

As your site traffic grows, consider scaling your WordPress service to run multiple containers. It lets you evenly distribute user requests across different containers for optimal performance and uptime.

In your Docker Compose file, specify the desired number of replicas, such as 3, to add more instances of your WordPress container:

 wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    ports:
      - "80:80"
  environment:
    WORDPRESS_DB_HOST: db:3306
    WORDPRESS_DB_USER: ${MYSQL_USER}
    WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
    WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
  volumes:
    - "./:/var/www/html"
  deploy:
    replicas: 3

Apply your changes by running:

docker-compose up -d --scale wordpress=3

Docker will now start additional WordPress containers and distribute traffic evenly between them.

Backing up WordPress files

To protect your WordPress site against data loss or corruption, you should back up both WordPress files and the database.

Since Docker mounts the WordPress directory as a volume, you can back up this directory using a simple command like this:

cp -r /path/to/wordpress /path/to/backup/location

To back up your MySQL database, run the following command. Replace [db_container_name], [MYSQL_USER], [MYSQL_PASSWORD], and [MYSQL_DATABASE] with your actual database container name and credentials:

docker exec [db_container_name] /usr/bin/mysqldump -u [MYSQL_USER] -p[MYSQL_PASSWORD] [MYSQL_DATABASE] > backup.sql

This command creates a database dump, which you can restore later if needed.

Alternatively, Hostinger VPS customers can use our built-in backup tools to easily back up and restore data. To access these features, go to Backup & Monitoring → Snapshots & Backups in your VPS dashboard’s left sidebar.

Then, choose to either create a snapshot to capture your current setup or restore previous data from an automated weekly backup.

Conclusion

In this article, you’ve learned how to install and configure WordPress using Docker on an Ubuntu VPS. From setting up Docker Compose to customizing for production environments, you now have a fully functional WordPress setup in a containerized environment.

After running WordPress using Docker, consider optimizing your site further, such as by using a content delivery network (CDN). You can also set up different Docker containers if you decide to host other applications on the same VPS.

If you have any questions or would like to share your experience developing WordPress websites with Docker, feel free to use the comment box below.

WordPress Docker FAQ

What are the benefits of using Docker for WordPress?

With Docker, you can deploy and scale your WordPress environment as needed. Docker isolates dependencies, making it easier to update, back up, and migrate your WordPress site efficiently.

Can I run multiple WordPress sites on a single server with Docker?

Yes, Docker lets you run multiple WordPress websites on a single server by using separate containers for each site. Set up each container with unique ports, volumes, and database connections for isolation.

How do I configure a WordPress Docker container?

To configure a WordPress Docker container, use a Docker Compose file to define services, ports, and environment variables. For sensitive data, such as database credentials, we suggest storing it in a .env file linked to the Compose file.

Author
The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.

Author
The Co-author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.