- docker-compose is a docker script to run multiple docker commands in one go. This is configured in
docker-compose.yml
file. As basic one looks like this:
# version of docker compose
version: '3'
# We can setup multiple services in one go
services:
# provide the name of the service, can be anything
node-app:
# everything below here are the same docker cli commands
build: .
port:
- "3000:3000"
volumes:
# docker compose let you use relative path for volumes
- ./:/app
# this is a trick so node_modules dont get overridden
- /app/node_modules
environment:
# we can provide envs either explcity or with env file
- PORT=3000
# env_file:
# - fileName
execute by running docker-compose up
. To tear down docker-compose down
additionally provide -v
flag to remove related volumes.
docker-compose
also builds the image and will cache this step if it can.
note: if you update your Dockerfile, docker-compose
is not smart enough to know to rebuild the image. use the --build
flag to force a rebuild of the image
-
its possible to have multiple
docker-compose.yml
files perhaps you want a docker setup for dev and another for production.-
setup
docker-compose.dev.yml
anddocker-compose.yml
- https://www.youtube.com/watch?v=9zUHg7xjIqQ&t=4896s- then when it comes to running these files we can run
docker-compose -f docker-compose.yml docker-compose.dev.yml -d
which will merge the docker-compose files together. the later script will override the former script
- then when it comes to running these files we can run
-
-
you can have if else statements in your Dockerfile
-
ARG
are build time environment variables