user2693017
user2693017

Reputation: 1860

Run docker container on a specific node in the swarm cluster

I'm trying to implement a backup system. This system requires the execution of a docker container at the time of the backup on the specific node. Unfortunately I have not been able to get it to execute on the required node.

This is the command I'm using and executing on the docker swarm manager node. It is creating the container on the swarm manager node and not the one specified in the constraint. What am I missing?

docker run --rm -it --network cluster_02 -v "/var/lib:/srv/toBackup" \
   -e BACKUPS_TO_KEEP="5" \
   -e S3_BUCKET="backup" \
   -e S3_ACCESS_KEY="" \
   -e S3_SECRET_KEY="" \
   -e constraint:node.hostname==storageBeta \
   comp/backup create $BACKUP_NAME

Upvotes: 2

Views: 2701

Answers (1)

BMitch
BMitch

Reputation: 263469

You are using an older classic Swarm method to try running your container, but almost certainly using Swarm Mode. If you installed your swarm with docker swarm init and can see nodes with docker node ls on the manager, then this is the case.

Classic Swarm ran as a container that was effectively a reverse proxy to multiple docker engines, intercepting calls to commands like docker run and sending them to the appropriate node. It is generally recommended to avoid this older swarm implementation unless you have a specific use case and take the time to configure mTLS on each of your docker hosts.

Swarm Mode provides an HA manager using Raft for quorum (same as etcd), handles encryption of all management requests, configures overlay networking, and works with a declarative model, giving the target state, rather than imperative commands to run. It's a very different model from classic Swarm. Notably, Swarm Mode only works on services and stacks, and completely ignores the docker run and other local commands, e.g.:

docker service create \
  --name backup \
  --constraint node.hostname==storageBeta \
  --network cluster_02 \
  -v "/var/lib:/srv/toBackup" \
  -e BACKUPS_TO_KEEP="5" \
  -e S3_BUCKET="backup" \
  -e S3_ACCESS_KEY="" \
  -e S3_SECRET_KEY="" \
  comp/backup create $BACKUP_NAME

Note that jobs are not well supported in Swarm Mode yet (there is an open issue seeking community feedback on including this functionality). It is currently focused on running persistent services that do not normally exit unless there is an error. If your command is expected to exit, you can include an option like --restart-max-attempts 0 to prevent swarm mode from restarting it. You may also have additional work to do if the network is not swarm scoped.

I'd also recommend converting this to a docker-compose.yml file and deploying with docker stack deploy to better document the service as a config file.

Upvotes: 2

Related Questions