Docker logo - a blue whale with containers on its back

Start your adventure with Docker-Console in the example of the Drupal 7 project

06.06.2017

People who follow our blog already know that we’re using Docker at Droptica. We've also already shown you how easy it is to start a project using our docker-drupal application (https://www.droptica.pl/blog/poznaj-aplikacje-docker-drupal-w-15-minut-docker-i-przyklad-projektu-na-drupal-8/).

Another step on the road to getting efficient and proficient with Docker is docker-console application, which is a newer version of docker-drupal, and exactly like its predecessor it was created in order to make building a working environment for Drupal simple and more efficient. How does it all work? You are going to see that in this write-up. Since we're all working on Linux (mainly on Ubuntu), all commands shown in this post were executed on Ubuntu 16.04.

Installing Docker

Of course, our adventure starts with installing Docker, you could read about this in one of our earlier posts. Just for the sake of clarity, in order to install Docker, you have to execute the following commands:
 

wget -qO- https://get.docker.com/ | sh 

sudo usermod -aG docker $(whoami) 

sudo apt-get -y install python-pip

sudo pip install docker-compose

Installing docker-console

Another step is to install the docker-console application. In order to do so, you should first install python-pip package, which allows to quickly install and upgrade our application:

sudo apt-get update && sudo apt-get -y install python-yaml python-setuptools python-pip python-dev build-essential 

Now all you have to do is to install docker-console, which can be done using the following command:

sudo pip install docker-console 

After the installation completes, a new command – docker-console (or dcon for short) – will be available in your terminal. After launching it, you should get a list of available options and actions:

Docker console available options and actions

Initialisation of a new project

Now we can finally begin our adventure with docker-console. To start with, let's assume that we don't have any Drupal project available, so we have to start a new one. This is possible by using the following command:

docker-console init --tpl drupal7 

Project's structure with visible folders

After running it, it should generate an entire project structure for us (like on the image above), which will allow us to quickly and efficiently organise a project. The command is used for generating Drupal 7 project structure, but nothing stops you from using a command to generate a Drupal 8 project instead:

docker-console init --tpl drupal8 

However, for the purpose of this guide, I will stick to a Drupal 7 project. The next step is downloading an appropriate version of Drupal from https://www.drupal.org/project/drupal and unpacking the archive to the “app” folder.

Start the containers

At that point, in order to start working with the project, you will also have to launch the containers with services required for the page to work (e.g. Apache, MySQL, PHPMyAdmin, Nginx – in a traditional working environment, you would have to install them locally on your machine). All the services we will need for work should be added and configured in docker-compose.XML file (the file for this project is presented below and can be found in the main folder of the project.) We build our docker-compose files based on version 1. If you would like to learn more about how it’s built, you should visit the project’s website:  https://docs.docker.com/compose/compose-file/compose-file-v1/ 
The configuration you can see below will launch three containers:

  • web: Apache and PHP,
  • MySQL,
  • PHPMyAdmin.

Three containers ready to be opened

In order to start the containers, execute the following command:

dcon up 

After a moment (a longer moment during the first launch, because all the images will have to be downloaded first) you should see a result that’s similar to this:

Containers opened up and running

The website is now built, you can go to the container’s IP address and take a look. You can check the IP address by running “docker inspect”.

docker inspect --format "{{ .NetworkSettings.IPAddress }}" dockerconsoleprojectexample_web_1 

In my case, the address was  http://172.17.0.23/ (it may be different, depending on the containers you launch).

Installing Drupal

After going to that address, you should see Drupal installation screen. It is by no means different than a standard one, therefore I am not going to write a step-by-step instruction. However, it is very important to use the data from your docker-compose.yml file when prompted to configure your database, exactly as it is shown in the illustrations below. The database name should be in line with MYSQL_DATABASE parameter, same goes to the user details (the username can be set in MYSQL_USER with a password stated in MYSQL_PASSWORD, you may also decide to use root, the password for root is set in MYSQL_ROOT_PASSWORD), you also have to enter the name of our database container as a host, otherwise you will not be able to connect to it.

All names are in line, database connected

All names are in line, database connected

 

Website launched

