About Raquel Pau

Raquel has graduated from Computer Engineering in the Universitat Politecnica de Catalunya. She also holds a Master degree in Computation (Software Engineering) from the same university. She has an I+D profile focused about model driven development. Additionally, she is the project leader of Walkmod, an open source project to code conventions, which is part of the result of her interest in the model driven development. Currently, Raquel works as a Software Architect in the Sparsity-Technologies.

Docker Compose example

1. Introduction

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define in a configuration file the set of docker containers that application requires an specific machine. Then, using a single command, you create and start all the services in a single host.

Docker Compose is specially useful for the following use case scenarios:

Create a development environment with all the required services started only using your own machine. In other words, stop to prepare your development environment manually over and over again in your own or different machines.

Automate test environments with the same characteristics than the production environments and run integration tests.

Single hosts deployments– that is use a machine with different services as docker containers. The most recommended way to do so is using the Docker machine or Docker swarm because you can launch docker compose remotely from your laptop in a safe way without requiring SSH.

To use Docker Compose, you just need to create configuration file called docker-compose.yml, which contains the specification to create and run a set of docker containers. Let’s try to create an example with a web application that uses a redis database server.

docker-compose

2. Docker Toolbox setup

Docker compose is one of the included tools into the Docker toolbox. To install Docker toolbox, you just need to download the appropriate installer from here for your operative system and execute it.

Docker Machine : to dynamically run docker commands to remote machines or in the local/default one.

Docker Kitematic : to Build and run containers through a graphical user interface (GUI).

VirtualBox : to run Docker.

Docker client: to create and run docker containers.

Once you have finished the installation procedure, you should be able to run docker-compose --help, which prints the accepted subcommands.

3. Docker compose example: An HTTP server connected to Redis

This example is about a web application for create a TODO list using Redis with basically a three steps:

Define how to run the web and database docker images in docker-compose.yml so they can be run together in an isolated environment.

Create the web application and its Dockerfile to create the runtime environment anywhere.

Lastly, run docker-compose up and Compose will start and run your entire app.

3.1 Creating the docker-compose.yml file

Create an empty project called todos and copy the following docker-compose.yml inside the project directory.

docker-compose.yml

web:
build: .
ports:
- "8000:8000"
links:
- redis
redis:
image: redis

This file specifies two containers: web, which contains our Java code to launch a web server and redis server. Notice that in the first case, appears the build property, whereas the second case, the image property. It means that in order web container needs to build a Docker image that appears in our local file system. However, for the redis server, the image is downloaded from the DockerHub repository.

With this docker-compose.yml file, Compose simulates two machines with different IP addresses in a same local network because they are linked (i.e link property). However, only the web is externally through the 8000 of our docker-machine and Docker creates a binding between this port and the container 8000 port.

3.2 Creating the web server

Now, let’s create the Dockerfile that Compose requires in the same directory than the docker-compose.yml file with the following contents.

This Dockerfile builds our todolist project with Maven and starts an specific Java class. Thus, it is necessary to convert our todolist project into a Maven project. To do so, copy the following pom.xml in the project directory (the same place than Dockerfile and docker-compose.yml.

Notice that the TodoServer is assuming that the Redis connection URL appears in an environment variable called REDIS_PORT. However, how to know the names of the available environment variables? Docker defines an standard way to do so here.

3.3 Lauching Docker Compose

All the elements for the example are ready now. So, open your prompt, go to the project directory and run:

Docker-Compose up command

docker-compose up

Voila! At this moment, we have an HTTP server connected with Redis in our default docker-machine. In order to discover the IP of the default docker-machine, run the following command.

Docker-Machine ip command

$docker-machine ip default
192.168.99.100

3.4 Testing the web server

Using the output of the previous command, we can test the web server using the curl command. The available options are:

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.