Docker Compose is a CLI tool for running and coordinating the communication of multiple Docker containers. Itâs a container orchestrator, like Kubernetes. You just have to supply a YAML config file with all the info needed for running each container, then with a single command theyâll all get created and started. You can also tear everything down instantly.
docker-compose.yml
is kept at the root of the project- It can create networks and attach containers to them and create volumes
Docker Compose supports the concatenation of multiple YAML compose files to get a âmergedâ compose file where more specialised compose files will overwrite rules in the one before it.
Itâs common practice to have multiple compose files with slight variations. Eg. in addition to a âsharedâ compose file, docker-compose.yml
, which contains all the config common to both dev and prod, you might also have docker-compose-dev.yml
and docker-compose-prod.yml
defining specific setups for development and production. In development for example, you might have a bind mount set up so that you can have hot reloading in the container. But for production, you wouldnât want this
Example
In the official Docker tutorial, weâre using these 2 commands to startup our app server and database server:
From this, we can create the following docker-compose.yml
file:
Note: Docker Compose does not replace your Dockerfile. See this relevant StackOverflow post.
Now you just need to run docker-compose up -d
and both these containers will be created, along with an isolated network and the volumes you listed:
Docker Compose vs. Kubernetes
The main difference is that Kubernetes can run and scale containers across multiple computers, but Docker Compose runs containers on a single host machine.
If you are networking containers within the same host go for docker compose. If you are networking containers across multiple hosts go for kubernetes. (source)
(Sourced from theserverside)