Recipe: Run a local copy of a WordPress site with Docker

I recently decided to publish my articles in French and in English. First, I had to find and test plugins in order to choose one. So I spent some time, more than I expected, to make it work locally through Docker. Since I encountered some pitfalls, I decided to share the recipe.

1. Create the artefacts

We connect to the server with ssh, then we generate the “dump” file of the database. Here, under mysql:

mysqldump -u USER -p PASSWORD > /BLOG_BASE_DIR/blog.sql

Replace “USER”, “PASSWORD” and “BLOG_BASE_DIR” with proper values.

2. Download the artefacts

Locally, create a blog directory. This is where the following steps will be performed.

rsync [USER@]HOST::/BLOG_BASE_DIR PATH_TO_BLOG_DIR/site

Then move blog.sql.

> mkdir db
> mv site/blog.sql db/.

3. Add directives to blog.sql

The blog.sql file contains the production values for WordPress. Two of them need to be modified to work in another environment.

Add this at the end of the blog.sql file.

UPDATE options SET option_value='http://127.0.0.1' WHERE option_name IN ('siteurl', 'home');
UPDATE options SET option_value='LOCAL TRIAL' WHERE option_name='blogname';

⚠️ It is important NOT to add a slash at the end of the address, otherwise you will get endless redirects when you try to access your site.

4. Update wp-config.php

Ouvrez site/wp-config.php, et changez les valeurs DB_USER et DB_PASSWORD pour root.

define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');

5. Create file setup.sh

docker exec blog_db_1 bash -c 'mysql -u root --password=root < /db/blog.sql'

docker exec blog_server_1 bash -c 'a2enmod rewrite'
docker exec blog_server_1 bash -c 'docker-php-ext-install pdo_mysql'
docker exec blog_server_1 bash -c 'docker-php-ext-install mysqli'
docker exec blog_server_1 bash -c 'service apache2 restart'

In detail: The first line loads the data into the database.

The following line installs modrewrite in apache, followed by two php modules: pdo_mysql and mysqli.

Finally, the apache2 service is restarted.

6. Create file docker-compose.yml

Here we use docker-compose to orchestrate two docker containers. It is simply installed with

pip install docker-compose

Here is the docker-compose.yml file

services:
  db:
    image: mariadb:10.4.24
    hostname: blog-db
    ports:
      - "127.0.0.1:3306:3306"
    volumes:
      - ./db:/db:ro
    environment:
      - MARIADB_ROOT_PASSWORD=root
  server:
    image: php:7.4-apache
    hostname: blog-server
    ports:
      - "127.0.0.1:80:80"
    volumes:
      - ./site:/var/www/html/
    depends_on:
      - db

Obviously, use the database, php and apache versions that suit you. Of course, the present recipe may not be exact depending on those versions.

7. Start the containers

docker-compose up

8. Execute setup.sh

In a new shell, run

bash setup.sh

9. Restart the apache container

In the shell where you ran docker-compose up, interrupt it (CTRL+C) and restart it (docker-compose up).

Explanation: The last command in setup.sh has the effect of shutting down the apache container. (The cause being that containers shut down when the process running under PID 1 shuts down. Since apache is running under PID 1 and the service apache2 restart command shuts it down, the container shuts down).

10. Access your local copy

Start your browser and point it to http://127.0.0.1

🎉

Cleanup

  1. Stop the containers CTRL+C in the shell where docker-compose up was run, and run docker-compose down.
  2. Delete the directory site

Related Posts

Comments

  1. You can add
    restart: always
    for cotainer to restart to not do hacky job with ctrl+c 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.