5.5 KiB
Quickstart: Docker Compose and WordPress
You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers. This quick-start guide demonstrates how to use Compose to set up and run WordPress. Before starting, you'll need to have Compose installed.
Define the project
-
Create an empty project directory.
You can name the directory something easy for you to remember. This directory is the context for your application image. The directory should only contain resources to build that image.
This project directory will contain a
Dockerfile
, adocker-compose.yaml
file, along with a downloadedwordpress
directory and a customwp-config.php
, all of which you will create in the following steps. -
Change directories into your project directory.
For example, if you named your directory
my_wordpress
:$ cd my-wordpress/
-
Create a
Dockerfile
, a file that defines the environment in which your application will run.For more information on how to write Dockerfiles, see the Docker Engine user guide and the Dockerfile reference.
In this case, your Dockerfile should include these two lines:
FROM php:5.6-fpm RUN docker-php-ext-install mysql ADD . /code CMD php -S 0.0.0.0:8000 -t /code/wordpress/
This tells the Docker Engine daemon how to build an image defining a container that contains PHP and WordPress.
-
Create a
docker-compose.yml
file that will start your web service and a separate MySQL instance:version: '2' services: web: build: . ports: - "8000:8000" depends_on: - db volumes: - .:/code db: image: mysql environment: MYSQL_ROOT_PASSWORD: wordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress
-
Download WordPress into the current directory:
$ curl https://wordpress.org/latest.tar.gz | tar -xvzf -
This creates a directory called
wordpress
in your project directory. -
Create a
wp-config.php
file within thewordpress
directory.A supporting file is needed to get this working. At the top level of the wordpress directory, add a new file called
wp-config.php
as shown. This is the standard WordPress config file with a single change to point the database configuration at thedb
container:<?php define('DB_NAME', 'wordpress'); define('DB_USER', 'wordpress'); define('DB_PASSWORD', 'wordpress'); define('DB_HOST', "db:3306"); define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); define('AUTH_KEY', 'put your unique phrase here'); define('SECURE_AUTH_KEY', 'put your unique phrase here'); define('LOGGED_IN_KEY', 'put your unique phrase here'); define('NONCE_KEY', 'put your unique phrase here'); define('AUTH_SALT', 'put your unique phrase here'); define('SECURE_AUTH_SALT', 'put your unique phrase here'); define('LOGGED_IN_SALT', 'put your unique phrase here'); define('NONCE_SALT', 'put your unique phrase here'); $table_prefix = 'wp_'; define('WPLANG', ''); define('WP_DEBUG', false); if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/'); require_once(ABSPATH . 'wp-settings.php'); ?>
-
Verify the contents and structure of your project directory.
![WordPress files](images/wordpress-files.png)
Build the project
With those four new files in place, run docker-compose up
from your project directory. This will pull and build the needed images, and then start the web and database containers.
If you're using Docker Machine, then docker-machine ip MACHINE_VM
gives you the machine address and you can open http://MACHINE_VM_IP:8000
in a browser.
At this point, WordPress should be running on port 8000
of your Docker Host, and you can complete the "famous five-minute installation" as a WordPress administrator.