Docker Compose
docker-compose.yml file contains the command to run, start, stop all the containers and services mentioned inside the file.
up
build or create or recreate container and start it
docker-compose up
build
build the container but won't start it
docker-compose build
start
start the already built container
docker-compose start
stop
stop the container but won't delete it
docker-compose stop
down
stop and delete the containers
docker-compose down
Common configuration keys inside the docker-compose.yml file
| Command | Description | Example |
| version | Written at the top of the file and for which version of docker compose | 3.0 |
| services | beginning of the containers to be managed | services: |
| name | name means actual name of the container | database_server |
| build | directory containing the Dockerfile for this container/service | ./database_server |
| ports | which port to expose for this app | 8080:8080 |
| volumes | list the directories that should be mounted into the container from the host OS | ./home/kasturitech/db/:/mysql |
| environment | used to pass environment variables to the app. eg: username, password. It's usually not safe | MYSQL_USER_PASSWORD=secret |
| restart | restart policy: no, always, on-failure, unless-stopped | always |
| depends_on | start order dependency. for eg. database servcies need to run before backend app | depends_on |
| image | what image to select to build this container | mysql:8.0.1 |
| networks | what networks the container will be part of. Containers can be part of multiple networks | backend_apps |
| secrets | Sensitive data. eg. Passowords, keys | secrets |
version: "3.9"
services:
web:
image: nginx
container_name: my_nginx
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
environment:
- NGINX_HOST=localhost
restart: always
depends_on:
- db
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- db_data:/var/lib/mysql
volumes:
db_data: