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


# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
10.0.0.245:5000/test_app.v7       flat                53ac9d27fbb3        5 days ago          1.699 GB
10.0.0.245:5000/test_oracle       latest              d3d172c29088        6 days ago          2.227 GB

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:


# mkdir test_app
# mcedit docker-compose.yml

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


test_app:
  image: "10.0.0.245:5000/test_app.v7:flat"
  ports:
    - "7000:7000"
    - "7002:7002"
    - "7006:7006"
    - "7032:7032"
    - "7036:7036"
  links:
    - oracle:oracle
  command: /bin/sh -c /opt/testScripts/startAll.sh
  stdin_open: true
  tty: true

oracle:
  image: "10.0.0.245:5000/test_oracle"
  ports:
    - "1521:1521"

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


# cd test_app
# docker-compose up

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


# docker-compose stop

To start again the containers created at STEP 3 just call


# docker-compose start

[paypal_donation_button]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.