Photo material from pexels.com

Instant Docker local environment for Wordpress

Whether you are a web developer or running business based on mass production of websites, you or your employees must be using local environment for faster development. In the past, fastest way for the local environment was to set up server stack MAMP/LAMP/etc. or image on some virtual machine like VirtualBox.

Then a few years ago new, simple and powerful player emerged. He is called Docker. For those who are not familiar with it — Docker is a virtual machine which uses containers for virtualisation. In most cases, it works out-of-the-box. You can read more about it on their official website, where you can find extensive guides for easy understanding and simple configuration.

Currently running containers for Wordpress local website environment

Now with Docker, the local development is even easier and faster. You can have fully functional Wordpress website up and run under a minute. You only need to create a file with docker configuration, which can then be copied and used for future projects Wordpress local environments.

So after you get to know Docker and have installed all needed tools for working with it, you are ready for next — more interesting step. you can also download complete project on Github.


How to configure Docker environment

For setting up Docker, we will use Compose tool.

“Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.”
— from docs.docker.com

First, we create a folder called docker-instant-wp which will be used as the folder for our new project. Inside we create file docker-compose.yml

This file is used for local environment configuration of Docker containers. We will specify which image should Docker use for specific container and also configure the connection between Wordpress and database easily as possible. We will set-up volumes for persistent data, so the changes in files will be visible on the host computer as in the container.

Our environment will have 3 containers (WordPress, database, PHPMyAdmin). This is all of the containers we would need to easily deploy the finished website to the production server. The changes in files are visible in our hosts’ folder and we can easily dump database with phpMyAdmin and import it on the production site.

docker-compose.yml

wordpress:
image: wordpress
links:
- mariadb:mysql
environment:
- WORDPRESS_DB_PASSWORD=fddqs32
ports:
- "8000:80"
volumes:
- ./wordpress:/var/www/html
mariadb:
image: mariadb
environment:
- MYSQL_ROOT_PASSWORD=fddqs32
- MYSQL_DATABASE=wordpress
volumes:
- ./database:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- mariadb:mysql
ports:
- "8181:80"
environment:
- MYSQL_USERNAME=root
- MYSQL_ROOT_PASSWORD=fddqs32
- PMA_HOST=mysql

This configuration file can be used for future projects without any modifications. You can just stop the running containers from one website and run another one. For running multiple containers at same time, only few modifications to ports would be necessary.

Let us start Docker local environment

We can easily start the environment if we open up Terminal or any other command-line tools then navigate to project folder and run command docker-compose up (you must previously install docker-compose module so you can use it in terminal).

Usage of docker-compose command for starting local Wordpress environment

After you run the command, you can see the progress of building our local environment printing on the screen. If you don’t have images locally, the Docker will download it for you. When Docker starts building your environment, hosts folder docker-instant-wp will get populated with Wordpress and database files.

If everything runs smoothly, your website will be available at localhost on port 8000 and terminal will be used for printing server requests you make on your local website. Your database access with phpMyAdmin will be on port 8181.


Conclusion

These days, when everything is moving fast — you got to keep up with the flow. If you can not keep it up, you are going to sink. It is that simple. So for business with mass production of websites, I would advise this: give Docker a chance. It sure deserves it.

“ This reduces the risk of — worked in dev, now an ops problem. ” ― James
Turnbull, The Docker Book: Containerization is the new virtualization

If you enjoyed reading this article, show some ❤ and be the reason for even more daily motivation. I sure am doing the same thing. Enjoy your day and thank you for stopping by.

Please reach out if you have any questions or proposal! You can follow me and ask questions on Twitter or contact me via email.

KA:NR