WPSurfer.com

How to Install WordPress Locally using Docker

Published on November 10, 2024 | Updated on November 11, 2024

I had been using Local by Flywheel and that was my favorite and only method to create WordPress sites locally.

I had installed Docker Desktop but I had never taken the time to play with it.

The other day I learned about creating sites using a YAML file and a couple of commands and I loved how easy creating a WordPress website can be with Docker.

In this post, you will find information about how to create WordPress sites locally using Docker



Install Docker Desktop

The first thing that you have to do is install Docker Desktop

  1. If you haven’t installed Docker Desktop yet, go to the Docker website and download the version for your operating system.
  2. Follow the installation instructions
  3. Verify the Docker Installation by opening the terminal and run the following command.
docker --version

Create a Folder

Now that you know that Docker is running on your computer, you have to create a folder.

If you plan to create several websites, create a folder and name it something like WordPress_Websites and create a folder for each one of them in it.

I have my folder on my Desktop.

Create an empty YAML file.

The name of the YAML file must be docker-compose.yml


YAML File

This is the YAML File I am using to create my WordPress sites

You will also be able to use WP-CLI

services:
  wps_wordpress:  # Unique WordPress service name for 'wps' site
    image: wordpress:latest
    container_name: wps_wordpress_site  # Unique container name
    restart: always
    ports:
      - "8080:80"  # Access WordPress via https://wpsurfer.com
    environment:
      WORDPRESS_DB_HOST: wps_db:3306  # Unique database host reference
      WORDPRESS_DB_NAME: wps_wordpress_db
      WORDPRESS_DB_USER: wps_user
      WORDPRESS_DB_PASSWORD: wps_password
    volumes:
      - ./wps_wordpress_data:/var/www/html  # Unique volume for persistence
    networks:
      - wps_net
    entrypoint: >
      sh -c "
        apt-get update && 
        apt-get install -y curl && 
        curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && 
        chmod +x wp-cli.phar && 
        mv wp-cli.phar /usr/local/bin/wp && 
        docker-entrypoint.sh apache2-foreground
      "
  wps_db:  # Unique MariaDB service for 'wps' site
    image: mariadb:latest
    container_name: wps_mariadb_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wps_root_password
      MYSQL_DATABASE: wps_wordpress_db
      MYSQL_USER: wps_user
      MYSQL_PASSWORD: wps_password
    volumes:
      - ./wps_db_data:/var/lib/mysql  # Unique volume for persistence
    networks:
      - wps_net
  wps_adminer:  # Unique Adminer service for managing the database
    image: adminer:latest
    container_name: wps_adminer
    restart: always
    ports:
      - "8081:8080"  # Access Adminer via https://localhost:8081
    networks:
      - wps_net
volumes:
  wps_wordpress_data:
    driver: local
  wps_db_data:
    driver: local
networks:
  wps_net:
    driver: bridge

if you plan to create several sites, use unique names for the containers and different ports to avoid conflicts

Each folder must contain a different YAML file in them


Install WordPress

Figure out what the location of the folder with the YAML file is.

Open the terminal and use it to go to the location of the file.

Since I have my folder on my desktop. This is the command I run

cd ~/Desktop/docker/wps

and then run this command:

docker compose up -d

Now go to the port you specified and add the information you need to finish the creation of your WordPress site.

If you are not creating a new site from scratch, just add any information since these will be replaced when you migrate a site to it.


Test If WP-CLI is Working

In case, you might want to check if WP-CLI is working, you can run this simple command

docker exec -u www-data -it wps_wordpress_site wp plugin list

Other Docker Commands

In case you want to see what services are running

docker ps 

In case you wanna see a list of running and stopped services

docker ps -a

If you name your stack in a sort of unique way, you can easily stop them or start them using these commands

docker stop $(docker ps -q --filter "name=wps")
docker start $(docker ps -aq --filter "name=wps")

Disclaimer

This was my experience playing with Docker.

Before November 10, 2024, I didn’t know what a YAML file was and how to use to create WordPress websites.

I use that file without any concern since these are Local sites. I use WordPress locally and then I upload a static version of my sites to Digital Ocean.

I really don’t have any security concerns since my sites are 100% static.

CloudPanel: Hosting Control Panel Review

CloudPanel: Hosting Control Panel Review

This post is the result of testing CloudPanel on Digital Ocean and consider as a the future for hosting my WordPress sites

I always make backups of the database or the installation in case I break something or something breaks.

If I wanted to use Docker on a server, I would have to be more careful.


Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.