'docker-compose down --rmi all with containers sharing image
I have a a docker compose file, which builds some services using a Dockerfile which is stored with it, the compose looks something like this
version: '3.5'
services:
my_app:
build: ""
image: my_image
....
...
my_two:
image: my_image
...
my_three:
image: my_mage
...
Now when I run docker-compose up what happens is the image is built (build: "" find the Dockerfile and builds it and names it "my_image") and then the other two (my_two, my_three) are using the already built image.
So far so good.
The problem is that when I run docker-compose down --rmi all it successfully removes my_image but then retries two times to remove the image again, because they are associated with other services.
The operation does what it needs to do, stopping and removing all containers and removing the images but the problem is that it displays error messages - which I want to avoid as I am wrapping some software around it.
How can I make docker-compose remove the image only once?
Solution 1:[1]
It might be easier to define all three services with build instead of image.
When you run docker-compose up, it will first build the first service, i.e. this bit is unchanged. What changes is that it then builds the second and third services - but it will be able to reuse the cached layers from the initial build, so you actually end up with one image with three different tags.
Then, when you come to take the images down, docker-compose down --rmi local can safely remove each 'image' in turn: the first time it actually just untags it, ditto the second time, and then the third time will remove the image for real.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vince Bowdren |
