Docker-Compose - Creating the best from Docker

  • 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.
  • 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 "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose2. Apply executable permissions to the binary usingsudo chmod +x /usr/local/bin/docker-compose
version: '3'services:

build: .
- "5000:5000"
- logvolume01:/var/log
- APP_NAME=web-app
- APP_PORT=5000

image: redis
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?



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store