Search code examples
dockerdocker-composedocker-build

Pull from remote image unless build is explicitly called


Is there a way to have both build: and image: specified in a docker-compose yaml file and have them play nice?

Ideally, the remote image is pulled on docker-compose up, and are built instead when docker-compose up --build or docker-compose build are run. The issue is a bit of a nuisance when developers must comment out the build param to pull the remote image.

Example 1

build: ./nginx
image: us.gcr.io/cool-project-123456/dev-box:latest

Build is always run, and then names and tags the container with the image: value.

Example 2

# build: ./nginx
image: us.gcr.io/cool-project-123456/dev-box:latest

Here it seems that as long as build: is commented, and image: exists, all build commands are ignored and the image is pulled instead.


Solution

  • Example 1 is not always run. It runs only when your don't have that image: us.gcr.io/cool-project-123456/dev-box:latest

    docker-compose up is always using existing images in your machine first.

    My suggestion is keeping

    build: ./nginx
    image: us.gcr.io/cool-project-123456/dev-box:latest
    

    and using

    • docker-compose up --build
    • docker-compose pull & docker-compose up