0
votes

I'm new to Docker Swarm. As I understand, Docker Swarm allows you to abstract from clustering. Means you don't care on which hardriwe container is deployed.

On the other hand, the standard way to handle database in Docker - is to write data outside Docker container (to avoid copy-on-write behaviour). That's achieved by mounting a Volume and write db-related data to it. The important thing here - are Volumes machine-specific? Are Docker & Docker Swarm clever enough to mount a Volume on the machine it's needed?

Example:

I have 3 machines and 3 microservices/containers. All of them are deployed through Docker Swarm. Only one microservice/container must connect to a database. So I need to mount Volume only on one machine. But on which?

1

1 Answers

1
votes

Databases and similar stateful applications are still a hard thing to deal with when it comes to Docker swarm and other orchestration frameworks. Ideally, containers should be able to run on any node in the swarm, but the problem comes when you need to persist data beyond the container's lifecycle.

Mounting a volume is the Docker way to persist data, however this ties the container with a specific node as volumes are created on the specific nodes. There are many projects that try to solve this problem and provide some sort of distributed storage.

There was a project called Flocker that deals with the above problem (it’s no longer maintained). There is also a newer project called REXRAY.

Are Docker & Docker Swarm clever enough to mount a Volume on the machine it's needed?

By default, no. Docker swarm will choose one of the nodes and deploy the container on it. However, you can work around this problem:

First, you need to define a named volume in you Stackfile/Composefile under the service definition. Second, you need to use node Placement Constraints to restrict where the database container should run.

If you do not you a distributed storage tool, then when it comes to databases and similar stateful containers that need volumes, you need to restrict the container to a specific nodes.