Kim
Kim

Reputation: 5425

How to control docker-compose build order?

I have three services to build, A、B and C. A should be built in the very first place, because B and C depend on A (they import A as image). I thought they should be built in order but I just find out they are built in some random order?

So, how do I control build order in docker-compose.yml?

Upvotes: 40

Views: 31589

Answers (5)

ShinChven
ShinChven

Reputation: 502

Since compose v2, BuildKit is enabled by default, it will build images parallelly.

By disabling BuildKit you get to build images according to the order in your docker-compose.yml file.

DOCKER_BUILDKIT=0 docker-compose build

If you still want to build images parallelly, you can consider defining services in multiple docker-compose.yml files for building, then composing up in another docker-compose.yml file for deployment.

Upvotes: 5

Display name
Display name

Reputation: 926

If you need to run one service before an other you can use docker-compose run bar after setting depends_on: - foo in your docker-compose.yml file.

For example:

# docker-compose.yml

  services:
    foo:
        . . . 
    bar:
         . . . 
      depends_on:
        - foo

then run,

# terminal

user@Name Docker % docker-compose run bar

However, per the docs, this will only control the order in which processes start, not the order in which they finish. This can often lead to situations in which dependent services do not properly start. Potential solutions for this include using wait-for-it, wait-for, or Dockerise. I recommend reading the docs before implementing any of the solutions listed above, as there are caveats.

Upvotes: 3

Quisse
Quisse

Reputation: 778

As stated in https://github.com/docker/compose/blob/e9220f45df07c1f884d5d496507778d2cd4a1687/compose/project.py#L182-L183

Preserves the original order of self.services where possible, reordering as needed to resolve dependencies.

So for me it worked by manually sorting the services with the depending services at first and following the services which is used by the other and the other as last.

Example

version: '3'
services:
    mydb:
        image: mydb
    serviceA:
        build: 
            dockerfile: DockerfileA
        depends_on:
            - mydb
    serviceB:
        build:
            dockerfile: DockerfileB # which contains 'FROM serviceA'
        depends_on:
            - mydb

Source: https://github.com/docker/compose/issues/5228#issuecomment-565469948

Upvotes: 0

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38932

Update: In recent practice, I have found my answer to only pertain to run ordering.
Refer to the answer by Quinten Scheppermans and the comment by Authur Weborg about dobi.

You can control build order using depends_on directive.

services:
  ...

  B:
    depends_on:
      - A
  C:
    depends_on:
      - A

Upvotes: 15

Quinten Scheppermans
Quinten Scheppermans

Reputation: 1094

The accepted answer above does not seem correct.

As seen in the documentation

There are several things to be aware of when using depends_on:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

Version 3 no longer supports the condition form of depends_on.

The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

Upvotes: 22

Related Questions