Now, when your website is ready and available at the container’s IP address, you can implement your changes and add content. You should, however, keep in mind that everything is happening in Docker containers, so if you remove them, everything – including the database – will be lost. This does not apply to the application files since they are only mapped from a local folder to the app folder in a container. If you want to secure your database and prevent losing it even if you remove the containers, you should also map your database files to a local folder. By doing so, you will allow them to synchronise with a folder in the container. What is more important, the database is not deleted when the containers are just stopped, for example when you turn off your computer. In this case, you can quickly relaunch them by running “dcon up” again. In another example, we will dump the database, which we will be able to use later in order to build another project.

Rebuilding a project

The first thing you should do is dump your database and files. To do this, I used the backup and migrate module ( https://www.drupal.org/project/backup_migrate), but you can do it in any way you like. It is, however, crucial that the files and the database end up as tar.gz archives named database.sql.tar.gz and files.tar.gz. After that, place the database file in the app_data folder, then move the compressed files to app_files.
If you would like to get to the PHPMyAdmin container in order to dump the database, you can, of course, do it just as easily by going to the IP address of the PHPMyAdmin container in your browser and logging in with the data from docker-compose.yml file shown above.
If you did not go through all the earlier steps, you can download my project, database and files. This way, you will be able to start your adventure with docker-console without creating your own project after installing docker, docker-compose and docker-console.

Project repository: https://github.com/DropticaExamples/docker-console-project-example

Database dump: https://www.dropbox.com/s/tcfkgpg2ume17r3/database.sql.tar.gz?dl=0

Project files: https://www.dropbox.com/s/hl506wciwj60fds/files.tar.gz?dl=0

In order to clone my project and launch the service containers, you will have to run the following commands in your terminal:

cd

git clone https://github.com/DropticaExamples/docker-console-project-example

cd docker-console-project-example 

git checkout docker-console-start 

cd app_databases wget https://www.dropbox.com/s/tcfkgpg2ume17r3/database.sql.tar.gz?dl=0 

cd ../app_files wget https://www.dropbox.com/s/tnl0ftfluyr5j7p/files.tar.gz?dl=0 

cd .. 

dcon up 

Otherwise, if you are using a project created beforehand, all you have to do is just:

dcon up 

Unless, of course, you had the containers already launched, in this case, you will not have to start them again (running “dcon up” when your containers are up and running should only tell you that they are running and up-to-date.)

Installed and running containers

If you are not sure whether your docker containers are built and active, you can check it using:

docker ps -a 

If everything went well, you should have three containers up and running:

  • web: Apache and PHP,
  • MySQL,
  • PHPMyAdmin.

Building a project

In this step, we are going to restore our working environment based on the files and the database, which were placed in proper folders beforehand. By doing so, we can constantly work in an environment that is close to production. Now we can build a website on working containers. In order to do so, execute the command:

dcon build 

In previous examples, we did not do it, because there was no need to start a project building sequence – there are no files and the database is empty when you are starting a new project). In this example, the building process covers the following steps:

  • confirmation that the building process should start;
  • copying settings;
  • cleaning database;
  • importing database;
  • unpacking files;
  • cleaning cache (drush cc all);
  • generating login link (drush uli);
  • changing file permissions.

This is why when we want to import a database and files to a project, we have to run dcon build.

Website’s ready

The website is now built and you can take a look at it exactly like before, by going to the container’s IP address.

Website is ready!

Summary

This is how you can quickly set up a project based on Docker and docker-console. At your first attempt, it may seem that the procedure is quite complicated, but you can take my word that you will quickly see a return on all your time invested into learning Docker, especially when you will want to quickly add a new service to an existing project or test some other configurations. Of course, this post is not fully exhaustive, and if the time allows, we are going to write more about docker-console.

In the meantime, I encourage everyone to read the project’s readme file at  https://github.com/droptica/docker-console and try to set up your own project with Drupal 8. Let us know how it went :)

We are also going to share the DrupalCamp Wrocław 2017 videos with you soon, featuring Maciej Łukiański talking about docker-console and some other stuff. 

Need a team of Drupal and PHP web development experts?

Contact us now!