I use some images straight from docker hub and for some I run a few additional commands.
From time to time I want to upgrade all my containers to use the latest image available.
For the containers I run some additional commands how can I run the dockerFile commands only when the source image has a new version available?
To clarify, here is an example
- /opt/test/docker/
docker-compose.yaml
version: '3.0'
services:
service1:
container_name: service1
image: alpine
service2:
container_name: service2
build:
context: .
dockerfile: service2.Dockerfile
- /opt/test/docker/
service2.Dockerfile
FROM alpine:latest
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]
I first start the containers with
docker-compose --project-directory /opt/test/docker up --detach
Pulling service1 (alpine:)...
latest: Pulling from library/alpine
213ec9aee27d: Pull complete
Digest: sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad
Status: Downloaded newer image for alpine:latest
Building service2
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM alpine:latest
---> 9c6f07244728
Step 2/3 : RUN apk add --no-cache mysql-client
---> Running in 87e2a5c87b22
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/7) Installing mariadb-common (10.6.9-r0)
(2/7) Installing libgcc (11.2.1_git20220219-r2)
(3/7) Installing ncurses-terminfo-base (6.3_p20220521-r0)
(4/7) Installing ncurses-libs (6.3_p20220521-r0)
(5/7) Installing libstdc++ (11.2.1_git20220219-r2)
(6/7) Installing mariadb-client (10.6.9-r0)
(7/7) Installing mysql-client (10.6.9-r0)
Executing busybox-1.35.0-r17.trigger
OK: 39 MiB in 21 packages
Removing intermediate container 87e2a5c87b22
---> 6d5f43603eb0
Step 3/3 : ENTRYPOINT ["mysql"]
---> Running in cb27de950226
Removing intermediate container cb27de950226
---> 1d8ca02b8bb7
Successfully built 1d8ca02b8bb7
Successfully tagged docker_service2:latest
WARNING: Image for service service2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating service1 ... done
Creating service2 ... done
When I want to update them, I stop and remove them with
docker-compose --project-directory /opt/test/docker down --remove-orphans
Removing service2 ... done
Removing service1 ... done
, then I start them with build
docker-compose --project-directory /opt/test/docker up --detach --build
Building service2
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM alpine:latest
---> 9c6f07244728
Step 2/3 : RUN apk add --no-cache mysql-client
---> Using cache
---> 6d5f43603eb0
Step 3/3 : ENTRYPOINT ["mysql"]
---> Using cache
---> 1d8ca02b8bb7
Successfully built 1d8ca02b8bb7
Successfully tagged docker_service2:latest
Creating service1 ... done
Creating service2 ... done
But the dockerFile commands for service2 are run again even though the Alpine image version has not changed.
How can avoid this build and keep the built image I already had?
Does docker-compose has a built-in mechanism for this?