Docker 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.
An application using Docker containers will typically consist of multiple containers. With Docker Compose, there is no need to write shell scripts to start your containers. All the containers are defined in a configuration file using services, and then docker-compose
script is used to start, stop, and restart the application and all the services in that application, and all the containers within that service. The complete list of commands is:
Command | Purpose |
---|---|
|
Build or rebuild services |
|
Get help on a command |
|
Kill containers |
|
View output from containers |
|
Print the public port for a port binding |
|
List containers |
|
Pulls service images |
|
Restart services |
|
Remove stopped containers |
|
Run a one-off command |
|
Set number of containers for a service |
|
Start services |
|
Stop services |
|
Create and start containers |
Docker Compose applications can use “classic” container linking that is confined to a single host. Alternatively, it can use multi-host networking introduced in Docker 1.9.
The application used in this section will show how to query a Couchbase sample data using simple Java EE application deployed on WildFly. The Java EE application will use JAX-RS to publish REST endpoint which will then be invoked using curl
.
-
Entry point to Compose is
docker-compose.yml
. Lets use the following file:mycouchbase: container_name: "db" image: arungupta/couchbase ports: - 8091:8091 - 8092:8092 - 8093:8093 - 11210:11210 mywildfly: image: arungupta/wildfly-couchbase-javaee7 environment: - COUCHBASE_URI=db ports: - 8080:8080
This file is available in https://github.com/javaee-samples/docker-java/raw/master/attendees/docker-compose.yml and shows:
-
Two services in this Compose are defined by the name
mycouchbase
andmywildfly
-
Image name for each service defined using
image
-
arungupta/couchbase
starts Couchbase server, configures it using Couchbase REST API, and loads a sample bucket -
arungupta/wildfly-couchbase-javaee7
starts WildFly and deploys application WAR file built from https://github.com/arun-gupta/couchbase-javaee. This image built using https://github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7/Dockerfile.
-
-
Environment variable for WildFly container is defined in
environment
-
Couchbase container is linked with WildFly container using
COUCHBASE_URI
. This is used in the application code as shown at https://github.com/arun-gupta/couchbase-javaee/blob/master/src/main/java/org/couchbase/sample/javaee/Database.java#L44. -
Port forwarding is achieved using
ports
-
-
All services can be started, in detached mode, by giving the command:
docker-compose --x-networking up -d
--x-networking
will create a bridge network. Docker 1.9 is required to run this application.And this shows the output as:
docker-compose --x-networking up -d Creating network "attendees" with driver "None" Creating attendees_mywildfly_1 Creating db
An alternate compose file name can be specified using
-f
.An alternate directory where the compose file exists can be specified using
-p
. -
Started services can be verified as:
docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------------------------- attendees_mywildfly_1 /opt/jboss/wildfly/bin/sta ... Up 0.0.0.0:8080->8080/tcp, 0.0.0.0:9990->9990/tcp db /entrypoint.sh /opt/couchb ... Up 11207/tcp, 0.0.0.0:11210->11210/tcp, 11211/tcp, 18091/tcp, 18092/tcp, 0.0.0.0:8091->8091/tcp, 0.0.0.0:8092->8092/tcp, 0.0.0.0:8093->8093/tcp
This provides a consolidated view of all the services started, and containers within them.
Alternatively, the containers in this application, and any additional containers running on this Docker host can be verified by using the usual
docker ps
command:docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a0435413a3b arungupta/couchbase "/entrypoint.sh /opt/" About a minute ago Up About a minute 0.0.0.0:8091-8093->8091-8093/tcp, 11207/tcp, 11211/tcp, 0.0.0.0:11210->11210/tcp, 18091-18092/tcp db be57cb3d1ad6 arungupta/wildfly-couchbase-javaee7 "/opt/jboss/wildfly/b" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp, 0.0.0.0:9990->9990/tcp attendees_mywildfly_1
-
Service logs can be seen as:
docker-compose logs Attaching to db, attendees_mywildfly_1 db | Starting Couchbase Server -- Web UI available at http://<ip>:8091 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 -XX:+UseCompressedOops -server -XX:+UseCompressedOops -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true mywildfly_1 | mywildfly_1 | ========================================================================= mywildfly_1 | mywildfly_1 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 mywildfly_1 | 01:06:57,381 INFO [org.jboss.modules] (main) JBoss Modules version 1.4.3.Final mywildfly_1 | 01:06:57,842 INFO [org.jboss.msc] (main) JBoss MSC version 1.2.6.Final mywildfly_1 | 01:06:57,938 INFO [org.jboss.as] (MSC service thread 1-2) WFLYSRV0049: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) starting mywildfly_1 | 01:07:03,068 INFO [org.jboss.as.controller.management-deprecated] (ServerService Thread Pool -- 3) WFLYCTL0028: Attribute 'job-repository-type' in the resource at address '/subsystem=batch' is deprecated, and may be removed in future version. See the attribute description in the output of the read-resource-description operation to learn more about the deprecation. mywildfly_1 | 01:07:03,088 INFO [org.jboss.as.controller.management-deprecated] (ServerService Thread Pool -- 7) WFLYCTL0028: Attribute 'enabled' in the resource at address '/subsystem=datasources/data-source=ExampleDS' is deprecated, and may be removed in future version. See the attribute description in the output of the read-resource-description operation to learn more about the deprecation. mywildfly_1 | 01:07:03,870 INFO [org.jboss.as.repository] (ServerService Thread Pool -- 24) WFLYDR0001: Content added at location /opt/jboss/wildfly/standalone/data/content/b6/5e987fe57e1d90e1414ba1c90e542f6311436d/content mywildfly_1 | 01:07:03,936 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http) mywildfly_1 | 01:07:04,064 INFO [org.xnio] (MSC service thread 1-1) XNIO version 3.3.1.Final mywildfly_1 | 01:07:04,160 INFO [org.xnio.nio] (MSC service thread 1-1) XNIO NIO Implementation Version 3.3.1.Final mywildfly_1 | 01:07:04,463 INFO [org.wildfly.extension.io] (ServerService Thread Pool -- 37) WFLYIO001: Worker 'default' has auto-configured to 2 core threads with 16 task threads based on your 1 available processors mywildfly_1 | 01:07:04,418 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem. mywildfly_1 | 01:07:04,562 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension mywildfly_1 | 01:07:04,612 INFO [org.jboss.as.jsf] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main] mywildfly_1 | 01:07:04,567 INFO [org.jboss.as.security] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem mywildfly_1 | 01:07:04,567 WARN [org.jboss.as.txn] (ServerService Thread Pool -- 54) WFLYTX0013: Node identifier property is set to the default value. Please make sure it is unique. mywildfly_1 | 01:07:04,593 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem mywildfly_1 | 01:07:04,881 INFO [org.jboss.remoting] (MSC service thread 1-1) JBoss Remoting version 4.0.9.Final mywildfly_1 | 01:07:05,081 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0003: Undertow 1.2.9.Final starting mywildfly_1 | 01:07:05,207 INFO [org.jboss.as.connector] (MSC service thread 1-2) WFLYJCA0009: Starting JCA Subsystem (IronJacamar 1.2.5.Final) mywildfly_1 | 01:07:05,277 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3) mywildfly_1 | 01:07:05,315 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0003: Undertow 1.2.9.Final starting mywildfly_1 | 01:07:05,686 INFO [org.jboss.as.security] (MSC service thread 1-2) WFLYSEC0001: Current PicketBox version=4.9.2.Final mywildfly_1 | 01:07:05,954 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0018: Started Driver service with driver-name = h2 mywildfly_1 | 01:07:05,938 INFO [org.jboss.as.naming] (MSC service thread 1-1) WFLYNAM0003: Starting Naming Service mywildfly_1 | 01:07:06,130 INFO [org.jboss.as.mail.extension] (MSC service thread 1-1) WFLYMAIL0001: Bound mail session [java:jboss/mail/Default] mywildfly_1 | 01:07:06,399 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0014: Creating file handler for path /opt/jboss/wildfly/welcome-content mywildfly_1 | 01:07:06,888 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0012: Started server default-server. mywildfly_1 | 01:07:07,303 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0006: Undertow HTTP listener default listening on /0.0.0.0:8080 mywildfly_1 | 01:07:07,322 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0018: Host default-host starting mywildfly_1 | 01:07:08,014 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS] mywildfly_1 | 01:07:08,430 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-2) WFLYDS0013: Started FileSystemDeploymentService for directory /opt/jboss/wildfly/standalone/deployments mywildfly_1 | 01:07:08,489 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0027: Starting deployment of "airlines.war" (runtime-name: "airlines.war") mywildfly_1 | 01:07:08,605 INFO [org.jboss.ws.common.management] (MSC service thread 1-1) JBWS022052: Starting JBoss Web Services - Stack CXF Server 5.0.0.Final mywildfly_1 | 01:07:11,430 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) WFLYWELD0003: Processing weld deployment airlines.war mywildfly_1 | 01:07:11,730 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-1) HV000001: Hibernate Validator 5.1.3.Final db | * Trying 127.0.0.1... db | % Total % Received % Xferd Average Speed Time Time Time Current db | Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0) db | > POST /pools/default HTTP/1.1 db | > User-Agent: curl/7.40.0-DEV db | > Host: 127.0.0.1:8091 db | > Accept: */* db | > Content-Length: 36 db | > Content-Type: application/x-www-form-urlencoded db | > db | } [36 bytes data] db | * upload completely sent off: 36 out of 36 bytes db | < HTTP/1.1 200 OK db | < Server: Couchbase Server db | < Pragma: no-cache db | < Date: Wed, 23 Dec 2015 01:07:11 GMT db | < Content-Length: 0 db | < Cache-Control: no-cache db | < 100 36 0 0 100 36 0 690 --:--:-- --:--:-- --:--:-- 765 db | * Connection #0 to host 127.0.0.1 left intact db | * Trying 127.0.0.1... db | % Total % Received % Xferd Average Speed Time Time Time Current db | Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0) db | > POST /node/controller/setupServices HTTP/1.1 db | > User-Agent: curl/7.40.0-DEV db | > Host: 127.0.0.1:8091 db | > Accept: */* db | > Content-Length: 26 db | > Content-Type: application/x-www-form-urlencoded db | > db | } [26 bytes data] db | * upload completely sent off: 26 out of 26 bytes db | < HTTP/1.1 200 OK db | < Server: Couchbase Server db | < Pragma: no-cache db | < Date: Wed, 23 Dec 2015 01:07:11 GMT db | < Content-Length: 0 db | < Cache-Control: no-cache db | < 100 26 0 0 100 26 0 348 --:--:-- --:--:-- --:--:-- 448 db | * Connection #0 to host 127.0.0.1 left intact db | * Trying 127.0.0.1... db | % Total % Received % Xferd Average Speed Time Time Time Current db | Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0) db | > POST /settings/web HTTP/1.1 db | > User-Agent: curl/7.40.0-DEV db | > Host: 127.0.0.1:8091 db | > Accept: */* db | > Content-Length: 50 db | > Content-Type: application/x-www-form-urlencoded db | > db | } [50 bytes data] db | * upload completely sent off: 50 out of 50 bytes db | < HTTP/1.1 200 OK db | < Server: Couchbase Server db | <{"newBaseUri":"http://127.0.0.1:8091/"} Pragma: no-cache db | < Date: Wed, 23 Dec 2015 01:07:12 GMT db | < Content-Type: application/json db | < Content-Length: 39 db | < Cache-Control: no-cache db | < db | { [39 bytes data] 100 89 100 39 100 50 268 344 --:--:-- --:--:-- --:--:-- 420 db | * Connection #0 to host 127.0.0.1 left intact db | * Trying 127.0.0.1... db | % Total % Received % Xferd Average Speed Time Time Time Current db | Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0) db | * Server auth using Basic with user 'Administrator' db | > POST /sampleBuckets/install HTTP/1.1 db | > Authorization: Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA== db | > User-Agent: curl/7.40.0-DEV db | > Host: 127.0.0.1:8091 db | > Accept: */* db | > Content-Length: 17 db | > Content-Type: application/x-www-form-urlencoded db | > db | } [17 bytes data] db | * upload completely sent off: 17 out of 17 bytes mywildfly_1 | 01:07:12,169 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named Database in deployment unit deployment "airlines.war" are as follows: mywildfly_1 | mywildfly_1 | java:global/airlines/Database!org.couchbase.sample.javaee.Database mywildfly_1 | java:app/airlines/Database!org.couchbase.sample.javaee.Database mywildfly_1 | java:module/Database!org.couchbase.sample.javaee.Database mywildfly_1 | java:global/airlines/Database mywildfly_1 | java:app/airlines/Database mywildfly_1 | java:module/Database mywildfly_1 | db | < HTTP/1.1 202 Accepted db | < Server: Couchbase Server db | < Pragma: no-cache db | < Date: Wed, 23 Dec 2015 01:07:12 GMT db | < Content-Type: application/json db | < Content-Length: 2 db | < Cache-Control: no-cache db | < 89 19 0 0 100 17 0 40 --:--:-- --:--:-- --:--:-- 46{ [2 b[]ytes data] 100 19 100 2 100 17 4 40 --:--:-- --:--:-- --:--:-- 45 db | * Connection #0 to host 127.0.0.1 left intact db | /entrypoint.sh couchbase-server mywildfly_1 | 01:07:13,117 INFO [org.jboss.weld.deployer] (MSC service thread 1-2) WFLYWELD0006: Starting Services for CDI deployment: airlines.war mywildfly_1 | 01:07:13,333 INFO [org.jboss.weld.Version] (MSC service thread 1-2) WELD-000900: 2.2.16 (SP1) mywildfly_1 | 01:07:13,609 INFO [org.jboss.weld.deployer] (MSC service thread 1-2) WFLYWELD0009: Starting weld service for deployment airlines.war mywildfly_1 | 01:07:17,929 INFO [com.couchbase.client.core.env.CoreEnvironment] (ServerService Thread Pool -- 65) ioPoolSize is less than 3 (1), setting to: 3 mywildfly_1 | 01:07:17,931 INFO [com.couchbase.client.core.env.CoreEnvironment] (ServerService Thread Pool -- 65) computationPoolSize is less than 3 (1), setting to: 3 mywildfly_1 | 01:07:18,389 INFO [com.couchbase.client.core.CouchbaseCore] (ServerService Thread Pool -- 65) CouchbaseEnvironment: {sslEnabled=false, sslKeystoreFile='null', sslKeystorePassword='null', queryEnabled=false, queryPort=8093, bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, bootstrapHttpDirectPort=8091, bootstrapHttpSslPort=18091, bootstrapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=3, computationPoolSize=3, responseBufferSize=16384, requestBufferSize=16384, kvServiceEndpoints=1, viewServiceEndpoints=1, queryServiceEndpoints=1, ioPool=NioEventLoopGroup, coreScheduler=CoreScheduler, eventBus=DefaultEventBus, packageNameAndVersion=couchbase-jvm-core/1.2.1 (git: 1.2.1), dcpEnabled=false, retryStrategy=BestEffort, maxRequestLifetime=75000, retryDelay=ExponentialDelay{growBy 1.0 MICROSECONDS; lower=100, upper=100000}, reconnectDelay=ExponentialDelay{growBy 1.0 MILLISECONDS; lower=32, upper=4096}, observeIntervalDelay=ExponentialDelay{growBy 1.0 MICROSECONDS; lower=10, upper=100000}, keepAliveInterval=30000, autoreleaseAfter=2000, bufferPoolingEnabled=true, tcpNodelayEnabled=true, mutationTokensEnabled=false, socketConnectTimeout=1000, queryTimeout=75000, viewTimeout=75000, kvTimeout=2500, connectTimeout=5000, disconnectTimeout=25000, dnsSrvEnabled=false} mywildfly_1 | 01:07:20,059 INFO [com.couchbase.client.core.node.Node] (cb-io-1-1) Connected to Node db mywildfly_1 | 01:07:21,273 INFO [com.couchbase.client.core.config.ConfigurationProvider] (cb-computations-3) Opened bucket travel-sample mywildfly_1 | 01:07:24,932 INFO [org.jboss.resteasy.spi.ResteasyDeployment] (ServerService Thread Pool -- 65) Deploying javax.ws.rs.core.Application: class org.couchbase.sample.javaee.MyApplication mywildfly_1 | 01:07:25,193 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 65) WFLYUT0021: Registered web context: /airlines mywildfly_1 | 01:07:25,440 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "airlines.war" (runtime-name : "airlines.war") mywildfly_1 | 01:07:25,995 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management mywildfly_1 | 01:07:25,997 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990 mywildfly_1 | 01:07:25,999 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) started in 29405ms - Started 313 of 493 services (221 services are lazy, passive or on-demand)
The log shows:
-
WildFly Application server has started
-
Couchbase is configured using the REST API
-
airlines.war
application is deployed
Access the application by invoking the REST API using curl
:
curl http://dockerhost:8080/airlines/resources/airline
[{"travel-sample":{"country":"United States","iata":"Q5","callsign":"MILE-AIR","name":"40-Mile Air","icao":"MLA","id":10,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"TQ","callsign":"TXW","name":"Texas Wings","icao":"TXW","id":10123,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"A1","callsign":"atifly","name":"Atifly","icao":"A1F","id":10226,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":null,"callsign":null,"name":"Jc royal.britannica","icao":"JRB","id":10642,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"ZQ","callsign":"LOCAIR","name":"Locair","icao":"LOC","id":10748,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"K5","callsign":"SASQUATCH","name":"SeaPort Airlines","icao":"SQH","id":10765,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"KO","callsign":"ACE AIR","name":"Alaska Central Express","icao":"AER","id":109,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":"5W","callsign":"FLYSTAR","name":"Astraeus","icao":"AEU","id":112,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"UU","callsign":"REUNION","name":"Air Austral","icao":"REU","id":1191,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","id":1203,"type":"airline"}}]
Complete set of REST APIs for this application are explained at https://github.com/arun-gupta/couchbase-javaee.
Stop the services as:
docker-compose stop
Stopping db ... done
Stopping attendees_mywildfly_1 ... done
Stop the services as:
docker-compose rm -f
Going to remove db, attendees_mywildfly_1
Removing db ... done
Removing attendees_mywildfly_1 ... done