Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose how to extend service with build to use an image instead

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?

like image 878
zurfyx Avatar asked Feb 11 '17 17:02

zurfyx


People also ask

Can docker compose build a docker image?

Define and run multi-container applications with Docker. docker-compose build : This command builds images in the docker-compose. yml file. The job of the build command is to get the images ready to create containers, so if a service is using the prebuilt image, it will skip this service.

How do I run a specific service in docker compose?

To start (up), stop, restart or build a single service (container) using the Docker Compose, simply specify the name of the service against which to run the corresponding docker-compose command.

Does docker compose up do a build?

Description. Builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services. The docker compose up command aggregates the output of each container (like docker compose logs --follow does).

What is the difference between docker build and docker compose?

The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.


1 Answers

To use build instead image in docker-compose.override.yml you should replace image with local image name and then build this image with target.

In docker-compose.override.yml:


    image: username-repo-tag
    build:
      context: .
      target: username-repo-tag

In your Dockerfile you should add target to FROM tag:

FROM username/repo:tag as username-repo-tag
...
your instructions
...
like image 100
Alexey Stepanov Avatar answered Sep 17 '22 23:09

Alexey Stepanov