Docker Compose 101

Programmer
2 min readJul 22, 2021

Configure and docker compose in 5 minutes for docker images

Introduction

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

Whenever one has to run more than a container and have them to communicate with another, Docker Compose comes to the rescue.

The core of docker compose can be understood with the below example.

Let’s say, we want to build an application with three services

  • myservice_1 — its some JVM application that uses 2g of memory and needs to communicate with redis (using tcp) and myservice_2 (using http).
  • we want to share a storage volume across all three of the containers.
services:
agent:
image: "myservice_1"
command: ["--cache", "cache:6379", "--backend_http", "http://backend_service:4000" ]
environment:
JAVA_OPTS: -Xmx2g
volumes:
- /tmp/disk:/data
ports:
- 49100:22
cache:
image: "redis"
volumes:
- /tmp/disk:/data
expose:
- "6379"
backend_service:
image: "myservice_2"
command: ["--http_port", "4000" ]
volumes:
- /tmp/disk:/data
expose:
- "4000"

image — the name of the docker image.
command — the parameters / cli args to the docker image.
environment— the placeholder to provide env variables to the docker container.
volumes — provides mounting external storage to the docker container. Note, when the same external volume is mounted to more than a container then the file-system is shared across containers.
expose — a mechanism to allow sharing of ports to peer containers.
ports — to allow host machine to access container port 22 . One can reach this port using the host’s port 49100 . Note, its also possible to get access to range of ports as well.

Thank you for the quick read.

⛑ Suggestions / Feedback ! 😃

--

--