A-To-Z of Docker-compose

Why Docker-compose?

NOTE: Docker-compose file is not a replacement of the Dockerfiles, they solve two different purposes.

Prerequisites

1. Docker installation

docker --versionORdocker -v

2. Docker-compose installation

docker-compose -vORdocker-compose --version

3. Basic docker knowledge

4. Basic YAML knowledge

Suggestion: I would like to suggest using VS code and install the Docker extension in it. It will add IntelliSense to the project, it will help you with proper suggestions of the docker-compose keys

Creating docker-compose file

Figure-1

Docker-compose keys description

1. version:

version: "3.8"

2. services:

version: "3.8"
services:
mongodb:
------------------
------------------
------------------
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------

3. image:

version: "3.8"
services:
mongodb:
image: 'mongo'
------------------
------------------
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------
image: 'custom-repository/mongo'

4. volumes:

version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
------------------
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------

volumes:
data:

5. environment:

environment:
MONGO_USERNAME: root

OR
environment:
- MONGO_USERNAME=root
version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
environment:
MONGO_USERNAME: root
MONGO_PASSWORD: root
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------

volumes:
data:

6. env_file:

MONGO_USERNAME=root
MONGO_PASSWORD=root
version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------

volumes:
data:

7. networks:

version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
node-app:
------------------
------------------
------------------
react-app:
------------------
------------------

volumes:
data:

8. build:

build: ./backend
build: .
build: 
context: ./backend
dockerfile: Dockerfile-dev
version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
node-app:
build:
context: ./backend
dockerfile: Dockerfile-dev
------------------
------------------
react-app:
------------------
------------------

volumes:
data:

9. ports:

ports:
- '3000:80'
- '8080:8080'
version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
node-app:
build:
context: ./backend
dockerfile: Dockerfile-dev
ports:
- '80:80'
react-app:
------------------
------------------

volumes:
data:

10. depends_on:

version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
node-app:
build:
context: ./backend
dockerfile: Dockerfile-dev
ports:
- '80:80'
depends_on:
- mongodb
react-app:
------------------
------------------

volumes:
data:

11. container_name

version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
container-name: mongodb
node-app:
build:
context: ./backend
dockerfile: Dockerfile-dev
ports:
- '80:80'
depends_on:
- mongodb
container-name: backend
react-app:
------------------
------------------

volumes:
data:

12. stdin_open: and tty:

version: "3.8"
services:
mongodb:
image: 'mongo'
volumes:
- data:data/db
env-file:
- ./env/mongo.env
networks:
- ecommerce-network
container-name: mongodb
node-app:
build:
context: ./backend
dockerfile: Dockerfile-dev
ports:
- '80:80'
depends_on:
- mongodb
container-name: backend
react-app:
stdin_open: true
tty: true
------------------

volumes:
data:
Figure-2

Starting services using docker-compose

docker-compose up

Detach mode (-d)

docker-compose up -d

Forcing image rebuild ( — build)

docker-compose up --build
docker-compose build

Stopping services using docker-compose

docker-compose down

Removing volumes while stopping services

docker-compose down -v

--

--

--

Full stack developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Make Sure You Avoid Tech-Tutorial Overload

Headless CMS API calls cost comparison

flotiq blog. api calls

Why do we need an execution context?

refs exists, cannot create

Things you need to know before Creating a Fintech App for Startups

Fintech App for Startups

Externalize Application Configuration With Spring Cloud Config

Encrypted QR Code generation using Flutter — AES Encryption

OSCP — C:\whoami

Offensive Security Certified Professional Logo and Text

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
Rohan Aggarwal

Rohan Aggarwal

Full stack developer

More from Medium

Docker for devs with hands-on| Part-3

Creating streamlined docker images

New relic agent installation for Java application servers

Windows Interactive Services Using Java (JNA)