0

Using standalone containers I run a cronjob with

docker run -name my_name --volumes-from container_name ...

I want to do the same in a Swarm stack, where my service have more that one replicas, so containers name are container_name.1., container_name.2., etc.

How to specify container's name not to change it each time after container restarting when it gets new id?

1 Answers1

0

Docker Swarm creates a stack with named services, and the services create containers that are disposable with unique names. It is bad practice to reference container names in a stack. The service name is in the docker-stack.yaml file. If you are looking to create a docker stack with persistent volumes, it is better to create a persistent volume instead. docker volume create [OPTIONS] [NAME] Docker volume create

You can also create it when you start your stack in your docker-stack.yaml. All containers spawn from my-service will use the same shared volume.

version: "3.8"
services:
  my-service:
    image: my-service:latest
    volumes:
       - my-data:/usr/share/mydata

volumes:
  my-data

You may also reference an externally-created volume (with docker volume create my-data) with this instead:

volumes:
  my-data:
    external: true
Joel Magnuson
  • 313
  • 2
  • 10