Lets Get Started With Docker & Docker-Compose

Aman Singh
6 min readJun 11, 2020

Lets start with traditional software development environment,the time when we have different environments for code development and another environment for deployment. It makes very difficult for coders to rectify bugs and errors, different versions of dependencies, different OS etc. Also, for system administrators it was very tough to manage the environment during peak hours. They were not able to scale the service, which cost the reliability, stability and availability.

In business terms company is going to lose customers if their service is down. In today’s fast track market one cannot afford that. So, here comes the solution.

Containerization solved this problem, by running the application in a containers. Containerization builds an application together with all its configurations, libraries and dependencies which is required by the application. Docker & Kubernetes are most widely used ecosystems to manage containers. So Lets discuss on Docker ecosystem and how it works along with what is docker-compose?

Docker

Docker is a tool designed to create, deploy and run applications by using containers. As discussed above containers allow developers to bind application code and required configurations, libraries and dependencies together. Lets start installing docker on Ubuntu machine. Installation is simple and straight forward like any other Linux installation. Start with updating the apt package index & than add the repository of docker-engine.

$ sudo apt update

Add the Docker’s GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the docker apt package repository to install docker engine.

$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Now, we are all set for installing docker engine on ubuntu machine. Run the apt package index update command & install docker engine with following command.

$ sudo apt update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

After installation start the service and and docker-engine is ready to work.

$systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-08-27 19:27:04 UTC; 5min ago
Docs: https://docs.docker.com
$ docker -v
Docker version 18.09.7, build 2d0083d

Docker Useful Commands

Here, are some useful and most commonly used docker commands. You can user docker help command to check more options with docker.

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Docker-Compose

Let’s consider a real world classic example: For running an application we need few of the services lets say a Loadbalancer, a database and the application which is been hosted on any server. If we relate this to containerization we three containers above application setup. Here comes Docker-Compose in picture.

Docker-Compose is a tool for defining and running multiple container docker application. We write YAML file to configure application services and with single command the whole stack will be up.

Docker Compose is a three step process:

  1. Define the application environment using Dockerfile.
  2. Define the services that will create entire environment in docker-compose.yml.
  3. Hit the docker-compose up command to run the entire application.

To start working with Docker-Compose you need to install docker engine first. Check out the link to install docker engine on different platforms.

After installing Docker engine, We will install docker-compose command. Follow the below mentioned steps to install docker-compose on your machine. I am using Ubuntu machine for this demo.

  1. Run the below command to download latest stable docker-compose release.
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2. Apply the executable permission to the docker-compose binary.

sudo chmod +x /usr/local/bin/docker-compose

After installing docker-compose check version.

docker-compose --version

Demo

Lets understand docker compose with a small Example: We are going to create one web application, which will have 2 web containers and a haproxy container to redirect traffic to the both the containers. below is the directory structure kindly refer to it.

Firstly, Lets start with writing Dockerfile for web container. We need to create Dockerfile in the web1 directory. Which will look like below

Dockerfile: Docker engine can build image form reading instructions from a Dockerfile.

A sample Dockerfile with will look like this.

FROM httpd:latestCOPY index.html /usr/local/apache2/htdocs/

Here, we are using apache image from dockerhub which is a latest version. Copying the index.html file to the host directory, a very simple and minimal Dockerfile. Similar steps we need to define for haproxy.

FROM haproxy:latest
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

Lets write a docker-compose file to connect all the services to each other.

version: "3.3"
services:
web1:
build: ./web1
container_name: web1
ports:
- "81:80"
web2:
build: ./Web2
container_name: web2
ports:
- "82:80"
haproxy:
build: ./haproxy
container_name: haproxy
links:
- web1
- web2
ports:
- "80:80"
expose:
- "80"

Lets check the tags which I have used and what they do:

  1. version: It reflects which compose file version supports specific docker release.
  2. services: All the services, network etc will come under service tag. web1 , web2 and haproxy is service name.
  3. build: Here we will provide the Dockerfile path.
  4. container_name: this tag will give name to your container.
  5. ports: Ports tag is used to bind port of container to host machine. Here port 81 of host is mapped to containers port 80.
  6. links: Link to containers in another service, here we are linking web1 and web2 to haproxy service.
  7. expose: Expose ports without publishing them to the host machine.

Here is link to my GitHub repository.

After setting all the files and configuration run docker-compose build command to build the environment and run it.

docker-compose up --build -d

To check the environment running run following command

docker ps

I hope you have liked the information I have shared and it will be useful for you. For more updates keep reading my blogs. For more reference & knowledge on docker-compose you can also refer to docker site.

Kindly visit here for solution related to hosting.

--

--

Aman Singh

I am an enthusiastic, zealous and a motivated DevOps Engineer & Security Engineer