How to control docker-compose build order?

2020-02-26 04:30发布

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?

3条回答
萌系小妹纸
2楼-- · 2020-02-26 05:04

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
查看更多
萌系小妹纸
3楼-- · 2020-02-26 05:12

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

查看更多
冷血范
4楼-- · 2020-02-26 05:13

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.

查看更多
登录 后发表回答