Categories
DevOps

How to use docker-compose to setup a WordPress instance

Use docker-compose to instantly setup a development WordPress instance.

I will use a local http://127.0.0.127:8080 address, admin user with 9YyO0T9sPqGplvL0 password.

Create plugins directory to locally edit plugins.

$ mkdir plugins

Inspect docker-compose configuration.

$ cat docker-compose.yml
version: '2'

services:
  wordpress:
    image: wordpress
    environment:
      - WORDPRESS_DB_HOST=database
      - WORDPRESS_DB_NAME=wordpress
      - WORDPRESS_DB_PASSWORD=C8aouSKnyPUV0l0u
      - WORDPRESS_DB_USER=wordpress
    ports:
      - "127.0.0.127:8080:80"
    volumes:
      - ./plugins:/var/www/html/wp-content/plugins
      - wordpress:/var/www/html
    depends_on:
      - database
  database:
    image: mariadb
    environment:
      - MYSQL_ROOT_PASSWORD=ZqJNzcQNuahnCY1n
      - MYSQL_DATABASE=wordpress
      - MYSQL_PASSWORD=C8aouSKnyPUV0l0u
      - MYSQL_USER=wordpress
  cli:
    image: wordpress:cli
    user: "33:33"
    depends_on:
      - database
      - wordpress
    environment:
      - WORDPRESS_DB_HOST=database
      - WORDPRESS_DB_NAME=wordpress
      - WORDPRESS_DB_PASSWORD=C8aouSKnyPUV0l0u
      - WORDPRESS_DB_USER=wordpress
    command: >
      bash -c "/bin/sleep 30 && 
               /usr/local/bin/wp core install --path=/var/www/html \
                                              --url=http://127.0.0.127:8080 \
                                              --title=\"WordPress Development\" \
                                              --admin_user=admin \
                                              --admin_password=9YyO0T9sPqGplvL0 \
                                              --admin_email=admin@example.com"
    volumes:
      - wordpress:/var/www/html
volumes:
  wordpress:

Create containers.

$ docker-compose up -d
Creating volume "wordpress_wordpress" with default driver
Creating wordpress_database_1 ... done
Creating wordpress_wordpress_1 ... done
Creating wordpress_cli_1       ... done

Inspect WordPress setup process.

$ docker-compose logs cli  
Attaching to wordpress_cli_1
cli_1        | sendmail: can't connect to remote host (127.0.0.1): Connection refused
cli_1        | Success: WordPress installed successfully.

WordPress setup will be executed only once, do not think about it.

WordPress is already installed.

Visit the WordPress site and play with it.

Stop and clean up environment.

$ docker-compose stop
Stopping wordpress_wordpress_1 ... done
Stopping wordpress_database_1  ... done
$ docker-compose rm -f  
Going to remove wordpress_cli_run_84e3dcdb2a72, wordpress_cli_1, wordpress_wordpress_1, wordpress_database_1
Removing wordpress_cli_run_84e3dcdb2a72 ... done
Removing wordpress_cli_1                ... done
Removing wordpress_wordpress_1          ... done
Removing wordpress_database_1           ... done
$ docker volume rm -f wordpress_wordpress
wordpress_wordpress

Nice. I like it.