This might be a duplicate, as i assume it's a common use case for docker-compose. However, i can't seem to find a similar question this specific scenario.
I have a docker-compose file with the following two services:
processService:
container_name: processservice
image: mcr.microsoft.com/dotnet/core/sdk:3.1
ports:
- 5001:80
volumes:
- ./backend/packages/dotnet:/backend/packages/dotnet
- ./backend/services/processService/src:/backend/services/processService/src
working_dir: /backend/services/processService/src/ProcessService.Application
command: dotnet watch run
organizationService:
container_name: organizationservice
image: mcr.microsoft.com/dotnet/core/sdk:3.1
ports:
- 5002:80
volumes:
- ./backend/packages/dotnet:/backend/packages/dotnet
- ./backend/services/organizationService/src:/backend/services/organizationService/src
working_dir: /backend/services/organizationService/src/OrganizationService.Application
command: dotnet watch run
As can be seen in the compose file, i use the same volume mount for both services, namely ./backend/packages/dotnet:/backend/packages/dotnet
.
I want to know whether or not it is possible to declare this volume once in the volumes section, and then reuse it across services.
I have tried searching for an answer to this. The answers i come across suggest to create a volume manually with docker volume create foo
and then using it as such:
...
volumes:
- foo:/backend/packages/dotnet
...
However, this doesn't make sense to me as I don't see how foo should be aware of the directory that I want it to read/write from when i havn't specified this at any point.
I have tried reading through the docker documentation, but I can't find anything related to creating a named volume mounting a specific directory.
Does anyone have any suggestions as to how to accomplish this?
These are two different volumes
section in a docker-compose file:
local (service)
Located into a service part (as you did in your file), local volumes
sections map data volume(s) (or host path(s)) to a container location.
global
Located at the same level as services:
section, the global volumes
section names and creates data volume(s).
Then, this is how to use data volumes within a docker-compose file:
service1:
volumes:
- vol_1:location_1
service2:
volumes:
- vol_1:location_2
volumes:
vol_1:
Thus, in your case, the docker-compose part should be:
processService:
container_name: processservice
image: mcr.microsoft.com/dotnet/core/sdk:3.1
ports:
- 5001:80
volumes:
- dotnet:/backend/packages/dotnet
- processService:/backend/services/processService/src
working_dir: /backend/services/processService/src/ProcessService.Application
command: dotnet watch run
organizationService:
container_name: organizationservice
image: mcr.microsoft.com/dotnet/core/sdk:3.1
ports:
- 5002:80
volumes:
- dotnet:/backend/packages/dotnet
- organizationService:/backend/services/organizationService/src
working_dir: /backend/services/organizationService/src/OrganizationService.Application
command: dotnet watch run
volumes:
dotnet:
processService:
organizationService:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.