Docker : docker-compose
Amine FERDJAOUI
amine.ferdjaoui@sogeti.com
Année universitaire 2024/2025
Why docker-compose
Usually we run multiple applications : Frontend, Backend, Database, etc.
The automatisation becomes dif cult.
Dif cult to maintain multi-container applications.
Dif cult to update application in production.
Docker
fi
fi
fi
Docker-compose file
• Uses yml (or yaml) le, usually used for con guration, it uses indentation (like python)
• The lename must be docker-compose.yml (or yaml)
• Automatisation of building and running images and containers
• De ne services (applications), parameters (ports, volumes, etc.), networks, etc.
Docker
fi
fi
fi
fi
Installation
• If you are under mac or windows, docker-compose comes installed with Docker desktop
• For Linux : follow installation instructions on : https://docs.docker.com/compose/install/
To verify that docker-compose is installed:
$ docker-compose --version
Docker
Docker-compose file https://docs.docker.com/reference/compose- le/
Start with compose version: version: “3.8”
Define the services:
services:
frontend:
backend:
Define configuration within each service:
Build file (Dockerfile)
Ports
Volumes
Docker
fi
Docker-compose file https://github.com/AmineFrj/docker-compose
docker-compose.yml
1 version: “3.8”
2 services:
3 backend:
4 build: .
5 ports:
6 - ”5000:5000”
7 volumes:
8 - ./templates:/templates #<local>:<container>
9 volumes:
10 templates: #pour sauvegarder le volume même après la suppression
Docker
Docker-compose commands - build
All commands are the same as docker commands except that the docker-compose ones
will be applied to the hole applications.
Build
Enter into the docker-compose le directory and run build command (without
any ags)
Image will be named: <folder-name> _ <service-name>
$ docker compose build
$ docker compose build
$ docker images
Docker
fl
fi
Docker-compose commands - Start
Run
Once the image built, the run command starts the new container(s)
$ docker compose up
If the image is not built, you can build the image and then run it
$ docker compose up - - build
$ docker compose up -d #detached mode
$ docker compose ps
Docker
Docker-compose commands - Stop and remove
Stop
To stop and remove containers (images are not removed)
$ docker compose down
$ docker compose down
$ docker compose ps
$ docker ps -a
$ docker images
Docker
Docker-compose - commands recap
Build docker image
$ docker compose build
$ docker images
Run container
$ docker compose up -d #in detached mode
$ docker ps -a
Stop and remove containers
$ docker compose down
$ docker ps -a
Docker