Orchestrate containers using Docker compose

This tutorial shows how you can use Docker compose tool to manage multiple contains from a single YAML configuration file.

Managing multiple containers individually can soon become troublesome if you have lots of containers. For a simpler Docker administration you can use a more advanced tool like docker­compose which lets you manage multiple containers from a single configuration file.

Behind the scenes, the docker­compose tool leverages the Docker engine for pulling images, building the images, starting the containers in a correct order, and making the right links among the containers and services.

Install Docker Compose

You can install Docker compose by pulling it from Docker’s github repoository with the following script:

$ curl -L https://github.com/docker/compose/releases/download/1.5.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Next, make sure that the docker­compose has the right permissions and then start it:

$ chmod +x /usr/local/bin/docker­compose

The docker­compose tool orchestrates containers using the docker­compose.yml configuration file. This file defines the services to be executed, the relationships between them and their runtime properties. You need to use the YAML syntax in this file, which specifies for each service a list of keys:values; hence it is an human readable file.

Let’s see how we can configure the connection between WildFly and MySQL using the following docker­compose.yml configuration file:

  image: mysql:latest
    MYSQL_DATABASE: sampledb
    MYSQL_USER: mysql
  image: jboss/wildfly
    ­ mysqldb:db
    ­ 8080:8080
    ­ 9990:9990

The content of this file is pretty readable. It contains two Docker images, described using the YAML format. You can start docker­compose in detached mode as follows:

docker­compose up ­-d

Some logs will be emitted to inform you that the images are getting started:

Recreating wildflymysql_mysqldb_1...
Creating jboss/wildfly_wildfly_1...

Now you can check that the two Docker process are active:

$ sudo docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED     
STATUS              PORTS                                            NAMES
f3710f16e747        wildfly­mysql:latest   "/opt/jboss/wildfly/b"   3 minutes 
ago       Up 3 minutes­>8080/tcp,­>9990/tcp   
6cdb1f6dd9ee        mysql:latest           "/entrypoint.sh mysql"   3 minutes 
ago       Up 3 minutes        3306/tcp                                         

You can now test connecting the application server to MySQL, just remember to check the IP address which has been assigned to the new mysql image:

$ sudo docker inspect ­f '{{ .NetworkSettings.IPAddress }}' 6cdb1f6dd9ee