Docker simple WordPress site

Recently I have used Docker to run WordPress on my new VPS, it was a very interesting journey that I want to share with you today.

What is Docker?

Docker is an engine that help you to build a containers think in this containers as a package of applications.

Why I have used Docker?

I wanted to run my WordPress site on a VPS so there are some options here, on of them to install the database, PHP and Apache / Nginx server to run my WordPress site.

But I preferred to user docker to know more about it.

First of all you will need to install Docker engine on your VPS, you can follow these steps here from their official site based on your VPS os.

After installing docker, create any directory on your VPS, I will assume you are using Ubuntu, you can create your director as follow.

mkdir ~/wordpress && cd ~/wordpress

After that you will need to create a file called docker-compose.yml into your WordPress directory you have created in the previous step.

cd ../wordpress
touch docker-compose.yml
vim docker-compose.yml

you can use whatever editor to edit above docker-compose.yml file in the previous step I have used vim editor.

Past the content of the docker-compose.yml file as follow.

version: '3'

services:
  db:
    image: mysql:8.0.31
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

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

volumes:
  db_data:

This file defines two Docker services: a MySQL database and a WordPress web server.

The MySQL database in this example is created using the official MySQL 8.0.31 Docker image, although you can use whatever version you need

The database is configured with a root password of “example” and a WordPress database with a username and password of “wordpress”.

The WordPress service is created using the official WordPress Docker image and is configured to use the MySQL database created earlier.

It also maps the wp-content directory in the current directory to the wp-content directory in the WordPress container and exposes the container’s port 80 as port 8000 on the host machine.

Then you can simple run docker-compose up -d

This will start both the MySQL database and WordPress containers in the background. You should now be able to access your WordPress site by visiting http://localhost:8000 in your web browser.

That’s it! You now have a simple WordPress site running in Docker. If you ever need to stop the containers, you can run docker-compose down in the same directory.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top