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.
-
Entry point to Compose is
docker-compose.yml
. Lets use the following file:mysqldb: image: mysql 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 file is available in https://github.com/javaee-samples/docker-java/raw/master/attendees/docker-compose.yml and shows:
-
Two services defined by the name
mysqldb
andmywildfly
-
Image name for each service 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
-
-
All services can be started, in detached mode, by giving the command:
docker-compose up -d
And this shows the output as:
Creating attendees_mysqldb_1... Creating attendees_mywildfly_1...
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_mysqldb_1 /entrypoint.sh mysqld Up 3306/tcp attendees_mywildfly_1 /opt/jboss/wildfly/customi ... Up 0.0.0.0:8080->8080/tcp, 9990/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 3598e545bd2f arungupta/wildfly-mysql-javaee7:latest "/opt/jboss/wildfly/ 59 seconds ago Up 58 seconds 0.0.0.0:8080->8080/tcp, 9990/tcp attendees_mywildfly_1 b8cf6a3d518b mysql:latest "/entrypoint.sh mysq 2 minutes ago Up 2 minutes 3306/tcp attendees_mysqldb_1
-
Service logs can be seen as:
> docker-compose logs Attaching to attendees_mywildfly_1, attendees_mysqldb_1 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 | . . . mywildfly_1 | 15:40:20,866 INFO [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-2) Deploying javax.ws.rs.core.Application: class org.javaee7.samples.employees.MyApplication mywildfly_1 | 15:40:20,914 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017534: Registered web context: /employees mywildfly_1 | 15:40:21,032 INFO [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "employees.war" (runtime-name : "employees.war") mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990 mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 9572ms - Started 280 of 334 services (92 services are lazy, passive or on-demand) mysqldb_1 | Running mysql_install_db mysqldb_1 | 2015-06-05 15:38:31 0 [Note] /usr/sbin/mysqld (mysqld 5.6.25) starting as process 27 ... mysqldb_1 | 2015-06-05 15:38:31 27 [Note] InnoDB: Using atomics to ref count buffer pool pages . . . mysqldb_1 | 2015-06-05 15:38:40 1 [Note] Event Scheduler: Loaded 0 events mysqldb_1 | 2015-06-05 15:38:40 1 [Note] mysqld: ready for connections. mysqldb_1 | Version: '5.6.25' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) mysqldb_1 | 2015-06-05 15:40:18 1 [Warning] IP address '172.17.0.24' could not be resolved: Name or service not known
-
Access the application at http://dockerhost:8080/employees/resources/employees/. This is shown in the browser as:
Stop the services as:
docker-compose stop Stopping attendees_mywildfly_1... Stopping attendees_mysqldb_1...
Warning
|
Stopping and starting the containers again will give the following error:
This is expected because the JDBC resource is created during every run of the container. In a real-world application, this would be pre-baked in the configuration already. |
Stop the services as:
docker-compose rm -f Going to remove attendees_mywildfly_1, attendees_mysqldb_1 Removing attendees_mywildfly_1... done Removing attendees_mysqldb_1... done
Start a Java EE application with WildFly and MySQL containers.
-
Create a new directory and name it
wildfly-mysql-javaee7
. -
Create a new file
docker-compose.yml
and copy the contents from the following:mysqldb: container_name: "db" image: mysql:latest environment: MYSQL_DATABASE: sample MYSQL_USER: mysql MYSQL_PASSWORD: mysql MYSQL_ROOT_PASSWORD: supersecret mywildfly: image: arungupta/wildfly-mysql-javaee7 environment: - MYSQL_URI=db:3306 ports: - 8080:8080
-
Run the application as:
docker-compose --x-networking up -d
--x-networking
will create a bridge network. Docker 1.9 is required to run this application.