3

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 just one image and reuse it?

1
  • I find it actually rebuild,, Commented Nov 25, 2019 at 14:15

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This right here should be accepted answer ^
2

you need to use the same :

image: myimage:mytag

for the both services

then docker-compose build will use cache

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.