- Install Docker
Docker only works inside a Linux environment. Mac and Windows users need to install Boot2Docker. But don't worry, once this is set up, using docker feels the same as using it locally.
Install boot2docker for Windows 7.1+
- Install
docker-compose
Windows Users: Unfortunately docker-compose isn't available for Windows yet. You'll need to manually set up your containers.
See the official docs for instructions
- Create a WordPress Container
-
Create a new folder in your user directory or just clone this repository.
-
Create a new file
docker-compose.yml
and enter the following:wordpress: image: wordpress links: - db:mysql ports: - 8080:80 db: image: mariadb environment: MYSQL_ROOT_PASSWORD: example
-
If using boot2docker, run:
boot2docker ip
Copy this down, you'll need it for Step 5
-
At the terminal, run:
docker-compose up
Docker will then start building the image. This may take some time, but it only needs to be done once.
-
Open up the Docker VM IP address in the browser, for example
http://<boot2docker ip>:8080
-
Install WordPress as usual!
-
Mounting a local folder
While developing a theme or plugin you will want to be able to edit the files inside the container. In this section we'll link up a folder on the local filesystem to the server's html directory. This way we can easily edit themes and plugins with our favourite editor.
-
Create a new folder:
mkdir src
-
Open the
docker-compose.yml
file and add avolumes
section under thewordpress
containerwordpress: ... volumes: - ./src:/var/www/html ...
-
Once again run:
docker-compose up
-
You can copy your themes, plugins etc. into
./src/wp-content
, or edit./src/wp-config.php
. -
Install WP-CLI
In this section we will customise the image used to start new WordPress containers by installing WP-CLI. First let's have a look at official WordPress docker image.
-
Take a look at the official WordPress Dockerfile
If you've set up a new Linux server to host WordPress, these commands might look pretty familiar. Notice the first line:
FROM php:5.6-apache
This means that this Dockerfile actually builds off another Dockerfile. In this way you can set up a tree of Dockerfiles as needed.
-
Create a new folder in which we'll describe the image with WP-CLI:
mkdir wpcli
-
Create a new file
./wpcli/Dockerfile
and fill it with the following:FROM wordpress # Install WP-CLI RUN apt-get update RUN apt-get install -y wget RUN wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /bin/wp RUN chmod +x /bin/wp
-
Inside
docker-wordpress.yml
, change the following lineimage: wordpress
to:
build: ./wpcli
-
Build and start the new containers
docker-compose up
-
Now you can start a new WordPress container running a terminal:
docker-compose run wordpress bash wp --allow-root post list