.

Start your adventure with Docker-Console

People who follow our blog already know that we’re using Docker at Droptica. Docker is great when you do development with drupal because it allows you to propagate enviroment between developers quickly and easily.

We've also already shown you how easy it is to start a project using our docker-drupal application.

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.

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 a 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

Before we start drupal installation, first we should run command:

dcon set-permissions

This command will set correct permissions on files and directories in the project. This way we will avoid errors during installation.

Now, when opening this address returned by "docker inspect", 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_databases 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.

To make database dump you can also use command:

dcon dump

This command creates database dump, compresses it to tar.gz format right away and places it in app_databases directory. app_databases is default location set in DUMP_EXPORT_LOCATION, in DB variable, in dc_settings.py file, which can be changed if needed.

Dump file made this way is ready for import, just permissions needs to be adjusted and also name changed to database.sql.tar.gz.


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. 

3. Best practices for software development teams