7

I have a docker-compose.yml file which works with docker-compose up --build. My app works and everything is fine.

version: '3' services: myapp: container_name: myapp restart: always build: ./myapp ports: - "8000:8000" command: /usr/local/bin/gunicorn -w 2 -b :8000 flaskplot:app nginx: container_name: nginx restart: always build: ./nginx ports: - "80:80" depends_on: - myapp 

But when I use docker stack deploy -c docker-compose.yml myapp, I get the following error:

Ignoring unsupported options: build, restart Ignoring deprecated options: container_name: Setting the container name is not supported. Creating network myapp_default Creating service myapp_myapp failed to create service myapp_myapp: Error response from daemon: rpc error: code = InvalidArgument desc = ContainerSpec: image reference must be provided 

any hints how I should "translate" the docker-compose.yml file to make it compatible with docker stack deploy?

1
  • Hi any solution on how to "translate" properly? Commented Nov 24, 2020 at 11:51

1 Answer 1

16

To run containers in swarm mode, you do not build them on each swarm node individually. Instead you build the image once, typically on a CI server, push to a registry server (often locally hosted, or you can use docker hub), and specify the image name inside your compose file with an "image" section for each service.

Doing that will get rid of the hard error. You'll likely remove the build section of the compose file since it no longer applies.

Specifying "container_name" is unsupported because it would break the ability to scale or perform updates (a container name must be unique within the docker engine). Let swarm name the containers and reference your app on the docker network by it's service name.

Specifying "depends_on" is not supported because containers may be started on different nodes, and rolling updates/failure recovery may remove some containers providing a service after the app started. Docker can retry the failing app until the other service starts up, or preferably you configure an entrypoint that waits for the dependencies to become available with some kind of ping for a minute or two.

Without seeing your Dockerfile, I'd also recommend setting up a healthcheck on each image. Swarm mode uses this to control rolling updates and recover from application failures.

Lastly, consider adding a "deploy" section to your compose file. This tells swarm mode how to deploy and update your service, including how many replicas, constraints on where to run, memory and CPU limits and requirements, and how fast to update the service. You can define a restart policy here as well but I recommend against it since I've seen docker engines restarting containers that conflict with swarm mode deploying containers on other nodes, or even a new container on the same node.

You can see the full compose file documentation with all of these options here: https://docs.docker.com/compose/compose-file/

Sign up to request clarification or add additional context in comments.

4 Comments

is there a way to at least remove the underscore from the swarm naming convention? as it causes issues with apache...
@A.JAlhorr I think you want to open a new question describing the problem you want to solve, rather than the solution you'd like to try implementing. Apache shouldn't know the container name.
Yes sorry, I figured it out, you can call it by the container name without the stack from within other containers in the same network; so for a container stack_container you can have your request simply to container and not to stack_container
@A.JAlhorr that's the service name, both swarm and compose create an alias for that, and is the preferred way to call other containers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.