Wordpress Docker (docker-compose.yml) guided tutorial

November 15, 2020

If you need to use Wordpress locally, but do not want to go to the effort of setting up a web server and MySQL then you can easily set it all up using Docker (with docker-compose).

If you have your WordPress files in /yoursite/ (so you have /yoursite/wp-content/themes/your-theme/ and /yoursite/wp-content/plugins/some-plugins) you can use the following docker-compose.yml.

(You do not need any WordPress files outside of /wp-content for this to work)

version: '3.1'
# Source: https://webdevetc.com/programming-tricks/wordpress/tips/wordpress-docker-docker-composeyml-guided-tutorial/

services:
  db: # mysql
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql # use the 'db' volume for `/var/lib/mysql`

  wordpress: # wordpress / php / apache docker config
    image: wordpress:5.5.3-php7.4-apache
    depends_on: db
    restart: always
    ports:
      - 8080:80 # access it on localhost:80
    environment: # database credentials:
      WORDPRESS_DB_HOST: db # points to the 'db' service (below)
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - ./wp-content:/var/www/html/wp-content # << maps your local ./wp-content to the wp-content directory on the server

volumes:
  db:  # for mysql

Once you have the above config saved as docker-compose.yml and you have Docker installed, just run docker-compose up to start everything.

Then visit http://localhost:8080 and you should see the WordPress installation.

Once done you can run docker-compose down.

Note: This docker config is ideal if you need to make changes to a theme or plugin. It is not ideal if you need to import a database dump. If you need to edit files outside of /wp-content then you can add more volumes configuration to the wordpress service.