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

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.6
COPY 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.


Instead of running docker-compose, you can implement a script, witch builds image with specific tag docker build ... -t your_tag, then runs docker-compose. In children dockerfiles you can use FROM your_tag.


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.