Docker Basics Part 4
Docker Compose
Docker Compose is an additional tool, offered by the Docker ecosystem, which helps with orchestration / management of multiple Containers. It can also be used for single Containers to simplify building and launching.
Why?
Consider this example:
Terminaldocker network create shopdocker build -t shop-node .docker run -v logs:/app/logs --network shop --name shope-web shop-nodedocker build -t shop-databasedocker run -v data:/data/db --network shop --name shop-db shop-database
This is a very simple (made-up) example - yet you got quite a lot of commands to execute and memorize to bring up all Containers required by this application.
And you have to run (most of) these commands whenever you change something in your code or you need to bring up your Containers again for some other reason.
With Docker Compose, this gets much easier.
You can put your Container configuration into a docker-compose.yaml file and then use just one command to bring up the entire environment: docker-compose up .
Docker Compose Files
A docker-compose.yaml
file looks like this:
docker-compose.yaml1version: "3.8" # version of the Docker Compose spec which is being used2services: # "Services" are in the end the Containers that your app needs3 web:4 build: # Define the path to your Dockerfile for the image of this container5 context: .6 dockerfile: Dockerfile-web7 volumes: # Define any required volumes / bind mounts8 - logs:/app/logs9 db:10 build:11 context: ./db12 dockerfile: Dockerfile-web13 volumes:14 - data:/data/db
You can conveniently edit this file at any time and you just have a short, simple command which you can use to bring up your Containers:
Terminaldocker-compose up
You can find the full (possibly intimidating - you'll only need a small set of the available options though) list of configurations here: https://docs.docker.com/compose/compose-file/
Important to keep in mind: When using Docker Compose, you automatically get a Network for all your Containers - so you don't need to add your own Network unless you need multiple Networks!
Docker Compose Key Commands
There are two key commands:
docker-compose up
: Start all containers / services mentioned in the Docker Compose file-d
: Start in detached mode--build
: Force Docker Compose to re-evaluate / rebuild all images (otherwise, it only does that if an image is missing)
docker-compose down
: Stop and remove all containers / services-v
: Remove all Volumes used for the Containers - otherwise they stay around, even if the Containers are removed
You might also like posts tagged with #docker:
Comments