Docker-compose cascade build images

荒凉一梦 提交于 2020-02-06 04:36:07

问题


I would like to build a set of images, one depending on the other. For example, I'd like to create image A, containing source code and SDKs, performing a build. Then, I'd like to create image B, containing runtimes and some binaries retrieved from machine A, to run the user front-end of a web application. Finally, I'd like to create image C, containing runtimes and some binaries retrieved (again) from machine A, to run administrative commands for the web application.

Summarizing, here's what I 'd like to do:

  • Create machine A; copy source files from host to machine A, perform build
  • Create machine B; copy files (binaries) from machine A to machine B
  • Create machine C; copy files (binaries) from machine A to machine C
  • throw machine A away
  • run machine B
  • run machine C

In such a situation, I should be able to create machine A from a dockerfile and give it a name and a tag; next I need to reference it from the other dockerfiles.

I tried to specify the image name and tag in the docker-compose file: image: myRepo/imageA:Images.A

Only, I cannot retrieve it from dockerfiles B and C. Is it necessary for me to build a repo machine and store machine A on it? Isn't it possible to avoid this step?


回答1:


The layout you describe is reasonable, but it can't be directly expressed to Docker Compose. (It doesn't have any way to express images that aren't attached to running containers or to express dependencies between different image builds.) You'd have to manually rebuild your base image when you need to

docker build -f Dockerfile.sdk -t image-a .

Your other Dockerfiles can start with

FROM image-a

There's no particular need to push this image to a repository, so long as you've correctly built and tagged the image and it exists locally you can start another Dockerfile FROM the image.

And then you can docker-compose up --build as normal.

You can "teach" Docker Compose about this, but only by adding an additional service you don't expect to actually have running.

services:
  a:
    build:
      context: .
      dockerfile: Dockerfile.sdk
    image: image-a
    command: /bin/true  # exit immediately

The docker-compose build documentation makes no statement about the order containers are built in (or even if they're guaranteed to be built serially) so again you'll need to manually build it

docker-compose build a
docker-compose up --build

It's also worth considering whether you need separate "application" and "tools" images. My intuition is that, in most images, the size of the base image and the language runtime is vastly larger than the size of the application and especially a single entry point into the application. If a single image with a couple of extra small tool commands meets your needs, then what you describe is a very typical use of a multi-stage build. To run a tool command you can either docker-compose run one of the services declared in your docker-compose.yml file with an alternate command, or specify both a build: and a target image: name in docker-compose.yml and docker run the resulting image.



来源:https://stackoverflow.com/questions/59714123/docker-compose-cascade-build-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!