Docker-Compose - Creating the best from Docker

Abhishek
3 min readFeb 2, 2021

--

In this article we will experience the magic of docker-compose by using it to combine multiple docker images. Consider an environment where you want to Up, Run or Manage several Docker Containers such that they shares the same network, or uses a common volume etc. All this can be done with the help of docker-compose.

Prerequisite

This article assume you have a basic understanding of Docker environment (images, containers etc). If not please check out my previous article about Docker first.

Introduction to Docker-Compose

  • We can use docker-compose for combining multiple docker images & can manage each container very easily.
  • Docker Compose is a tool for defining and running multi-container Docker applications.
  • We can create a YAML file (docker-compose.yml) to configure application’s services. Then, with a single command, we can start all the services from our configuration.

Installing Docker-Compose

You can go to https://docs.docker.com/compose/install/ to install docker-compose according to your platform.

  • For Windows & MacOS:
Geneally it comes pre-installed along with the standard docker installation. So most users don't need to install it seperately.
  • For Linux:
1. Download the current stable version's binary (1.28.2) usingsudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose2. Apply executable permissions to the binary usingsudo chmod +x /usr/local/bin/docker-compose

General Syntax in docker-compose.yml

version: '3'services:

web-container:
build: .
ports:
- "5000:5000"
volumes:
- logvolume01:/var/log
environment:
- APP_NAME=web-app
- APP_PORT=5000

redis-container:
image: redis
volumes:
logvolume01: {}
  • Here web-container & redis-container are the name of two docker containers we wish to create
  • “ build . ” conveys that the Dockerfile for our web-container is present in same directory (location “.”)
  • ports: -5000:5000” means forwarding the port (5000 of the container) to the port (5000 in the host machine). i.e. “host_port : container_port
  • volumes: - logvolume01:/var/log” means there is a folder named logvolume01 (inside our host system) which we want to map to /var/log (inside the container). Any change that will happen to /var/log by any process inside the container will be reflected back to logvolume01 & vice-versa. i.e. They will be treated as same volume.
  • In the environment section we have added two environment variables (APP_NAME & APP_PORT) with values (web-app & 5000) respectively.
  • “image: redis” this statement signifies that we are not using any Dockerfile for this container rather than we want the image (from dockerhub or any other docker hosting server) through docker pull redis.
  • We can also configure & define some other properties of the volume (We havn’t in this case) in the volumes section. (So this part is completely optional)

Running & Experimenting with our containers using Docker-Compose

To run the complete application we can now execute
-
docker-compose up
To run the complete application in background we can now execute
-
docker-compose up -d
To stop the application running in background
-
docker-compose stop
To List all the running containers
-
docker-compose ps
To run any command on a container
Command is "docker-compose run container-name command"
example: docker-compose run web-container ls
We can even get a complete shell of that terminal using
"docker-compose run container-name bash" OR
"docker-compose run container-name sh"
example: docker-compose run web-container bash

Now, what’s Next?

Please also have a look at my next & final blog of this series (Part 3) as well. In this Part 3 we will create a full stack application using different containers each for database (using postgres) API routes (using python & Fast API) and Frontend & reverse proxy server (using nginx).

https://unsplash.com/photos/M3fhZSBFoFQ

Congratulations for Successfully Completing this article & Thanks for sticking till the end.

Please let me know about your views & Queries in the comment section.

--

--

Abhishek
Abhishek

No responses yet