Reusing docker image in docker-compose - docker

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?

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

Related

How to make docker-compose build faster

I have two services in my docker-compose.yaml that use the same build context:
service_1:
image: 'service_1:latest'
container_name: service_1
env_file:
- ./service_dir/.env
ports:
- "8000:80"
build:
context: ./service_dir/
dockerfile: Dockerfile
volumes:
- ./service_dir/app:/app
command: /start-reload.sh
service_2:
container_name: service_2
build:
context: ./service_dir/
dockerfile: Dockerfile
command: python app/my_script.py
Building this takes forever. I assume it has something to do with the fact that the build context is taken twice to the docker daemon? I'm not sure if these commands could be run in the same service but I'd prefer to have two separate containers so I can e.g. follow service_2 logs more easily. Any suggestions for a good solution to my problem?

How to restart a specific service in docker

I was looking forward to know, how can i restart a specific service in a docker-compose.yaml file for example
version: '2.1'
volumes:
-grid-shared:
services:
service-A:
build:
context: .
dockerfile: ../../Dockerfile
service-B:
build:
context: .
dockerfile: ../../Dockerfile
service-C:
build:
context: .
dockerfile: ../../Dockerfile
After i run docker-compose up
I mad some changes in service-A Dockerfile file and now i want to sync my changes to only that container
You can use docker-compose restart service-A for restarting containers but am not sure if this will trigger a build.
You might end up using the same cached service.
But what will work for sure is docker-compose down service-A
and then docker-compose up --build service-A

Docker "Builder_container" exit before docker "Gather_container" process finished

I have two docker containers, when docker "builder_container" must wait for docker "Gather_container" to finish the process before starting.
Here is what the Docker-compose file looks like:
version: '3'
services:
Gather:
build:
dockerfile: ./Gather/Dockerfile
container_name: gather_container
image: gather_img
volumes:
- ./Gather:/app/
Builder:
build:
dockerfile: ./Builder/Dockerfile
container_name: builder_container
image: builder_img
volumes:
- ./Builder:/app/
depends_on:
- Gather

Run multiple commands services in yml file when docker-compose up

I want to run multiple command when I docker-compose up this service, but always failed
Win 10, Powershell, docker: 2.4.0.0, Engine: 19.03.13, Compose:1.27.4
version: '3.7'
services:
cypress:
container_name: tax_t_cypress
image: cypress/included:5.4.0
command: bash -c "npm i cypress-file-upload#5.0.3" && cypress run
volumes:
- ./:/tax
working_dir: /tax
environment:
- CYPRESS_BASE_URL=http://nginx:8888
depends_on:
- webpack
It will error
You passed: npm i cypress-file-upload#5.0.3
The error was: Cannot read property 'split' of undefined
If I use single command like command: cypress run, It's okay, But I really need install the dependency first in this image, How can I run multiple command in yml successfully.
if you need to install something on the image, you should do it on the image level, by writing a dockerfile and referencing it in your docker compose.
your dockerfile could be
FROM cypress/included:5.4.0
RUN npm i cypress-file-upload#5.0.3
CMD ["cypress" , "run"]
in your docker-compose.yml
version: '3.7'
services:
cypress:
container_name: tax_t_cypress
build:
context: .
dockerfile: dockerfile
volumes:
- ./:/tax
working_dir: /tax
environment:
- CYPRESS_BASE_URL=http://nginx:8888
depends_on:
- webpack

Having one docker file for multiple containers

Hi I have three repos each of them having docker-compose files with respective ports and need to have one docker compose file that would stand all these containers up. Is it possible to do so? I am quite new to docker and have exhauseted all searches online. Thanks in advance
version: '3'
services:
api:
container_name: container-name
restart: always
build: ./
ports: "8085":8085
stub:
container_name: test-stub
restart: always
build: ./
ports: "8086":8086
In the build option you can specify which dockerfile to use :
version: "3"
services:
my_service:
container_name: my_container
build:
context: .
dockerfile: my_container.Dockerfile

Resources