Site icon Voina Blog (a tech warrior's blog)

Docker: Orchestrate multiple containers with docker-compose V2

Advertisements

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


# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
10.0.0.245:5000/test_app.v7               flat                53ac9d27fbb3        5 days ago          1.699 GB
docker.io/wnameless/oracle-xe-11g         latest              38037473cce1        9 weeks ago         2.234 GB

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:


# mkdir my_app_env
# cd my_app_env
# mkdir db
# mcedit Dockerfile

Paste as the content of the Dockerfile file the following:


FROM wnameless/oracle-xe-11g
ADD init.sql /docker-entrypoint-initdb.d/
CMD /usr/sbin/startup.sh && /usr/sbin/sshd -D

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:


# cd ..
# mkdir db
# mcedit init.sql

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


-- USER SQL
create user myuser IDENTIFIED by password default tablespace USERS temporary tablespace temp;
-- ROLES
GRANT "CONNECT" TO "myuser" ;
GRANT "RESOURCE" TO "myuser" ;

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

Create a new directory and then create Dockerfile configuration file:


# mkdir app
# mcedit Dockerfile

Paste as the content of the Dockerfile file the following:


FROM nexus:5555/my_app:v6
CMD /opt/testScripts/startAll.sh

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:


# cd ..
# mcedit docker-compose.yml

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


version: '2'

services:
    my_app:
       build: ./app
       ports:
         - "7000:7000"
         - "7002:7002"
         - "7006:7006"
         - "7032:7032" 
         - "7036:7036"
       links:
         - oracle:oracle
       stdin_open: true
       tty: true

    oracle:
       build: ./db
       ports:
         - "1521:1521"

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


# cd test_app
# docker-compose up

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


REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
10.0.0.245:5000/test_app.v7               flat                53ac9d27fbb3        5 days ago          1.699 GB
docker.io/wnameless/oracle-xe-11g         latest              38037473cce1        9 weeks ago         2.234 GB
my_app_envcomposev2_my_app                latest              a6f495d75046        15 hours ago        2.542 GB
my_app_envcomposev2_oracle                latest              2880b4ee4984        17 hours ago        2.234 GB

We can check out the running containers:


# docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                                                                                                                                NAMES
ce02c9b9f31b        my_app_envcomposev2_my_app   "/bin/sh -c /opt/test"   15 hours ago        Up 15 hours         4848/tcp, 0.0.0.0:7000->7000/tcp, 0.0.0.0:7002->7002/tcp, 7001/tcp, 0.0.0.0:7006->7006/tcp, 0.0.0.0:7032->7032/tcp, 7031/tcp, 8080/tcp, 8181/tcp, 9009/tcp, 0.0.0.0:7036->7036/tcp   my_app_envcomposev2_my_app_1
6a09af4ce7ae        my_app_envcomposev2_oracle   "/bin/sh -c '/usr/sbi"   17 hours ago        Up 15 hours         22/tcp, 8080/tcp, 0.0.0.0:1521->1521/tcp                                                                                                                                             my_app_envcomposev2_oracle_1
48c832c6333c        541a6732eadb            "/entrypoint.sh /etc/"   4 months ago        Up 17 hours         0.0.0.0:5000->5000/tcp

STEP 6: Start/Stop, rebuild the environment

To stop the containers created just call


# docker-compose stop

To start again the containers created just call


# docker-compose start

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


# docker-compose up --build

[paypal_donation_button]

Exit mobile version