8

I have a few images that share common code but are not exactly the same. Is there a way to create a base image in docker-compose so that it will not run itself when doing docker-compose up and I will be able to extend it in my Dockerfiles?

Here is example what I want to achieve:

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: ./php/Dockerfile

    php-fpm:
        build:
            context: .
            dockerfile: ./php-fpm/Dockerfile

    php-cron:
        build:
            context: .
            dockerfile: ./php-cron/Dockerfile

    php-worker:
        build:
            context: .
            dockerfile: ./php-worker/Dockerfile

Base dockerfile

FROM php:7.2-fpm-alpine

RUN docker-php-ext-install bcmath
... and other extensions

And the other dockerfiles (with small variations):

php-fpm

FROM my-docker-compose:php

RUN docker-php-ext-install php-fpm

CMD php-fpm 

php-cron

FROM my-docker-compose:php

COPY php/crontab /tmp/crontab
RUN /usr/bin/crontab -u www-data /tmp/crontab

CMD crond 
HubertNNN
  • 1,727
  • 1
  • 14
  • 29
  • Does this answer your question? [Docker-compose.yml file that builds a base image, then children based on it?](https://stackoverflow.com/questions/45173574/docker-compose-yml-file-that-builds-a-base-image-then-children-based-on-it) – Sebastian Wagner Apr 19 '22 at 10:19

1 Answers1

1

You would typically accomplish this by storing your base images in a Docker registry. You can either store them in the public registry (https://hub.docker.com) or some private registry (either hosted in the cloud our on premise).

Here is more information on Docker registry: https://docs.docker.com/registry/

Some additional information on using base images: https://docs.docker.com/develop/develop-images/baseimages/

RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • 4
    I know about the registry, but that is for stable finished images. I am looking for a local development solution that will be handled 100% on my computer possibly in a single docker-compose. – HubertNNN Oct 10 '18 at 12:36