This should not be hard but I can't figure it out. I have a container running and used -v /tester
in the docker run command. I then do docker exec ti containername /bin/bash
. I then create a file called /tester/stayhere.txt with vi /tester/stayhere.txt
. I then save the file and verify it's there. Then I do a docker commit and start up the new image and the stayhere.txt is missing. What am I doing wrong?
When you mount a volume inside a docker container with -v
it's not considered part of container itself. That's why when you commit changes it's missing.
According to Docker documentations:
A data volume is a specially-designated directory within one or more containers that bypasses the Union File System.
And:
Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization.
What this means is, data volumes live outside of Union Filesystem and if you even delete that container data volume still persists:
Data volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically delete volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container.
So they are not part of your docker commit command.
In order to achieve what you want you need to copy file within the same path in your image while it's not mounted as docker volume. You can use Dockerfile or directly launch a container and alter filesystem then commit.
Next time when you launch a container with volume and you should be able to see those files.