Docker-Compose
Docker File
Before we talk about compose one thing should we know is Docker Fie . What is Docker File ? . A Dockerfile is file that Docker reads from top to bottom. It contains a bunch of instructions which informs Docker HowDocker image should get built. Short Answer Dockerfile basically for Customize your Docker image .
You can relate it to cooking. In cooking you have recipes. A recipe lets you know all of the steps you must take in order to produce whatever you’re trying to cook. Recipe = Docker FIle
Docker File Command
Commands: |
Create Docker FIle
Ok Let’s Practice, for example we have a Use Case:
- you wanna build Node on top of Linux Alpine
- Expose port 4000
- Adding custom work directory to /app
- Adding tini for graceful shutdown before you running it.
Answer :
FROM node:10.17-alpine |
After that you ready to build the docker file by run
- Docker build -t
.
Very Simple Right ? It is ! . That’s why Developer & Operaion Team love Docker.
Docker Compose
What the heck is compose ? If you wanna run multiple container at single service (in local environment) you need compose . Short Answer compose is YAML file for defining multi container such as port , shared volume , relation between container etc.
Compose Command
Docker Compose CLICommands:
build Build or rebuild services |
Create Docker Compose
The most important things you need to know, you must write docker compose YAML version in first line , There are two version now . Version 2 for local development and Version 3 if you using orchestration like kubernetes or swarm . I recomend you using version 2 because have a lot of feature for local development.
Remember Docker Compose care about Indentation and case sensitive
Back To the create docker compose , for example we have use case :
- You have 2 services , first app service and second mongo services which is your database
- App services is build from docker file , so you no need to download image again.
- Expose your app to port 3000 & make sure node app run after mongodb up and running
- you need set MongoDB Volume too and expose mongo port to 27019
Answer :
version: '2.4'
services:
app:
restart: always
build: .
ports:
- '3000:3000'
depends_on:
- mongo
mongo:
image: mongo:4.2.0
ports:
- '27018:27017'
volumes:
- mloandata:/data/db
- mloanconfig:/data/configdb
volumes:
mloandata:
mloanconfig:
Last Word
That’s it about docker compose hope you will get better understanding about compose workflow, dont forget to check really good blog post about docker & compose , Link down below.