Boost your Drupal development with Docker
Blog

Boost your Drupal development with Docker

Vagrant is a great virtualisation tool, which I prefer heavily for my development purposes. But sometimes it gets a bit hectic and resource consuming, to set up a new vagrant environment to work trivial things or testing out a module/API. 

Not being a great fan of local *AMP stack I was looking for some alternative to Vagrant to use. In comes Docker, which is super fast and very easy to setup. Containers (“virtual machines”) are easy to destroy and  rebuild.They do not require the overhead of virtual machines but they  still provide a high level of isolation from the host OS.

Docker hub have many Docker containers for Drupal which are ready to use . But I prefered to create my own Docker container which just works and runs Drupal smoothly.

  1. Create a directory anywhere in your system. I have it placed in ~/Documents/docker/drupal. (“drupal” is my Drupal root folder)
  2. Create a file called Dockerfile and paste the below code:
  3. What it does? This will install all the necessary packages required for running Drupal. Note: There is no MySQL package installed.
  4. Once the Dockerfile is placed, create the Docker image out from your Dockerfile.
  5. Run docker build -t drupal . This will take a  few minutes, which will create an image named “drupal”.
  6. Now we need to have a container for MySQL. The reason for having a separate container for MySQL is to make my Drupal container faster.
  7. Run docker run -p 3308:3306 --name drupal-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=drupal -d mysql:5.5 This will again take few minutes to download the MySQL image. Once it’s downloaded, from next time the command will be executed in milliseconds. NOTE: You need to give the MySQL container IP as host and 3308 as MySQL Port.
  8. Run the Drupal container from the image created in Step 4.
  9. docker run --name drupal8 -p 8080:80 -p 8028:22 --link drupal-mysql:mysql -v ~/docker/drupal8/drupal:/var/www/html -d drupal This will forward the Apache port 80 of the host system to 8080 port for the container. Also notice we are mapping our MySQL Container to the Drupal container's MySQL.
     

That’s it. Now if you do a docker ps you will see the running docker containers.

Docker ps

Now, go to http://localhost:8080 and you will see the Drupal installation screen, from where it is plain Drupal installation.

Brownie Points:

  • When your work is done: docker stop drupal-mysql && docker stop drupal8
  • Start working again: docker start drupal-mysql && docker start drupal8
  • Destroy the environment and rebuild: 
    • docker rm drupal-mysql, then `docker run -p 3308:3306 --name drupal-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=drupal -d mysql:5.5`
    • docker rm drupal8, then  `docker run --name drupal8 -p 8080:80 -p 8028:22 -p 3038:3036 -v ~/docker/drupal8/drupal:/var/www/html -d drupal8`