Assuming I have a set of images which depend on a common base image:
base (this is only a set of common dependencies)
FROM ubuntu:16.04
ENV FOO 1
child1
FROM mybaseimage # where mybaseimage corresponds to base
CMD ["bar1_command"]
child2
FROM mybaseimage # where mybaseimage corresponds to base
CMD ["bar2_command"]
Is it possible to create docker-compose
file which would build base
without running it? Lets say I have following dependencies:
version: '2'
services:
child1:
build: ./path-to-child1-dockerfile
services:
child2:
build: ./path-to-child2-dockerfile
depends_on:
- child1
I would like base
to be build even if it is not explicitly started. Is something like this even possible? Or should I simply use external Makefile to build dependencies?
build_base:
docker build -t mybaseimage mybaseimage
build_all: build_base
docker-compose build
depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.
Yes, answer is "true" to both questions. If you start 2 (or more) containers on the same host, all using the same base image, the whole content of the base image will be shared.
It's a cool tool to make handling container configuration or multiple interconnected containers a bit easier. The “don't use docker-compose in production” statement is motivated by hidden assumptions which are not necessarily valid for everybody, and propagated by unclear communication.
To use multiple override files, or an override file with a different name, you can pass the -f option to the docker-compose up command. The base Compose file has to be passed on the first position of the command.
It's possible. There's a kind of workaround. You're close, but you were missing explicit image tags (so you had little ability on child images to declare which image you inherited from).
version: '3.2'
services:
base:
image: mybaseimage
build: ./path-to-base-dockerfile
child1:
build: ./path-to-child1-dockerfile
depends_on:
- base
child2:
build: ./path-to-child2-dockerfile
depends_on:
- base
Let's say you have no images built. You run docker-compose up
. The following things will happen:
mybaseimage
. It knows how to build mybaseimage
(you gave it a build path), so it will build it now, and tag it as mybaseimage
.base
service.
base
so that it quits immediately, or has no entrypoint. since we don't actually want it to run this service.child1
. It knows how to build child1
(you gave it a build path), so it will build it now, and tag it as child1
.child1
serviceThe next docker-compose up
will be simpler (we have tagged images available, so we skip all build steps).
If you already have tagged images, and want to rebuild: use docker-compose build
to tell it to build all images (yes, base and children will both be rebuilt).
Use a Makefile. docker-compose is not designed to build chains of images, it's designed for running containers.
You might also be interested in dobi which is a build-automation tool (like make) designed to work with docker images and containers.
Disclaimer: I'm the author of dobi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With