2

my docker-compose.yml file is below.

I want to build parallel those images - I am running the a command docker-compose build --parallel

BUT I want to run a command before it builds the images of service2 & service3 while building service1 - parallel.

When the command will be finished it will join to the building-parallel-process.

version: '3.4'
  services:
    service1:
      image: "company/service1:${TAG}"
      build:
        context: ./folder/service1/
        dockerfile: Dockerfile
    service2:
      image: "company/service2:${TAG}"
      build:
        context: ./folder/service2/
        dockerfile: Dockerfile
    service3:
      image: "company/service3:${TAG}"
      build:
        context: ./folder/service3
        dockerfile: Dockerfile

1 Answer 1

3

Compose doesn't really have any sort of workflow handling like this, especially around building images. It's assumed that building an image only depends on the local source tree and nothing else. Compose also doesn't have any ability to run non-Docker commands or launch temporary containers as part of the up workflow.

The good news is that re-running a build is very quick if nothing has changed. So with the workflow you've described, you might separately build the first image, run the command, and then rebuild everything; re-rebuilding the first image will take almost no time and you won't get a new image.

#!/bin/sh

# Build the one image that needs special handling
docker-compose build service1

# Run the command
the_command

# Rebuild everything in parallel (service1 will be a no-op)
docker-compose build --parallel

If you can run the preparatory step in a Dockerfile RUN command that might be easier to manage. If that needs software that isn't ordinarily part of your image, you could use a multi-stage build to do it in effectively a temporary image.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.