Getting Started With Docker Compose

DZone 's Guide to

Getting Started With Docker Compose

Get up and running with Docker Compose's container orchestration as well as a checklist of helpful commands you can use.

· Cloud Zone ·
Free Resource

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

Steps for implementing Docker Compose:

  1. Create your Dockerfiles for each service.

  2. Define services to run your application in the docker-compose.yml file.

  3. Start and run your entire application with the help of the docker-compose.yml file.


  1. Install Docker (Link – Docker setup)

  2. Install Docker Compose (Link – Docker compose setup)

Create a Dockerfile

A Dockerfile is a text document where we specify all the commands that are required to run a service. Name it Dockerfile and put this in the project directory.

Sample Dockerfile:

# Scala and sbt Dockerfile
# https://github.com/hseeberger/scala-sbt

# Pull base image
FROM openjdk:8


# Scala expects this file
RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

# Install Scala
## Piping curl directly in tar
curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz – -C /root/ && \
echo >> /root/.bashrc && \
echo ‘export PATH=~/scala-$SCALA_VERSION/bin:$PATH’ >> /root/.bashrc

# Install sbt
curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
dpkg -i sbt-$SBT_VERSION.deb && \
rm sbt-$SBT_VERSION.deb && \
apt-get update && \
apt-get install sbt && \
sbt sbtVersion

# Define working directory

Create a docker-compose.yml File

Create a file named docker-compose.yml in your project directory.

Sample docker-compose.yml file:

image: zookeeper:3.4
– 2181:2181
image: ches/kafka:latest
– 9092:9092
– zookeeper:3.4
image: elasticsearch:2.4.0
– 9200:9200


This means you have specified all your services that you need to run the complete application. With these services, you have to specify the image name, port to be run, and links — where we specify the dependencies of a particular service over any other service. So, to start all the services in order to run the application, we just need to up the docker-compose file and we're done. The application will be up and running.

Run the docker-compose.yml File

The command to up the container is docker-compose up, while the command to stop is docker-compose stop. Note that you need to traverse to the directory where the docker-compose.yml file is present and then you can explore the following commands.


You can check all the docker compose commands by typing the docker-compose –help command on your terminal.

Command Use
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information


cloud, container orchestration, docker compose, tutorial

Published at DZone with permission of Akhil Vijayan , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}