Trying Drupal 8 on Docker and Mac in three easy steps

Docker is getting a lot of traction nowadays, and for a reason. It’s an amazing way to develop and deploy web applications.

Over past few years, I’ve tried almost all possible tools and ways to develop locally, including running native LEMP stack, using MAMP, Vagrant, and even a very nice tool called Otto (a so-called successor to Vagrant).

What stopped me from jumping right on the Docker bandwagon was probably lack of native support for Mac computers. Sure, you could still run Docker inside a Vagrant-powered VM, but having too many levels of virtualization didn’t sound very appealing to me.

Well, good news everyone! We can now embrace the full power of Docker on our Macs without extra hassle. Meet Docker for Mac Beta.

Pro tip: it’s available for Windows too ;)

There is one catch, though — learning all things Docker is not so easy and probably compares to mastering similar tools for orchestrating your infrastructure.

So here I will show you an example on how to jump right in and give it a try by spinning up a sample Drupal website so that later on you can dive deeper, learn stuff and do cool things on your own.

The result of this tutorial can be used just for playing around, and not suitable even for local development.

1. Get Docker for Mac

Previously, you would have to use the Docker Toolbox, which includes not just Docker engine, but also a virtual machine. This is no longer needed, so just remember you don’t need Toolbox unless you want to experiment and have fun.

Instead, visit https://beta.docker.com and apply for an account. Once you get approval, install the Docker app and check if it’s running correctly:

docker run hello-world

What happened here:

  1. The Docker client requested the Docker daemon to pull the «hello-world» image from the Docker Hub (where you will find a gazillion of other images btw);
  2. The Docker daemon fired a new container from that image, containing a very basic program, which only purpose is to print «Hello world» stuff;
  3. The Docker daemon streamed that output to the Docker client so that you can see the output in the terminal.

To run apps like Drupal, we will need appropriate containers from the Docker Hub. Most notably:

  • HTTP Server with PHP: I will use Drupal image with Apache and PHP bundled in;
  • SQL Server: I’m going to be using an official MySQL docker image.

Note that for those images you can have plenty of configurations, and choose to install different Drupal versions, use Apache or Nginx etc. Just keep an eye on available tags.

Our goal now is to pull the images, run the containers and make them talk to each other so that our Drupal container can connect to MySQL one.

2. Run the Drupal and MySQL containers.

Let’s build the MySQL image (that's one line, just copy and paste).

docker run —name test-mysql -e MYSQL_ROOT_PASSWORD=12345 -e MYSQL_DATABASE=drupal -e MYSQL_USER=drupal -e MYSQL_PASSWORD=drupal -d mysql:5.7

We’ve just run the container with a database completely ready to be used for the Drupal installation. The environmental variables we’ve passed are very self-explanatory.

Next, let’s spin up the Drupal container from the Drupal image.

docker run —name test-drupal —link test-mysql:mysql -p 8080:80 -d drupal:8-apache

Make sure no other apps are running on port 8080 on your local machine!

Let’s see a list of the running containers:

docker ps

To see a list of ALL containers:

docker ps -a

To remove a container:

docker rm [name or hash]

To stop a container:

docker stop [name or hash]

To start a container (created previously):

docker start [name or hash]

Let’s see if it worked:

http://localhost:8080

And we should see the Drupal 8 installation screen.

Working with MySQL database

You can connect to the MySQL container as to any other remote or local server. Just execute

docker exec -it test-mysql bash

And we’re inside our MySQL container, so you can do things like

mysql -u root -p12345  
SHOW DATABASES;  

and see the list of databases.

3. Install Drupal

Ok, so we have both containers running, what remains now is to figure out the IP address of MySQL container. We will need it in a second.

For this, type the following command and copy the IP address. The last hash is the ID of our MySQL container.

docker inspect --format '{{ .NetworkSettings.IPAddress }}' test-mysql

Next, let’s use this IP to install Drupal.

Docker Drupal MySQL

And we’re done!

How to access Drupal Docker container

Just as we did it for MySQL container before

docker exec -it test-drupal bash

What's next

If you got it up and running finally (well, it appeared to be quite a journey), and want to learn even more, then be sure to study the following concepts before:

  • What Docker Compose is and how it can make our lives easier;
  • How to manage data in containers so that your custom files won't be wiped when you shut down the container (they will in our case, if you followed along);
  • Study the images available on Docker Hub and pick the ones which suit you better (or even build your own image!)

Have fun and good luck!