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
> mkdir db > mv site/blog.sql db/.
3. Add directives to blog.sql
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
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
site/wp-config.php, et changez les valeurs
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:
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
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
8. Execute setup.sh
In a new shell, run
9. Restart the apache container
In the shell where you ran
docker-compose up, interrupt it (
CTRL+C) and restart it (
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
- Stop the containers
CTRL+Cin the shell where
docker-compose upwas run, and run
- Delete the directory