How to deploy WordPress with Docker Compose

In this short post, I just want to share how to run a WordPress installation in your local environment. Let’s suppose that you are creating a new website from scratch, or you need to work on an existing one.

First, let’s configure your containers to run a WordPress with MariaDB for the database and phpMyAdmin to manage your database and tables from a web browser.

  • Requirements:
    • Docker

Create a file named ‘docker-compose.yml‘ in the root folder of your WordPress instance. Copy and paste the following code to the file:

version: "3"

services:
  # Database
  db:
    image: mariadb:latest
    container_name: udooku_wordpress_db
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: rootpass
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    container_name: udooku_wordpress_phpmyadmin
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: rootpass
    networks:
      - wpsite
  # WordPress
  wordpress:
    container_name: udooku_wordpress_wordpress
    depends_on:
      - db
    # build: .
    image: wordpress:latest
    ports:
      - "80:80"
    restart: always
    # uncomment line if you want to use the entire wordpress local folder instead
    # of having to download a wordpress image for the core files
    # volumes: ["./:/var/www/html"]
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./wp-config.php:/var/www/html/wp-config.php
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: rootpass
    networks:
      - wpsite
networks:
  wpsite:
    driver: bridge
volumes:
  db_data:


GitHub link: https://github.com/mad4n7/docker-containers/blob/main/wordpress/docker-compose.yml

Start the container, with the following command:

docker compose up

Let’s create a database named ‘wordpress‘. In your browser, open http://localhost:8080

You will see a page similar to the image above. For the username, type ‘root,’ and for the password, type ‘rootpass‘.

Now create a database. Click on ‘New‘, type a database name and click on ‘Create‘.
Example:

Optional step:

If you need to work with an existing WordPress database, you’ll have to create a dump of your database file and import it into the container. While you can use phpMyAdmin for this task, if you have a large database, let’s have some fun using the terminal instead. First, you’ll need to find the container ID for MariaDB.

docker container list

Find and copy the container id where it’s written ‘mariadb:latest‘.

In your terminal, navigate to the folder where you have your WordPress database dump and execute the following command to import it into your MariaDB Docker volume:

docker exec -i CONTAINER_ID_HERE mariadb -uroot -prootpass wordpress < DUMP_FILE_NAME_HERE.sql

After the import is completed, open phpMyAdmin (http://localhost:8080), navigate to the ‘wordpress’ database, browse the ‘wp_options’ table, and replace the values for ‘siteurl’ and ‘home’ with ‘http://localhost’.

That’s it. Now open http://localhost in your browser.

I hope you find this article helpful. 😁