Running WordPress in Docker
Hey there all you WordPress developers. Guess what? You can now use the amazing world of Docker to quickly and easily create WordPress environments anywhere on any computer. The advantages of using WordPress in Docker are huge. You can make a number of WordPress environments in seconds that have various versions of PHP and/or various databases.
In the last article, we learned about docker compose and about working with more advanced environments. In this case, WordPress would be considered a ‘complex environment.’ This is because in one place we’ll be running PHP and in another separate place we’ll be running MySQL. In the example from the last article, we use PHPMyAdmin while here we’ll build WordPress. It’s pretty much the same principle—a compose file with settings for both environments inside.
Make a folder on your computer and put a docker-compose.yml file inside of it. Then, go to the WordPress section of the Docker website and find the compose file you need.
version: '3.1' services: wordpress: image: wordpress restart: always ports: - 8080:80 environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: exampleuser WORDPRESS_DB_PASSWORD: examplepass WORDPRESS_DB_NAME: exampledb db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: exampledb MYSQL_USER: exampleuser MYSQL_PASSWORD: examplepass MYSQL_RANDOM_ROOT_PASSWORD: '1'
The environmental variables are the most important thing here. I think I probably don’t need to go into too much detail about them. Just keep in mind that if your Docker is on an external environment (not a development one), it’s a very bad idea to use a password like ‘examplepass’ in a production environment.
So what’s the problem with this file? The file will actually work just fine, but we’ll have trouble accessing any WordPress files in order to add, edit, or change them. With WordPress development, at some point, we’re going to have to work on templates and plugins. So how do we do this? Just a little addon that will connect between the files of the various compartments and our hard drive. This is done by adding a little bit of code to the original compose:
version: '3.1' services: wordpress: image: wordpress restart: always ports: - 8080:80 environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: exampleuser WORDPRESS_DB_PASSWORD: examplepass WORDPRESS_DB_NAME: exampledb # Keep your data volumes: - ./site:/var/www/html/ db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: exampledb MYSQL_USER: exampleuser MYSQL_PASSWORD: examplepass MYSQL_RANDOM_ROOT_PASSWORD: '1' # Keep your data volumes: - ./db:/var/lib/mysql
In general, the settings are found under the volumes. First the local folder (relative to the compose file) and second the setting inside the container. For example:
This line says that the site folder will be inside the folder where the compose folder is located on our computer. So this will be the var/www/html folder of the container that has WordPress files. Easy as pie. When we run the compose file it will create the folder and will also populate it with the WordPress files that were downloaded.
It also gives us persistence. In other words, if we shut down Docker and our local machine, and then reboot everything afterward, the files and data will all be there. Pretty sweet!
Next article: coming soon!
About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.