DevOps

Docker Compose to Orchestrate Containers

Docker Orchestration using Fig showed how to defining and control a multi-container service using Fig. Since then, Fig has been renamed to Docker Compose, or Compose for short.

First release of Compose was announced recently:
 
 
 
 
 
 

From github.com/docker/compose:

Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.

Docker Compose uses the same API used by other Docker commands and tools.

techtip77-docker-compose

This Tech Tip will rewrite Docker Orchestration using Fig blog to use Docker Compose. In other words, it will show how to run a Java EE 7 application that is deployed using MySQL and WildFly.

Lets get started!

Install Docker Compose

Install Compose as:

curl -L https://github.com/docker/compose/releases/download/1.1.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Docker Compose Configuration File

Entry point to Compose is docker-compose.yml. To begin with, docker-compose tool also recognizes fig.yml file name but shows the following message:

fig.yml is deprecated and will not be supported in future. Please rename your config file to docker-compose.yml

And if both fig.yml and docker-compose.yml are available in the directory then the following message is shown:

Found multiple config files with supported names: docker-compose.yml, fig.yml
Using docker-compose.yml

Use the same configuration file from the previous blog and rename to docker-compose.yml:

mysqldb:
  image: mysql:latest
  environment:
    MYSQL_DATABASE: sample
    MYSQL_USER: mysql
    MYSQL_PASSWORD: mysql
    MYSQL_ROOT_PASSWORD: supersecret
mywildfly:
  image: arungupta/wildfly-mysql-javaee7
  links:
    - mysqldb:db
  ports:
    - 8080:8080

This YML-based configuration file has:

  1. Two containers defined by the name “mysqldb” and “mywildfly”
  2. Image names are defined using “image”
  3. Environment variables for the MySQL container are defined in “environment”
  4. MySQL container is linked with WildFly container using “links”
  5. Port forwarding is achieved using “ports”

Start, Verify, Stop Docker Containers

  1. All the containers can be started, in detached mode, by giving the command:
    docker-compose up -d

    And that shows the output as:

    Creating wildflymysqljavaee7_mysqldb_1...
    Creating wildflymysqljavaee7_mywildfly_1...
  2. Verify the containers as:
    docker-compose ps
                 Name                            Command               State                Ports               
    -------------------------------------------------------------------------------------------
    wildflymysqljavaee7_mysqldb_1     /entrypoint.sh mysqld --da ...   Up      3306/tcp                         
    wildflymysqljavaee7_mywildfly_1   /opt/jboss/wildfly/customi ...   Up      0.0.0.0:8080->8080/tcp, 9990/tcp
    
  3. Logs for the containers can be seen as:
    docker-compose logs

    And shows the output as:

    mywildfly_1 | => Starting WildFly server
    mywildfly_1 | => Waiting for the server to boot
    mywildfly_1 | =========================================================================
    mywildfly_1 | 
    mywildfly_1 |   JBoss Bootstrap Environment
    mywildfly_1 | 
    mywildfly_1 |   JBOSS_HOME: /opt/jboss/wildfly
    mywildfly_1 | 
    mywildfly_1 |   JAVA: /usr/lib/jvm/java/bin/java
    mywildfly_1 | 
    mywildfly_1 |   JAVA_OPTS:  -server -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true
    
    
    . . .
    
    
    mywildfly_1 | 18:43:38,449 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: INSERT INTO EMPLOYEE_SCHEMA(ID, NAME) VALUES (6, 'Raj')
    mywildfly_1 | 18:43:38,452 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: INSERT INTO EMPLOYEE_SCHEMA(ID, NAME) VALUES (7, 'Howard')
    mywildfly_1 | 18:43:38,455 INFO  [stdout] (ServerService Thread Pool -- 50) Hibernate: INSERT INTO EMPLOYEE_SCHEMA(ID, NAME) VALUES (8, 'Priya')
    mywildfly_1 | 18:43:39,714 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-9) Deploying javax.ws.rs.core.Application: class org.javaee7.samples.employees.MyApplication
    mywildfly_1 | 18:43:39,751 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-9) JBAS017534: Registered web context: /employees
    mywildfly_1 | 18:43:39,805 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "employees.war" (runtime-name : "employees.war")
    mywildfly_1 | 18:43:39,828 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
    mywildfly_1 | 18:43:39,828 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
    mywildfly_1 | 18:43:39,829 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 8393ms - Started 280 of 334 services (92 services are lazy, passive or on-demand)
    
  4. Find the IP address of the host as:
    boot2docker ip

    And access the application as:

    curl http://192.168.59.103:8080/employees/resources/employees/

    To see the output as:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><employee><id>1</id><name>Penny</name></employee><employee><id>2</id><name>Sheldon</name></employee><employee><id>3</id><name>Amy</name></employee><employee><id>4</id><name>Leonard</name></employee><employee><id>5</id><name>Bernadette</name></employee><employee><id>6</id><name>Raj</name></employee><employee><id>7</id><name>Howard</name></employee><employee><id>8</id><name>Priya</name></employee></collection>
    

    Or in the browser as:

    techtip77-browser-output

  5. Stop the containers as:
    docker-compose stop

    to see the output as:

    Stopping wildflymysqljavaee7_mywildfly_1...
    Stopping wildflymysqljavaee7_mysqldb_1...
    

Docker Compose Commands

Complete list of Docker Compose commands can be seen by typing docker-compose and shows the output as:

velopment environments using Docker.

Usage:
  docker-compose [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  --verbose                 Show more output
  --version                 Print version and exit
  -f, --file FILE           Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME   Specify an alternate project name (default: directory name)

Commands:
  build     Build or rebuild services
  help      Get help on a command
  kill      Kill containers
  logs      View output from containers
  port      Print the public port for a port binding
  ps        List containers
  pull      Pulls service images
  rm        Remove stopped containers
  run       Run a one-off command
  scale     Set number of containers for a service
  start     Start services
  stop      Stop services
  restart   Restart services
  up        Create and start containers

A subsequent blog will likely play with scale command.

Help for each command is shown by typing -h after the command name. For example, help for run command is shown as:

docker-compose run -h
Run a one-off command on a service.

For example:

    $ docker-compose run web python manage.py shell

By default, linked services will be started, unless they are already
running. If you do not want to start linked services, use
`docker-compose run --no-deps SERVICE COMMAND [ARGS...]`.

Usage: run [options] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

Options:
    --allow-insecure-ssl  Allow insecure connections to the docker
                          registry
    -d                    Detached mode: Run container in the background, print
                          new container name.
    --entrypoint CMD      Override the entrypoint of the image.
    -e KEY=VAL            Set an environment variable (can be used multiple times)
    --no-deps             Don't start linked services.
    --rm                  Remove container after run. Ignored in detached mode.
    --service-ports       Run command with the service's ports enabled and mapped
                          to the host.
    -T                    Disable pseudo-tty allocation. By default `docker-compose run`
                          allocates a TTY.

Enjoy!

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button