docker_about_docker_docker_compose_and_docker_swarm - DmitrySemikin/dsemikin_notes GitHub Wiki
About Docker, docker-compose and Docker Swarm
Good article about difference between docker-compose
and Docker Swarm (managed by docker stack
): https://linuxhint.com/docker_compose_vs_docker_swarm/
Two (or more) meanings of the word Docker
As it is often the case the word "docker" has more than one meanings.
The first meaning is "The SW system, which is meant to create, run and manage containers. Meaning the whole thing."
The second meaning is "The command-line command docker
".
The confusion (at least for me) came among other things from the fact, that the cli-command docker
does many of the things, which is described in the explanation of the first meaning: create, run and manage containers. But the point is, that it is only one part of the system, and other parts are also important (and it is important to know them).
Docker Runtime ( == Docker Server, == Docker Service, == Docker Daemon)
Disclaimer: some of the words/terms I use here I invented myself, so sorry, if they do not match common conventions.
Docker uses client-server architecture.
Server does heavy lifting: creates images, runs containers etc.
On Windows Systems Docker-Servers for Linuxed-based containers run inside VMs, so effectively on a remote machine (docker SW suite manages it transparently for you, so one may even not notice it). One could also explicitly create VM with linux and docker-server on it, or even use real remote machine.
On Linux systems one can run Docker Server on the same machine, where the client is (assuming that containers are Linux-based). Usually this would be the setup for the development environment. Of course on Linux one can still use remote Docker Server, which is usually the case for production environments.
Docker Client (variety of them)
As there is a server, of course, there is also a client. In fact multiple clients.
The most known docker client is docker
cli-command. Another client is docker-compose
.
docker
(except docker stack
)
Notice: regarding Docker Swarm (managed by the command docker stack
see below).
The docker
command allows for working with single container: building, starting, stopping, running commands
on it etc. Also docker
serves to manage single networks and volumes (create, delete, examine etc).
If one just want to build image from single existing Dockerfile or to run a single container from existing image
(probably to try some technology), then usually docker
command should be sufficient. It is also a good fit
if one to need to check or interact with already running container etc.
Notice: in some cases docker-compose
may be perffered over docker
even when running single container. See below.
docker-compose
Notice: Same as docker
, docker-compose
command (client) is part of the Docker SW Suite.
The docker-compose
command (client) is intended to work with one or multiple containers running on the same
Docker Server. The major difference from "plain" docker
is that it takes as an input prepaired configuration
file (by default docker-compose.yml
). For running single container this would mean, that you don't need to
specify all the details about container invocation in the command line, but instead put it into the configuration
file and then can run simple command line docker-compose up
.
And quite naturally if one can pre-configure and run one container with simple command, the same can be done
for multiple containers. This is where docker-compose
shines. I.e. if your full setup is done of multiple
containers (e.g. DB and several microservices), then you don't need to start each container separately with
plain docker
, but instead write docker-compose.yml
, which describes the "orchestration" and then just
fire single command to start the whole thing.
Note, that even when working with single container, using of docker-compose
may be beneficial to memorize
the options used to start the container (like e.g. volume names, port-mappings, env-vars values etc.)
and to avoid tedious repeated typing into command line every time the container needs to be started. One could
of course write simple bash or cmd/powershell scripts for this, but docker-compose
is a better approach,
in that it is more unified and working on all systems the same.
docker stack
)
Docker Swarm (The docker stack
command is the command aimed to deal with Docker Swarm. Docker Swarm is in turn
a distributed setup of Docker Servers aimed for high availability systems (one can of course has Swarm
made of single node, but it does not make too much sense - probably only for uniformity).
The previous paragraph already explains the difference between docker-compose
and docker stack
.
Both are aimed to deal with multiple containers at once. The difference is though, that the docker-compose
can only run all containers on the same server, while docker stack
runs containers on Docker Swarm, which
is distributed system utilising multiple Docker Servers.
If docker stack
can also run containers on single Docker Server, why would one need docker-compose
?
The answer is simple: by giving up possibility to run containers on multiple Docker Servers docker-compose
gains possibility to do more operations with the containers. For example docker stack
cannot build
images - the images used in the configuration should be already built and available for the client
before the Swarm can be started.
Summary
- Docker is not a monolith thing, but has Docker Server (aka Docker Daemon) and clients
docker
anddocker-compose
(maybe there are more???) docker
(exceptdocker stack
) is best suited for the single-invocation operations (not assumed to be run repeatedly), like running container to have a look at new technology, or single action of building an Image, when this process is not embedded into some development process.docker-compose
enables "repeatability" of the configuration and starting multiple containers at once. This can be useful e.g. when working with containers in some development environment. Being only able to work with single Docker Server it provides more commands, thendocker stack
, which can be used with distributed systems.- Docker Swarm (managed by
docker stack
command) serves for running containers on distributed setups with multiple Docker Servers. This one is most useful in production environments, when distributed setups are of interest.