Docker: Orchestrate multiple containers with docker-compose

By | November 8, 2016

In an enterprise environment we usually separate the components in independent containers:
– a database container
– a business application container
– a web-layer application container
– a load balancing layer container/containers
– etc.
When running our environment with docker and if the environment consist of something more than some simple tests, at some point we are going to end up with a concert of containers running together.
The easiest way to coordinate the instantiation of the containers of the environment and link them together is by using docker-compose. See the official documentation page Docker Compose

In the following I am going to orchestrate the creation of an environment containing a Glassfish based application linked to a custom-made oracle database instance.

STEP 1: List the existing images

We can see that we already have two custom images with our test application based on Glassfish application container and an oracle 11g image.

STEP 2: Create the docker-compose configuration file.

Create a new directory and then create docker-compose.yml configuration file:

Paste as the content of the docker-compose.yml file following:

where:

test_app: the name we give to the application container
image: the image from which the container is going to be instantiated
ports: mapping of internal ports to the host ports. This is equivalent of -p from docker run command
links: link to a dependency container. This is the equivalent of the –link from docker run command
command: this is the command line used to start the container. Note that we have here a call to a shell script where we start several Glassfish domains and other applications.
stdin_open: true = this starts interactive mode. This is the equivalent of the -i from docker run command
tty: true = this allocates a terminal. This is the equivalent of the -t from docker run command

oracle: the name given to the DB container. Note that this is the name refered in the above –link option.
image: the image from which the container is going to be instantiated
ports: mapping of internal ports to the host ports. This is equivalent of -p from docker run command. It is not necessary to map this port for test_app to be able to access it. We are doing this mapping just to be able to connect from the host directly to the DB with a client like sqldeveloper or toad.

STEP 3: Initialize the environment

The docker-compose will create two new containers and start them up.

STEP 4: Start/Stop the environment

To stop the containers created at STEP 3 just call

To start again the containers created at STEP 3 just call

Advertisements