Install WordPress container on Docker

server docker wordpress
4 minutes, 46 seconds

In my previous post, I set up my new MiniPC with Debian and Docker, ready to host new containers. My initial idea for getting into this was to have a go at self-hosted servers, and my first test has to be with this website. I need to migrate this website, which is currently hosted on an external hosting service and managed using WordPress, to my brand-new mini server and get it up and running on Docker. Let’s get started.

Find the correct Docker image

I’m not sure if I explained this in the previous article, where I installed Docker on Debian, but the way a container works is very simple: essentially, all the elements required for the specific programme to run, such as libraries or the operating system, are packaged into an image. This packaged image runs in a completely isolated environment without interacting with other containers, utilising the resources made available by Docker, such as the processor, RAM or the necessary hard disk space.

Generally, the creators of these programmes themselves, such as WordPress, have their own Docker images and make them available to us, but any user with a certain level of knowledge can also create a Docker image or modify an existing one to share it with others. For this purpose, there is a website that brings together a large number of Docker images and makes them available to us: Docker Hub.

However, Docker Hub can be a bit of a nightmare when it comes to installing these images: you select the image you want to install, read the description, go to the GitHub repository, read the instructions again… In this post, we’ll try to make it a bit easier and I’ll provide the steps you need to follow to get your WordPress environment ready to run.

Connect to the server via SSH (or locally if you have a monitor and keyboard) and let’s get started:

Instruccions

1. Create the directory for the container.

Remember that in the initial post on installing Docker, we created a folder called “docker” to store the compose files (container configuration files) and the folders for each container, so let’s navigate to that folder and create the subfolders we need to get WordPress up and running.

cd docker
mkdir wordpress
cd wordpress
mkdir mysql && mkdir wp-data
cd mysql
mkdir wp
cd ..

Following these instructions, we have created the “wordpress” folder, which will hold our website’s configuration and files within the “wp-data” subfolder. We have also created the “mysql” folder and, within it, the “wp” subfolder, where the WordPress database will be stored.

2. Create the docker-compose file.

This is the file containing the instructions for initially installing and configuring the WordPress container and its database.

sudo nano docker-compose.yml

In the compose file, copy and paste the following settings:

services:
  db:
    image: jc21/mariadb-aria:latest
    container_name: wordpress-db
    restart: unless-stopped
    networks:
      - wp
    environment:
      MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
      MYSQL_DATABASE: $MYSQL_DATABASE
      MYSQL_USER: $MYSQL_USER
      MYSQL_PASSWORD: $MYSQL_PASSWORD
      MYSQL_PORT: $MYSQL_PORT
    volumes:
      - ~/docker/mysql/wp:/var/lib/mysql/wp

  wordpress:
    image: wordpress:latest
    container_name: wordpress
    restart: unless-stopped
    networks:
      - wp
    ports:
      - "8080:80"                                   # Cambia 8080 si ya usas ese puerto
    depends_on:
      - db
    environment:
      WORDPRESS_DB_HOST: $MYSQL_HOST
      WORDPRESS_DB_USER: $MYSQL_USER
      WORDPRESS_DB_PASSWORD: $MYSQL_PASSWORD
      WORDPRESS_DB_NAME: $MYSQL_DATABASE
    volumes:
      - ~/docker/wordpress/wp-data:/var/www/html

networks:
  wp:
    external: true

volumes:
  wp:
    driver: local
  wp-data:
    driver: local

Under the ‘services’ section, we include the two services that Docker will run: the first is the database (MariaDB, which is lighter) and the second is the CMS itself.

Next, we have the ‘networks’ section, where we specify that the ‘wp’ network is external (external:true). This tells Docker that it does not need to create it every time compose runs, but that it must be created beforehand. Therefore, in Portainer, we go to the ‘networks’ section and create a new network called ‘wp’, leaving all the options at their default settings.

The final section is ‘volumes’, where we configure the database and WordPress folders as local volumes.

3. Create a hidden variables file.

Each service has a number of settings, such as the image to use (image) and the image version (:latest), the name given to the containers (container_name), the network to which it will connect, and which ports each container will use.

There is an ‘environment’ section where we configure specific characteristics for each container; in this case, we have hidden these using hidden environment variables (the $ symbol indicates that this reference must be looked up in an external file).

sudo nano .env

With this command, we create the hidden variables file and specify the value of each variable within it. Include the following in the hidden .env file.

MYSQL_ROOT_PASSWORD=ROOT_PASSWORD    # Replace ROOT_PASSWORD with your MariaDB root password
MYSQL_DATABASE=wordpress             # Database name
MYSQL_USER=wordpress                 # Database user
MYSQL_PASSWORD=wordpress             # Database password
MYSQL_HOST=db                        # Service name
MYSQL_PORT=3306                      # Database port

Along the line, the text following the # symbol explains what each environment variable is. In Docker, everything to the right of the # symbol is read-only.

Execution and access.

Now all that remains is to run the compose file and start the containers.

docker compose up -d

Once the containers have started up, you can check their status in Portainer. Once that’s done, all that remains is to access the WordPress web portal, create an administrator account and install the CMS to get the website up and running.

http://server_ip:8080

Having followed these steps, I now have WordPress up and running on my MiniServer; all that remains is to create a backup of the hosting and import it into the local container. Next, I need to set up the local server with a proxy to avoid any nasty surprises, but we’ll look at that in the next article.

This was written without the use of Artificial Intelligence. Thank you for reading the post.