Docker: Orchestrate multiple containers with docker-compose V2

By | February 9, 2017

This is post in continuation of Docker: Orchestrate multiple containers with docker-compose

The same application environment is specified using the docker-compose V2 specification. This looks very similar but with a simplified syntax and some extra features.

STEP 1: List the existing images

We can see that we already have two images with our test application based on Glassfish application container and a copy of the official oracle 11g image.

STEP 2: Create the Dockerfile configuration file for the DB.

Create a new directory for the environment “my_app_env” and under it another for DB files “db” and in it then create Dockerfile configuration file:

Paste as the content of the Dockerfile file the following:

where:
FROM = specifies the base image we are using to create our custom image
ADD = add aditional data to the base image in the specified location
CMD = add a custom command used to start-up a container from this image

Then create the database initialization script:

Here we can create tablespaces, add users, import data etc.

STEP 3: Create the Dockerfile configuration file for the APP.

Create a new directory and then create Dockerfile configuration file:

Paste as the content of the Dockerfile file the following:

where:
FROM = specifies the base image we are using to create our custom image
CMD = add a custom command used to start-up a container from this image

STEP 4: Create the docker-compose.yml configuration file for the application environment.

Create a new directory and then create Dockerfile configuration file:

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

where:
version: tell the docker-compose this is a version 2 yml file
my_app: the name we give to the application container
build: the directory where the image build files reside. Here we usually have the Dockerfile and all the additional files needed to build the image
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 referred in the above –link option.
build: the directory where the image build files reside. Here we usually have the Dockerfile and all the additional files needed to build the image
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 my_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 5: Initialize the environment

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

We can check out the running containers:

STEP 6: Start/Stop, rebuild the environment

To stop the containers created just call

To start again the containers created just call

In case changes are added to rebuild again the images and create new containers just call

Advertisements