问题
Having a base docker-compose.yml like the following:
version: '2'
services:
web:
build: .
...
How can I extend it to use an image instead?
docker-compose.prod.yml
version: '2'
services:
web:
image: username/repo:tag
Running it with docker docker-compose -f docker-compose.yml -f docker-compose.prod.yml up still promps:
Building web
Step 1/x : FROM ...
I tried with docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --no-build:
ERROR: Service 'web' needs to be built, but --no-build was passed.
I'm expecting the "Pulling from name/repo message" instead. Which options do I have? Or do I need to create a complete duplicate file to handle this slight modification?
回答1:
Omit the build on the base docker-compose.yml, and place it in a docker-compose.override.yml file.
When you run docker-compose up it reads the overrides automatically.
Extracted from the Docker Compose Documentation.
Since your docker-compose.yml file must have either build or image, we'll use image that has less priority, resulting in:
version: '2'
services:
web:
image: repo
[...]
Now let's move onto docker-compose.override.yml, the one that will run by default (meaning docker-compose up or docker-compose run web command).
By default we want it to build the image from our Dockerfile, so we can do this simply by using build: .
version: '2'
services:
web:
build: .
The production one docker-compose.prod.yml run by using docker-compose -f docker-compose.yml -f docker-compose.prod.yml up will be similar to this one, excepting that in this case we want it to take the image from the Docker repository:
version: '2'
services:
web:
image: repo
Since we already have the same image: repo on our base docker-compose.yml file we can omit it here (but that's completely optional).
来源:https://stackoverflow.com/questions/42179146/docker-compose-how-to-extend-service-with-build-to-use-an-image-instead