Setting up Local WordPress Development using - Docker Compose

We will use a tool called Docker Compose that enables running multi-container docker applications. You will use a YAML file to configure your application's services.

Using compose consists of 3 steps:

  1. Ceate a docker-compose.yml file that defines all services that make up your app and provides an isolated environment where they can run together
  2. Run docker-compose up to starting and running your app.

Basic config example

The first definition in the yaml file below is for the web service.

build: build it from the Dockerfile in the parent directory

links: link to the database container

ports: map the external port 8000 to the internal port 80

volumes: map the parent directory on the host to /app in the container, with read and write access. map the data directory on the host to /data in the container, with read and write access

command: by default, when the command docker-compose run is issued, execute python manage.py runserver 0.0.0.0:80

env_file: use the .env-local to supply environment variables to the container

In [ ]:
# docker-compose.yml
web:
 build: .
 links:
  - "db:postgres"
 ports:
  - "8000:80"
 volumes:
  - ".:/app:rw"
  - "./data:/data:rw"
 command: python manage.py runserver 0.0.0.0:80
 env_file: .env-local
db:
 image: postgres:9.4
 volumes:
  - ".:/app:rw"

Commond Commands

Starts existing containers for a service.

docker-compose start

Stops running containers without removing them.

docker-compose stop

Pauses running containers of a service.

docker-compose pause

Unpauses paused containers of a service.

docker-compose unpause

Lists containers.

docker-compose ps

Builds, (re)creates, starts, and attaches to containers for a service.

docker-compose up

Stops containers and removes containers, networks, volumes, and images created by up.

docker-compose down

1. Create a directory(folder) called wordpress in your computer & get into the directory

In [ ]:
mkdir wordpress && cd wordpress

2. Create a docker-compose.yml

You can use the command line interface or any text editor to create the file. Make sure that it is stored in the root directory. The docker-compose.yml file can also be obtained from the docker documentation for wordpress https://docs.docker.com/compose/wordpress/

In [ ]:

  version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "8001:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

3. Build the project

In [ ]:
docker-compose up -d
#  runs docker-compose up in detached mode,
# pulls the needed Docker images, 
# and starts the wordpress and database containers

4. Go to your browser and type http://localhost:8000

You can now proceed with the Wordpress installation.

In case, if you get some error message that wordpress service container can not start the servvice. Follow the instructions below

Right Click on the Docker icon in the Windows Task bar Click Settings >> Resources >> File Sharing >> Click the + button and add the directory where your docker project is.

Capture.PNG

References:

https://docs.docker.com/compose/
https://gist.github.com/jonlabelle/bd667a97666ecda7bbc4f1cc9446d43a
https://docs.divio.com/en/latest/reference/docker-docker-compose/