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
Announcing @docker compose https://t.co/jJNvzbYCyk by @aanand @bfirsh & community!
— Docker (@docker) February 26, 2015
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.
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:
1
2
3
4
|
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:
1
2
3
|
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:
1
2
3
4
|
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
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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:
- Two containers defined by the name “mysqldb” and “mywildfly”
- Image names are defined using “image”
- Environment variables for the MySQL container are defined in “environment”
- MySQL container is linked with WildFly container using “links”
- Port forwarding is achieved using “ports”
Start, Verify, Stop Docker Containers
- All the containers can be started, in detached mode, by giving the command:
123docker-compose up -d
1234Creating wildflymysqljavaee7_mysqldb_1...Creating wildflymysqljavaee7_mywildfly_1... - Verify the containers as:
1234567docker-compose psName Command State Ports-----------------------------------------------------------------------------------------------------------wildflymysqljavaee7_mysqldb_1 /entrypoint.sh mysqld --da ... Up 3306/tcpwildflymysqljavaee7_mywildfly_1 /opt/jboss/wildfly/customi ... Up 0.0.0.0:8080->8080/tcp, 9990/tcp - Logs for the containers can be seen as:
123docker-compose logs
123456789101112131415161718192021222324252627mywildfly_1 | => Starting WildFly servermywildfly_1 | => Waiting for the server to bootmywildfly_1 | =========================================================================mywildfly_1 |mywildfly_1 | JBoss Bootstrap Environmentmywildfly_1 |mywildfly_1 | JBOSS_HOME: /opt/jboss/wildflymywildfly_1 |mywildfly_1 | JAVA: /usr/lib/jvm/java/bin/javamywildfly_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.MyApplicationmywildfly_1 | 18:43:39,751 INFO [org.wildfly.extension.undertow] (MSC service thread 1-9) JBAS017534: Registered web context: /employeesmywildfly_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/managementmywildfly_1 | 18:43:39,828 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990mywildfly_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) - Find the IP address of the host as:
123boot2docker ip
123curl http://192.168.59.103:8080/employees/resources/employees/
123<?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> - Stop the containers as:
123docker-compose stop
to see the output as:
1234Stopping 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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!
Nice tip!
Is it possible to use pipework with Docker-compose?
Seems like a pending issue at: https://github.com/docker/compose/issues/1003
Question.. How do i use cid=$(ifconfig eth0 | grep ‘inet addr:’ | cut -d: -f2…) inside a docker-compose.yml file?
Hariharan,
Not sure if this is possible, file a bug?
i get troubleshooting in running [root@ix1-dv-c6-survey-01 javaee7-arungupta]# docker run arungupta/wildfly-mysql-javaee7
/bin/bash: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: Permission denied.
I am on centOS 6.7, SELinux disabled (https://github.com/docker/docker/issues/7318)
uname -a
Linux 020e23a319a9 2.6.32-573.3.1.el6.x86_64 #1 SMP Thu Aug 13 22:55:16 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
mount | grep ‘ / ‘
/dev/mapper/VG0-lv_root on / type ext4 (rw)
[root@ix1-dv-c6-survey-01 javaee7-arungupta]# docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d
OS/Arch (server): linux/amd64
Are you creating your own images? Or using the ones that already exist?