Is is possible to create a volume for each replica when create a service?

I need to deploy a nginx service with 4 replica using the compose file like the following.
Is is possible that it create a volume for each replica automatically? If it is possible, what is the format to specify
in the compose file?

version: “3”

services:

nginx:
image: nginx
ports:
- "5000:80"
volumes:
- log:/var/log/nginx

deploy:
  replicas: 4
  restart_policy:
    condition: on-failure

volumes:
log:

syntax would be

services:
  nginx:
    volumes:
      - /var/log/nginx

but this will create unnamed volumes. These are not mapped to your host as in your example.

If you just need the volumes for logging, consider a docker favoured logging solution like ELK for example.

Thanks. I think what I need is a named volume. Is it possible to create a named volume for each replica?

then it is not possible to use the automatic way. you need to write down the 4 services by hand.

extends could help you a bit. You write the basic service with everything in common and later just let the 4 services extend that one.

Thanks. It will help if we decide how many replica we need in advance. There is no way to scale up or scale down a stateful service in the current Docker Swarm support, right?

1 Like