Docker-compose.yml file that builds a base image, then children based on it? Docker-compose.yml file that builds a base image, then children based on it? docker docker

Docker-compose.yml file that builds a base image, then children based on it?


Yes, kind of. Use it like this:

version: '2'services:    wls-admin:        container_name: wls-admin        image: weblogic-domain        build:            context: wls-admin            args:                - ADMIN_PORT=${WLS_ADMIN_PORT}                - CLUSTER_NAME=${WLS_CLUSTER_NAME}                - PRODUCTION_MODE=dev        networks:            - wls-network

image clause here makes docker-compose build generate docker image named weblogic-domain for this service. This image can be re-used by other services' Dockerfiles, even in the same build process.


As per the documentation the build option of a service takes a directory as an argument which contains the famous Dockerfile. There is no way to build a base image and then the actual image of the service.

Docker is a environment in which your application runs. When you are creating a base image, it should have things which are not going to change often. Then you need to build baseiamge once and upload to your repository and use FROM baseimage:latest in the Dockerfile.

For example, if you are building a python application you can create it from python and install requirements:

FROM python:3.6COPY requirements.txt .RUN pip install -r requirements.txt

here, python:3.6 is the base image which is not going to change often and thus you need not build it every time you are running docker compose commands.


Just a minor addition to Kanedias' answer. If you choose to follow his approach (which was my choice), you can avoid instantiating a container for the base image with the --scale flag from the docker-compose up command:

docker-compose up --scale wls-admin=0

From the up command documentation:

    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the                               `scale` setting in the Compose file if present.

One important thing to note is that the scale setting in the docker-compose.yml was removed in v3, so there is actually nothing to override in v3.