问题
Let's say I have this project structure:
proj/
├── docker
│ └── myservice
│ └── Dockerfile
└── docker-compose.yml
And this is my docker-compose.yml:
version: '3'
services:
master:
build: docker/myservice
slave:
image: proj_master
depends_on: master
I want the master service to create an image, which will be used by the master and the slave services (with different parameters, not shown here). By trial and error I have found out that the slave must refer to the image proj_master.
- Where is this documented?
- Why do I need to make a reference to the
proj? Usually a docker composer file is agnostic related to where it is located ...
回答1:
Docker Compose builds your image with the name proj_master, beacuse you did not specify an image name under your master service in the Compose file.
You have a build section, so Docker Compose will build the image and give it a name based on your <directory_name_where_you_run_compose>_<service_name>:latest. I did not find this in the documentation, but tried with one of my projects, and it's in line with what you experienced.
You can fix your project by specifying the image name in your Compose file and using the same image for both services:
version: '3'
services:
master:
build: docker/myservice
image: username/masterimage:version
slave:
image: username/masterimage:version
depends_on: master
回答2:
If you'd use the following syntax in docker-compose.yaml the image will be built once by master, then used by slave:
version: '3'
services:
master:
build: docker/myservice
image: master-image:3
slave:
image: master-image:3
回答3:
This feature is not that broadly discussed but it is awesome. You can just link your slaves to your master.
dg: &dg
image: python:3.7.6-alpine
command: ....
dg_socketshark:
<<: *dg
restart: on-failure
ports:
- 9011:9011
command: /start-dg_socketshark.sh
dg_run_telegram:
<<: *dg
restart: on-failure
command: /start-dg_run_telegram.sh
Now the all share the same source but use different command o start.
来源:https://stackoverflow.com/questions/50019948/reuse-image-built-by-one-service-in-another-service