Getting started with Docker Compose

Reading Time: 5 minutes

Prerequisite

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

Lets gear up with the basics of Docker.

What is docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

And importantly, Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren’t available out of the box.

Who is Docker for?

Docker is a tool that is designed to benefit both developers and system administrators, making it a part of many DevOps (developers + operations) toolchains. For developers, it means that they can focus on writing code without worrying about the system that it will ultimately be running on. It also allows them to get a head start by using one of thousands of programs already designed to run in a Docker container as a part of their application. For operations staff, Docker gives flexibility and potentially reduces the number of systems needed because of its small footprint and lower overhead.

I have cleared the basics of docker here and you can explore more from Docker Doc

What is docker Compose?

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 services

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

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

 

1) Create a Dockerfile

Dockerfile is a text document where we specify all the commands which are required to run a service. Name it as 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

 

2) Create 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 require to run the complete application. With these services you have to specify the image name, port which is to be run, links where we specify the dependencies of a particular service over any other service. So to start all the service in order to run the application we just need to up the docker-compose file and done. The application will be up and running.

 

3) Run docker-compose.yml file

Now we need to just up the docker-compose.yml file which will pull the images for the services specified(if not present in the local system).Command to up the container is docker-compose up and to stop is docker-compose stop. Note that you need to tranverse 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 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
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 :-

Open Source

Docker Documentation

Docker Hub

Written by 

I am a Software Consultant at Knoldus Inc. I am a Scala Enthusiast. I am familiar with Object Oriented Programming Paradigms, and has also worked upon .NET based technologies. Aside from being a programmer, I am familiar with NoSQL database technologies such like Cassandra. I also worked on Lagom microservice architecture.

1 thought on “Getting started with Docker Compose5 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading