Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

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.

Prerequisites

  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

ENV SCALA_VERSION 2.12.1
ENV SBT_VERSION 0.13.15

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

# Install Scala
## Piping curl directly in tar
RUN \
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
RUN \
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
WORKDIR /root


Create a docker-compose.yml File

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

Sample docker-compose.yml file:

zookeeper:
image: zookeeper:3.4
ports:
– 2181:2181
kafka:
image: ches/kafka:latest
ports:
– 9092:9092
links:
– zookeeper:3.4
myElastic:
image: elasticsearch:2.4.0
ports:
– 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.

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

References

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
cloud ,docker compose ,container orchestration ,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 }}