简体   繁体   中英

Reusing docker image in docker-compose

I've seen the following in https://www.distributedpython.com/2018/11/15/celery-docker/

which reuses the built image I believe

services: 
  worker:
    build: .
    image: &img worker 

  beat:
    build: .
    image: *img

Since I'm using dockerfile, I can do something like this, I see it's rebuilding the image ( pip install in Dockerfile runs twice for each service)

 services:
   worker:
     build:
       context: ../../
       dockerfile: ./retention/docker/celery/Dockerfile
     image: &img worker
     container_name: celery
     # command: [celery, worker, --app=app1, --loglevel=INFO]


   beat:
     build:
       context: ../../
       dockerfile: ./retention/docker/celery/Dockerfile
     image: *img
     # command: [celery, beat, --app=app1, --loglevel=INFO]

How can I build 1 image and reuse it?

you need to use the same :

image: myimage:mytag

for the both services

then docker-compose build will use cache

You can re-use the same image in the same docker-compose but the order will mater, what if the image is not built yet and service B starting with that image that not exist yet which is supposed to be built in service A stage?

So add depends_on will help this race case also remove the build context from the beat service.

version: '3.7'
services:
  worker:
    image: worker_beat
    build:
      context: ../../
      dockerfile: ./retention/docker/celery/Dockerfile
    container_name: celery
    command: [celery, beat, --app=app1, --loglevel=INFO
  beat:
    image: worker_beat
    depends_on:
      - worker 
    command: [celery, beat, --app=app1, --loglevel=INFO]

to run your stack you will just need docker-compose up --build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM