Native docker-based local environment for WordPress

Use this Docker compose file to spin up local environment for WordPress with a native Docker app on Linux, Mac OS X and Windows.

Docker4WordPress is designed to be used for local development, if you're looking for a dev/staging/production solution check out Wodby. Use Wodby to deploy container-based infrastructure consistent with Docker4WordPress to any server.


Overview

The WordPress bundle consist of the following containers:

Container Service name Image Public Port Enabled by default
Nginx nginx wodby/wordpress-nginx 8000
PHP 7 php wodby/wordpress-php
MariaDB mariadb wodby/wordpress-mariadb
phpMyAdmin pma phpmyadmin/phpmyadmin 8001
Mailhog mailhog mailhog/mailhog 8002
Redis redis redis/redis
Memcached memcached _/memcached
Solr solr _/solr 8003
Varnish varnish wodby/wordpress-varnish 8004

PHP, Nginx, MariaDB and Varnish configs are optimized to be used with WordPress. We regularly update this bundle with performance improvements, bug fixes and newer version of Nginx/PHP/MariaDB.

Instructions

Feel free to adjust volumes and ports in the compose file for your convenience.

1. Install docker for Linux, Mac OS X or Windows. For Mac and Windows make sure you're installing native docker app version 1.12, not docker toolbox.

For Linux additionally install docker compose

2. Download the compose file from this repository and put it to your WordPress project codebase (you might want to add docker-compose.yml to .gitignore).

3. Since containers do not have a permanent storage, directories from the host machine (volumes) should be mounted: one with code of your WordPress project and another with database files.

By default, the directory with the compose file (volume ./) will be mounted to PHP container (assuming it's your codebase directory). Additionally docker-runtime directory will be created to store files for mariadb and, optionally, solr containers. Do not forget to add docker-runtime to your .gitignore file.

Linux only: fix permissions for your files directory with:

$ sudo chgrp -R 82 sites/default/files
$ sudo chmod -R 775 sites/default/files

4. Update database credentials in your wp-config.php file:

DB_NAME: wordpress
DB_USER: wordpress
DB_PASSWORD: wordpress
DB_HOST: mariadb

5. If you want to import your database, uncomment the following line in the compose file:

#      - ./docker-runtime/mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) her

Create the volume directory ./docker-runtime/mariadb-init in the same directory as the compose file and put there your SQL file(s). All SQL files will be automatically imported once MariaDB container has started.

6. If you need to deploy one of the optional containers (Redis, Memcached, Apache Solr) uncomment the corresponding lines in the compose file.

7. Now, let's run the compose file. It will download the images and run the containers:

$ docker-compose up -d

8. Make sure all containers are running by executing:

$ docker-compose ps

9. That's it! You wordpress website should be up and running at http://localhost:8000.

Containers

Accessing containers

You can connect to any container by executing the following command:

$ docker-compose exec php sh

Replace php with the name of your service (e.g. mariadb, nginx, etc).

Nginx

Nginx is being used as a web server. Nginx is pre-configured to be used with WordPress.

PHP

PHP is used with Nginx via PHP-FPM.

Composer

PHP container has installed composer. Example:

$ docker-compose exec --user 82 php composer update

Xdebug

If you want to use xdebug, enable the following option in the compose file:

PHP_XDEBUG_ENABLED: 0 # Set 1 to enable.

MariaDB

Configuring

Many configuration options can be passed as flags without adjusting a cnf file. See example in the compose file:

#    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

Import

Check out the instructions (step 7) to learn how to import your existing database.

Export

Exporting all databases:

docker-compose exec mariadb sh -c 'exec mysqldump --all-databases -uroot -p"root-password"' > databases.sql

Exporting a specific database:

docker-compose exec mariadb sh -c 'exec mysqldump -uroot -p"root-password" my-db' > my-db.sql

Redis

To spin up a container with Redis cache and use it as an object cache storage follow these steps:

  1. Download and install plugin Redis Object Cache
  2. Add the following line to wp-config.php file:
define('WP_REDIS_HOST', 'redis');

Memcached

To spin up a container with Memcached and use it as an object cache storage follow these steps:

  1. Download and install plugin Memcached Object Cache
  2. Add the following line to wp-config.php file:
$memcached_servers = array(
    'default' => array(
        'memcached:11211',
    ),
);

Mailhog

By default, container with mailhog included in the bundle. It will catch all email sent from the PHP container. You can view emails by visiting its admin UI on localhost:8002.

phpMyAdmin

By default, container with phpMyAdmin included in the bundle. You can access it by localhost:8001

Apache Solr

To spin up a container with Apache Solr search engine uncomment lines with solr service definition in the compose file. Use volume directory ./docker-runtime/solr to access configuration files. Solr admin UI can be accessed by localhost:8003

Varnish

To spin up a container with Varnish uncomment lines with varnish service definition in the compose file. Use the port specified in the compose file to access the website via Varnish.

Multiple projects

To use D4D with multiple projects simply adjust the ports in the compose file, e.g. instead of ports 8000, 8001, 8002 you can use 7000, 7001, 7002.

Docroot in subdirectory

If your docroot located in a subdirectory use options PHP_DOCROOT and NGINX_DOCROOT to specify the path (relative path inside the /var/www/html/ directory) for PHP and Nginx containers.

Logs

To get logs from a container simply run (skip the last param to get logs form all the containers):

$ docker-compose logs [service]

Example: real-time logs of the PHP container:

$ docker-compose logs -f php

Status

We're actively working on this instructions and containers. More options will be added soon. If you have a feature request or found a bug please submit an issue.

We update containers from time to time, to get the lastest changes simply run again:

$ docker-compose up -d