Reputation: 41665
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?
Upvotes: 3
Views: 1070
Reputation: 59946
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
Upvotes: 4
Reputation: 18578
you need to use the same :
image: myimage:mytag
for the both services
then docker-compose build
will use cache
Upvotes: 2