DEV Community

Muhammad Urwah
Muhammad Urwah

Posted on

WordPress with Docker Desktop and Docker Compose

In this guide, you will learn how to set up and use WordPress with Docker Desktop using Docker Compose. Using this method will make you stop using XAMPP, LAMP or WAMP.

Prerequisites

Before proceeding, make sure you have the following prerequisites installed on your system:

  • Docker Desktop: Download and install Docker Desktop from the official Docker website for your operating system.

Step 1: Create a Docker Compose file

  1. Open your favorite text editor such as VS Code.
  2. Create a new file named docker-compose.yaml in the directory of your choice.
  3. Copy and paste the following content into the docker-compose.yaml file and then make sure to save the file:
version: '3.8'
services:
  db:
    # We use a mariadb image which supports both amd64 & arm64 architecture
    image: mariadb:10.6.4-focal
    # If you really want to use MySQL, uncomment the following line
    # image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  wordpress:
    image: wordpress:latest
    ports:
      - 80:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

Step 2: Start the WordPress container

  1. Open Docker Desktop on your system.
  2. In the Docker Desktop interface, navigate to the folder where you saved the docker-compose.yaml file.
  3. Click on the "Compose" button in the Docker Desktop interface.
  4. Select the docker-compose.yaml file you created.
  5. Click on the "Run" button to start the WordPress container.

Docker Desktop will pull the required images and start the containers.

Step 3: Access WordPress in your browser

  1. Open your web browser.
  2. Enter the following URL in the address bar:

http://localhost

Follow the WordPress installation steps to set up your website and then you're good to go. It will be fully functional.

Working Wordpress

Step 4: Stop and remove the containers

If you want to stop and remove the WordPress containers, follow these steps:

  1. Open Docker Desktop on your system.
  2. In the Docker Desktop interface, navigate to the folder where you saved the docker-compose.yaml file.
  3. Click on the "Compose" button.
  4. Select the docker-compose.yaml file.
  5. Click on the "Stop" button to stop the containers.

Docker Desktop will stop and remove the containers, but it will retain the WordPress database and its data.

Congratulations! You have successfully set up WordPress using Docker Desktop and Docker Compose. You can now manage and customize your WordPress website within the Docker environment.

Remember to regularly backup your WordPress data to avoid data loss, and refer to the Docker documentation for more advanced configuration options and deployment scenarios.

The Docker Compose code is taken from the docker/awesome-compose github repository which contains many different templates for docker-compose.

Top comments (0)