Note: This is revision of a previous article “Cloud Native WordPress: Local Development Setup“. This revision is much more simple and in my opinion, is the way to go in 2017 for local WordPress development on Mac and Window machines.
Why Use Containers for WordPress?
The objective is to make WordPress a backend microservice. Encapsulating WordPress into containers in our local environment will give us the opportunity to not only build our app in a easily distributed manner, but also allow us to architect cloud based microservices using WordPress.
What are Linux Containers?
Linux Containers have been around for awhile, but have been recently standardized through the Open Container Initiative (OCI). Linux Containers act as isolated VMs “but but without the overhead that comes with running a separate kernel and simulating all the hardware”.
What is Docker?
The most popular linux container runtime is Docker. Docker offers a CLI to create, manage and distribute containers. The Docker for Mac/Windows environments runs on top of Mac and Windows native hypervisors and replaces the need for VirtualBox and Vagrant.
Step 1: Install Docker for Mac/Windows
Step 2: Check Preferences for File Sharing
In your top right tool bar you should see a Docker whale icon. Click it and navigate to the Preferences section and then to the “File Sharing” tab. Ensure that the folder you are going to install is a sub directory of a shared folder there.
Step 3: Create Docker-Compose File
In the root of your project directory, create a file called docker-compose.yml and paste this into its contents:
version: '2' services: db: image: mariadb:latest volumes: - ./env/mariadb/data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: wordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8080:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: - ./wordpress:/var/www/html volumes: db_data:
This is the same as the Docker CLI equivalents:
docker run -d -v /home/core/env/mariadb/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 --name mariadb mariadb:latest
docker run -d --name wordpress --link mariadb:mysql -p 8080:80 -v /home/core/share/site:/srv/www/space-rocket/public_html/wordpress wordpress:4.7.0-php7.0-apache
The most important and challenging part of this is the mapping the “volumes” correctly. In this example, I am keeping my WordPress files inside a folder called “wordpress” and mapped to /var/www/html inside the Docker container (this is where Docker has the files inside the container, as stated with the line:
volumes: - ./wordpress:/var/www/html
Step 4: Start the Docker Containers
Start the Docker container by running:
docker-compose up -d
Thats it! Wait a couple moments (It takes a minute or so for the installation to complete. You can see the status by running
docker logs <container name> --follow). Now go to http://www.space-rocket.com to see the good ol WordPress installation page:
If you have a running container preventing you from using port 8080, identify it by typing:
and then removing it by typing:
docker rm <container name>
If its running, you’ll need to add a force flag, -f
stop all containers:
docker kill $(docker ps -q)
remove all containers:
docker rm $(docker ps -a -q)
remove all docker images:
docker rmi $(docker images -q)
Docker for Mac/Windows is awesome. We now have a way to work locally with our files and database stored on our computer using Docker containers. When you are done, you can save the Docker container as a Docker image and easily distribute to other team members to work on or use in a cloud cluster.
Hope you enjoyed this tutorial. Now go create what you like and have fun!