Search code examples
dockerdocker-composedocker-swarmdocker-swarm-modedocker-stack

Adding a service to a stack after the stack has been deployed


I’m trying to add a service to a stack after the stack was already deployed. But this new service is having trouble communicating with services (redis) inside the stack.

This is my current understanding of stacks and services, please let me know if there are any inaccuracies.

Stacks are an abstraction on top of services that provide useful utilities like DNS so services across a stack can communicate with one another. Stacks allow us to logically separate out groups of services, that might be running on the same swarm (so different development teams can share the same swarm).

I would like to first deploy a stack to a swarm (via compose file) and then periodically add containers like the one described in this article about one-shot containers. These containers are different because they are performing long, stateful operations. They need to be spun up with some initial state, do their work, and then go away. They are different because they don’t need to be replicated, or load balanced.

Essentially what I’m trying to do is:

Launch a “stack” like this:

docker stack deploy --with-registry-auth --compose-file docker-compose.yml my-stack

And then some time later when certain criteria is met, add a container like this:

docker service create -name statefulservice reponame/imagename

And this generally behaves as expected, except statefulservice isn’t able to talk to redis inside my-stack.

I’m confident that statefulservice is engineered correctly because when it’s added to the docker-compose.yml it behaves as expected.

A further detail that may or may not be relevant is that the command to create a new service is issued from a container within the swarm. This happens using the go sdk for docker, and I’m using it the way the one-shot container article described

The reason I suspect this isn’t relevant: I still run into this issue when I do this operation via docker-cli only (and not use the docker sdk for go).


Solution

  • When you deploy a stack like this:

    docker stack deploy --with-registry-auth --compose-file docker-compose.yml my-stack
    

    It creates a network called my-stack_default

    So to launch a service that can communicate with services that are in that stack you need to launch them like this:

    docker service create --name statefulservice --network my-stack_default reponame/imagename