Docker Community Forums

Share and learn in the Docker community.

Running one off commands in swarm containers


(Talkingquickly) #1

I’m using a docker-compose.yml file along with docker stack deploy to deploy a web service, a database service and some other supporting services across a cluster. This all works as expected.

The only piece I can’t work out is how to handle one off commands.

For example, I want to run the command bundle exec rake db:migrate within a web service container. That container needs to have access to the database server via it’s hostname. When using compose locally, it’s easy I can just use:

docker-compose run SERVICE_NAME CMD

But I can’t work out a way to do this on swarm. I’ve tried:

docker run -it --network=dcdeploydemo_default 127.0.0.1:5000/dcdeploydemo:1.1 bundle exec rake db:migrate

But the hostname doesn’t resolve.

Similarly I want to be able to run bash in one of these containers and attach to it. What’s the current correct way of approaching this when using swarm?


(Ryanscope) #2

The closest I’ve come across so far is running a service with --restart-condition=none.

Example:

docker service create --restart-condition=none --name=db_migration {db container} bundle exec db:migrate

I hope that I’m wrong on this, and there is (eventually) a better way to run one-off commands in swarm.