How to set up local development environment for WordPress with Docker for Mac (For Beginner)

When we develop WordPress theme, we prepare the local server with XAMPP or MAMP, in case of professionals, they tend to be using Vagrant.
(I usually use Trellis, Bedrock and Sage, too)
*Recently, the good tool called “Local” appeared as well.

However, we are dissatisfied that when using XAMPP or MAMP on more than one person, the difference might occur and Vagrant doesn’t have such a problem but it has problems that are very slow starting up and waste a capacity of storage very much.

In this time, I introduce the way of building local environment for WordPress with Docker for Mac which gains momentum.
I’m not sure that this way can solve those complaining, though.

 

Anyhow, To get Docker for Mac.

Visit Docker official document and downloading Docker for Mac and install it. It’s ok with the stable version.

You become to be able to use docker command on the terminal after install.
Let’s try to do it in order to confirm that Docker was installed properly.
When executing following command on terminal, you can confirm installed Docker’s version.

[bash]
$ docker —version
Docker version 17.06.2-ce, build cec0b72
[/bash]

About Docker Image and Docker Container

You can think that Docker image is a template for a container in order to create a container and Docker container is a box for working images.
An instance of an image is called a container.

 

Create a Docker Container for MySQL.

It’s created immediately using a command available Docker provided.
Also, if you get a failure, it can delete or re-build easily.

[bash]
$ docker run –name [container-name] -e MYSQL_ROOT_PASSWORD=[password] -d mysql:[version]
[/bash]

 

You should register and search to Docker Hub in order to install MySQL which is the version that you want.
This time, I’ll try to install MariaDB instead of MySQL.

[bash]
$ docker run –name dev_db -e MYSQL_ROOT_PASSWORD=admin -d mariadb:10.3.1
[/bash]

 

Confirm MariaDB is running correctly after installing it.
At first, display what instances are running.

[bash]
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c37a2445f8e mariadb:10.3.1 "docker-entrypoint…" 5 hours ago Up 5 hours 3306/tcp dev_db
[/bash]

 

Next, confirm what MariaDB is running or not.

[bash]
$ docker exec -it [database name] bash
[/bash]

[bash]
root@2c37a2445f8e:/# ps ax
PID TTY STAT TIME COMMAND
1 ? Ssl 0:09 mysqld
180 pts/0 Ss 0:00 bash
186 pts/0 R+ 0:00 ps ax
[/bash]

 

It’s ok if such behavior and display.
End with exit command after you confirmed.

[bash]
root@2c37a2445f8e:/# exit
[/bash]

Create a Docker Container for WordPress.

Next, I’ll create a Docker container for running WordPress.
I’m using Apache version which is easy to install.
Input following command on the terminal in reference to Docker Hub.

[bash]
$ docker run –name (container name for wordpress) –link (name of db container):mysql -p 8080:80 -v (local project path):/var/www/html -d wordpress
[/bash]

 

e.g)

[bash]
$ docker run –name wordpress –link dev_db:mysql -p 8080:80 -v ~/project/WP_Docker/wordpress:/var/www/html -d wordpress
[/bash]

 

There is a case that package is expanding to chosen directory even after the command is finished on the terminal.
You’ll check WordPress files is expanded properly for the directory which is you’ve chosen after 2 or 3 minutes.

[bash]
$ cd ~/project/WP_Docker/wordpress
$ ls -la
[/bash]

 

Confirm running instances with the way as same when creating MySQL container.

[bash]
$ docker ps -a
[/bash]

[bash]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f14346582e99 wordpress "docker-entrypoint…" 1 minutes ago Up 15 hours 0.0.0.0:8080->80/tcp wordpress
2c37a2445f8e mariadb:10.3.1 "docker-entrypoint…" 5 minutes ago Up 15 hours 3306/tcp dev_db
[/bash]

 

Let’s access to following address in the browser after confirm that MySQL and WordPress instances are running.

http://localhost:8080
http://0.0.0.0:8080

 

If displayed WordPress install page, the local development environment is finished creating!!

 

Get Started!

And then, create WordPress theme as usual.
Thank you for your hard work!

This environment is faster starting up than Vagrant and hasn’t got to config virtual host like MAMP so we can work with quick.
I’m researching how to use Roots Bedrock in this environment now.
If you know how to do it, please let me know. 🙂
( I heard Bedrock can put into Local so I might use it. lol )