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.
Join the DZone community and get the full member experience.Join For Free
Steps for implementing Docker Compose:
Create your Dockerfiles for each service.
Define services to run your application in the docker-compose.yml file.
Start and run your entire application with the help of the docker-compose.yml file.
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.
# 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.
You can check all the docker compose commands by typing the
docker-compose –help command on your terminal.
|build||Build or rebuild services|
|bundle||Generate a Docker bundle from the Compose file|
|config||Validate and view the compose file|
|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|
|logs||View output from containers|
|port||Print the public port for a port binding|
|pull||Pull service images|
|push||Push service images|
|rm||Remove stopped containers|
|run||Run a one-off command|
|scale||Set number of containers for a service|
|top||Display the running processes|
|up||Create and start containers|
|version||Show the Docker-Compose version information|
Published at DZone with permission of Akhil Vijayan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.