Basic Docker configuration as development environment for Drupal
this post aims to give a first time introduction for anyone who still lay about Docker.
And hopefully for anyone in the future will learn more about Docker and implement it into the project everyday.
Problems
Sometimes we are lazy to install apache,mysql,php (amp)separately on our computers to develop websites (with cool features that we will create), before we deploy them to our favorite servers. At least that's what I experienced before.
indeed there are such applications that greatly facilitate us to install these 3 components.
Docker is an open-source application for automating deployment of Linux applications by using container-based software link
Docker is similar to Vagrant, and container here can be analogized as a server, but with many advantages such as:
- It feels faster and easier to configure it (compared Vagrant).
- Be able to setup multiple different containers for our applications with ease, for example: Apache and Apache installed A containers only; container B installed MySQL only; container C installed Apache SOLR only, and so on.
- Because of the second point above, the one container is isolated from another container, in other words, if container B has a problem, the other container is not affected.
Docker Installation
the first step we do is download and install Docker on our computer. Please go to the official Docker page for further
Note:
- Docker toolbox includes old version of app. The latest one is Docker for Mac.
- Docker for Mac is very easy on installation rather than the Docker toolbox.
- I'm still using the Docker toolbox, because I think it's more stable than Docker for Mac.
After that, make sure Docker is already installed correctly by using the command below in our terminal.
If you get the output as below, then Docker is installed correctly.
Anatomy of Dockerfile
In the following steps, make sure we have prepared Drupal project (Drupal files and folders) which we will use to configure Docker.
After that, we need to create a file with the name Dockerfile.
This file is the main file that Docker will be reading for the first time.
This Dockerfile file can be placed anywhere, provided it should still refer to the files and folders of our Drupal project.
For details see the two sample below:
The basic codes of Dockerfile can be seen below:
First and second line on Dockerfile
Please note that drupal here is the name of the image itself, and the 7-apache separated by a colon, is the tag name of the image in use.
In the second line, this section only tells who made the configuration of this Docker.
For those who are accustomed to working with Linux or MacOS of course these commands may already be familiar.
Anatomy of docker-compose.yml
A little explanation of the Docker compose, this is a command from the Docker to call and connect multiple containers simultaneously.
Also note that the docker-compose.yml file needs to be placed in the same folder / directory where the Dockerfile file is located.
In the first line, this is the syntax to start Docker compose.
version 2 here means is to declare that the commands we will use for Docker compose is the newest, ie version 2.
In the second line, the services command here is the syntax to declare the containers we will create / configuration, in this case: ardiweb and ardimysql (see image below for more info).
The first container in docker-compose.yml
As already mentioned in the docker-compose.yml command there are 2 containers to be configured, the first is a container named ardiweb, and the second is ardimysql.
For naming the container, we are free to call it anything, as long as it is clear and easy to understand.
Let's discuss the ardiweb container first. This container will serve as a server for Apache and PHP, along with our Drupal project.
We can see there are some key: value there:
build:
This section specifies which directories we will use to create the container. Usually the directory used is a Dockerfile file.
Please note that the value is a POINT (.) Which specifies the root directory we will use.
ports:
This section is to determine which ports of the container to be in the folder (linked) to our local computer. In the above example it states that port 80 which inside the container will be maped into port 8080 on our computer.
volumes:
This section specifies the directories we will mount (connect) from the container to the directory on our computer.
Based on the above syntax example, we use the reference from Figure 6 above (see the Anatomy chapter of Dockerfile).
links
This section is to connect the first container with a second container named ardimysql.
working dir
This section specifies the home directory to be accessed for the first time when we access (can be analogous to access to ftp or ssh) into our container.
The "docker-compose up" command
Once we are done with the configuration file from Dockerfile and docker-compose.yml, run the command below in our terminal (command-line):
After that Docker will download all the components, configurations, and applications that we have configured in the two files.
It is advisable when executing these commands must use a sufficient internet connection, because this process may be felt for the first time.
After getting more or less results like below, it means the process is done.
After that open your favorite browser, then type http://localhost:8080 in its address bar.
At that time should have appeared your Drupal website installation page. After that run the installation process as usual.
Make sure to use settings for the database as below:
Database name: dbname
Database username: usrname
Database password: passwd
Database host: ardimysql
If we need to export a pre-existing database, we can use the command below:
Please run the command below to see a list of the containers already in our computer (after we run the docker-compose up command):
After that will get the results as below:
Then see the red circled section of the screenshot above. The name is the name of the container Docker that can be from the docker-compose.yml configuration that we have done before.
Posted on Utopian.io - Rewarding Open Source Contributors