Simplifying service environment with Docker

This article was made to helps developers building a simple infrastructure in its cycle for develop an app.

Augusto Giles
Mobix Software Studio Reports
4 min readOct 30, 2018

--

At the end of this post you are going to be able to create a simple environment with the following containers:

  • MongoDB;
  • Elasticsearch;
  • Kibana;

The problem

For each product development, a stack of infrastructure service is needed (database, message queue service, search engine…). To build this infrastructure in a fast, organised and simple way, Docker would be the perfect solution!

What is Docker?

Docker is an open source designed to make easier create, deploy, and run apps in an isolated environment, assuring though a fast availability of services for a developer. But here we’re going a little beyond, using docker compose, which allows an orchestration of multiple containers just through a single configuration file.

Compose file

The compose file or docker-compose.yml file is where all containers and its configurations are declared. Moreover, it’s possible to create subnetworks, external mapping volumes among a lot of other possible configurations which can be better described on the official website.

MongoDB Container

We're going to start by Mongo DB:

In the 1st line above, the service name is declared. Then, you can declare which image you want to use, in this case I'm using a image available at dockerhub. You can also define a name for your container in the container_name tag. With the environment tag you can declare some environment variables in your service context. Lines 8 and 9 are optional variables for mongo authentication.

In volume, it's possible to link all files or folders that you may want to persist, independent of removing containers. In this case (described above), a link was done to persist all created databases.

The port tag allows you to bind a container port with a local port. (<local-port>:<container-port>)

The command tag allows you to overwrite the default command to execute the service in container. You can define one o more commands to be executed at startup.

ElasticSearch Container

In the following section an elasticsearch container is defined to a develop environment. The most tags presented below in the config file were explained before.

There were some different informations in the code above. The read-only file elasticsearch.yml, defined by :ro reserved word; the definition of a hostname and a docker intranet with networks tag. Also, the definition of a hostname is useful for communication among containers in a network. Further, there is no need to concern about what IP each container assigned because you can refer to a container for your container_name. When you declare a network in the container, you need to declare a driver for that network at the end of the file.

Container Kibana

The definition of this container is very similar to the elasticsearch container.

Note that we already have defined the same subnet both for kibana and elasticsearch containers. The depends_on variable is used for describe which containers the current container depends to be executed.

For more information about docker with elasticsearch and kibana, check the elastic documentation.

The complete code

How to execute

--

--