This post was contributed by guest author Peter Suhm. Peter is a web developer from the Land of the Danes. He is the creator of WP Pusher and a huge travel addict, bringing his work along with him as he goes.
In the last few years, a wave of virtualization technologies have swept through our WordPress development environments. The one that's sounded the most promising to me has been Docker: lightweight and flexible. Yet, until recently, getting Docker up and running was an overwhelming task – especially on a non-Linux machine. If you managed to get it up and running in a virtual machine (using Vagrant or similar), getting port-forwarding to work would make you give up and just use Vagrant instead.
Now it's different.
With (a stable) Docker for Mac and Windows and Docker Compose at hand, getting Docker up and running is easy and pain-free. With Docker Compose you can tell Docker exactly what you want your WordPress development environment to look like and it will take care of it.
What is Docker?Docker is a technology that makes it really simple to create isolated containers for your applications and websites to run in. These containers can be combined and modified to fit the needs of your applications. Docker is utilizing the Linux Containers technology (LXC) where multiple isolated environments can share the same Linux kernel – making it very lightweight compared to something like Vagrant.
The Docker ecosystem is built around containers. In the Docker Hub, you can find an endless number of containers that other people have built or you can build your own using a Dockerfile. When building your own, you can start from scratch using the base Ubuntu image or extend someone else's image.
You can share local directories with your containers and link the networks, so they can talk to each other – just like you know it from other virtualization technologies. However, this is where it gets complicated which leads me to Docker Compose:
What is Docker Compose?Docker Compose is what makes Docker available to mortals like you and me. As the name implies, Docker Compose is a tool for composing Docker containers. That means defining your services (containers), setting up the network between them, sharing local directories with them, and a few more things.
With Docker Compose you create a simple file in the root of your project that describes the setup required by your application/website. For a WordPress theme that might mean a container to run WordPress, a container to run MySQL and a container to run Gulp or Grunt. This can very easily be defined in a docker-compose.yml file that can then be shared with your team members. This means that you can now share your WordPress theme, including an isolated WordPress environment to run it in. Hurray for virtualization!
Why use Docker?There are a few reasons why Docker is an attractive technology for me. Here are the most important requirements I have for my development environment and how Docker solves them:
This is the very simple Docker setup I use for development of my WP Pusher plugin: A WordPress and a MySQL container. Both of them use the official Docker Hub images, so setting it up is very easy.
My docker-compose.yml file looks like this:
version: '2'
services:db:image: mysql:5.7volumes:- wp_pusher_db_data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: wordpressMYSQL_DATABASE: wordpressMYSQL_USER: wordpressMYSQL_PASSWORD: wordpresswordpress:depends_on:- dbimage: wordpress:php5.6-apachevolumes:- .:/var/www/html/wp-content/plugins/wppusher/ports:- "80:80"restart: alwaysenvironment:WORDPRESS_DB_HOST: db:3306WORDPRESS_DB_PASSWORD: wordpressvolumes:wp_pusher_db_data:
It describes two services: a MySQL 5.7 database and WordPress running on PHP 5.6 and Apache. The database is using a volume on my local machine, so data will be persisted every time I shut off the container. My current directory (in this case a plugin) is mounted into the wp-content/plugins directory. This allows me to work on my plugin in a completely isolated WordPress environment – without installing anything, besides Docker, on my Mac. The WordPress container forwards port 80 to my local machine, so I can access it as "localhost" in my browser.
If you want to try it for yourself, and have Docker installed on your machine, just add the file to your plugin (or theme) and run:
$ docker-compose up -d
In order to see which containers are running, just run:
$ docker ps
This a very simple setup that is easy to extend and build upon.
I hope this post made you curious about Docker and WordPress. Thanks for reading along!
Links Like this:Like Loading...
Related
Source: Composing a WordPress Development Environment with Docker
No comments:
Post a Comment