A-To-Z of Docker-compose

Why Docker-compose?

Prerequisites

docker --versionORdocker -v
docker-compose -vORdocker-compose --version

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

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

volumes:
data:
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:
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:
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:
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:
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:
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:
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:
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
docker-compose up -d
docker-compose up --build
docker-compose build

Stopping services using docker-compose

docker-compose down
docker-compose down -v

--

--

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