I'm trying to create a new Docker image that no longer uses volumes from a running container that does use images. The volumes were created using docker-compose file, not Dockerfile. The problem is, when I launch a new container via new docker-compose.yml file it still has the volumes mapped. I still need to keep these volumes and the original containers/images that use them. Also, if possible I would like to continue to use the same docker image, just add a new version, or :latest. Here's the steps I used:
New version of an existing image:
docker commit <image id> existingImage:new-version
Create a new image from current running container:
docker commit <Image ID> newimage
Create new docker-compose.yml with no volumes defined and run docker-compose with a different project name
docker-compose -p <new project name>
Running without docker-compose, just use docker run:
docker run -d -p 8093:80 <img>:<version>
Any time I run any combination of these the volumes are still mapped from the original image. So my question is, how to I create a container from an image that once had mapped volumes but I no longer want to use the volumes?
Edit: Additional things I've tried:
- Stop container, remove container, restart docker, run docker compose again. No luck.
Edit 2: Decided to start over on the image. Using a base image, launched a container with an updated docker compose file that uses the now unrelated image. Run docker-compose -f up -d -> STILL has these same volumes mapped even though the image does not (and never has) any volumes mapped, and the current docker-compose.yml file does not map files. It looks like docker-compose caches what volumes are mapped for projects.
After searching for caching options in docker-compose, I came across this article: How to get docker-compose to always re-create containers from fresh images? which seems to solve the problem of caching images but not containers caching volumes
To remove volumes along with the containers used by
docker-compose
, usedocker-compose down -v
.To start containers with
docker-compose
, leave your existing volumes intact, but not use those volumes, you should change your project name. You can usedocker-compose -p new_project_name up -d
for that.Edit: here's an example showing how docker-compose does not reuse named volumes between different projects, but it does reuse and persist the volume unless you do a
down -v
:Volume is now populated, lets stop and start a new container to show it persist:
There's the expected persistent volume, lets run a different project at the same time to show the volume would be independent:
The volume is completely empty in the new project. Let's cleanup.
Note the volume is there in proj1 from before.
But doing a
down -v
deletes the volume.According to another SO post what I am trying to do is not possible. For future reference, one cannot attach volumes to an image, and then later decide to remove them. A new image must be created without the volumes instead. Reference: How to remove configure volumes in docker images