Scroll Top
19th Ave New York, NY 95822, USA

Setting Up A Local Development Environment Using Docker

Building a custom WordPress theme? Developing a plugin? No doubt you would need a local environment to develop and test your creation. But how do you go about it? Surely you can go with XAMPP (or WAMP), but have you ever felt the frustration of going through the installation of a PHP extension to XAMPP or the performance issues XAMPP brings when used locally? Here’s the solution for you – Docker.

In this post, we will guide you through the Docker quickstart for your local WordPress development environment.

Before Getting Started

What is Docker? Basically, it is a tool that lets you create, manage and run applications in virtual containers. The bright part of it is Docker packs the application, its config and all of its dependencies into just one container. That allows you to run the same container in different environments be it local, a staging server on the cloud hosting or even the production environment on your production server. (Still, we must add that running the production application (WP website in this case) in a Docker container is not recommended, but it is possible.)

What makes a Docker virtual container so fast to start and really lightweight is the fact that it only includes the application code isolated from the system it runs on. There’s no operating system (OS) included and it uses the underlying OS instead.

Where Do I Start?

Before starting the Docker environment setup you would need to download and install a couple of things:

The official Docker website has a pretty straightforward installation guide. Just select the OS you are using on your machine and follow the instructions.

Ok, Done. What’s Next?

Next, you need to create a new folder in your file system which will be the main folder for your Docker files. Create a folder and name it, for example, docker-wp.

Then enter that folder and create a file called  docker-compose.yml.  This will be your central Docker configuration file.

The thing we really like about Docker is Docker Hub — an official image library that lets you implement and use a variety of container images you can use in your work. Images we will be using for this example are an official WordPress image and MariaDB (database forked from MySQL).

Now we will need to add the configuration itself to our docker-compose.yml. Below is an example config  with comments describing each block.

version: '2'
 # Defines the Docker Compose version
services:
 # Services block defines which images Docker will run. In this case it is MariaDB image for MySQL and a WordPress image
 mysql:
 image: 'mariadb:latest'
 # Using the latest MariaDB image from the Docker Hub   environment:
 MYSQL_ROOT_PASSWORD: example_root_pass
 MYSQL_DATABASE: example_wpdbname
 MYSQL_USER: example_user 
 MYSQL_PASSWORD: example_password_db
 # MySQL variables: database, database username, database user password, and the MySQL root password.
 ports:
 - 3308:3306
	# Ports for our database  wp-site:
 depends_on:
 - mysql
 # This line makes WordPress image to wait until MariaDB loads before starting itself
 image: wordpress:latest
	# Docker Hub image for WordPress
 environment: 
 WORDPRESS_DB_HOST: db:3306 
 WORDPRESS_DB_USER: example_user 
 WORDPRESS_DB_PASSWORD: example_password
 WORDPRESS_DB_NAME: example_wpdbname
 # Same with the variables here. These are the variables for WordPress container to work
 ports:
 - 80:80
 - 443:443
	# Opening both HTTPS and HTTP ports
 volumes:
 - ./wp-content:/var/www/html/wp-content
 links:
 - mysql
	# This line connects the WordPress image to the MariaDB image within our container

Starting the Container

Now that our Docker container is configured we can start it using a simple command in the CLI:

docker-compose up -d

This command will start the Docker container using Docker Composer tool and the standard docker-compose.yml file. In case you have a couple of different config YAML files with different settings you want to try out, your command would look something like this:

docker-compose -f docker-compose.dev.yml up -

Once your container is up and running go to http://localhost/ or http://127.0.0.1/ to finish your WordPress installation.

Once you are done with it voila! Your local development environment is ready to go and serve you in your work. 

So How Do I Access the Files and, Well, Develop?

Remember that WordPress container image we pulled in our YAML file? When we imported it, it had automatically created all the necessary files and folders for our WP environment, such as wp-content, wp-admin, and wp-includes. Now, you can simply navigate to your docker-wp and place all the other files there that you need for your project.

Conclusion

Docker is a powerful, easy-to-use and really handy tool that helps you streamline your development process. While switching between different solutions for the local development environment for the last couple of years we find this option to be one of the best.    

At Points Group, we make website development easy by partnering with you to manage all your website needs. Contact us today if you have questions about your website! 

Some ReadMores