hainabaraka
hainabaraka

Reputation: 837

multiple docker services from same Dockerfile - how to utilize caching

I have a series of docker services that uses the same base Dockerfile, e.g.:

services:
  service_1:
    build:
      context: .
    ...
  service_2:
    build:
      context: .
    ...

On Dockerfile, I run a few installations:

FROM base_image

RUN apk add --no-cache \
        g++ \
        git \
...

How do I make sure that build for Dockerfile takes place only once? That is, after the Dockerfile build for service_1, the service_2 uses a cached result?

Upvotes: 0

Views: 135

Answers (1)

If you run docker-compose build without any arguments, it should simply use your locale cache already.

Though, on the other hand, there might also be some other options, depending on your real usage.

In case you can live with only one definition in the compose file (everything is the same) as here:

services:
  app:
    build:
      context: .

You can also simply start with docker-compose up -d --scale app=5 to get 5 instances running.

An other option is to re-use the image, as written here, though like this I never tried.

services:
  service_1:
    build:
      context: .
    image: base_service
    depents_on:
      - app_base # to make shure it is builded
    ...
  service_2:
    image: base_service
    depents_on:
      - app_base # to make shure it is builded
    ...

Upvotes: 1

Related Questions