FROM ubuntu:latest
VOLUME /myVol
RUN echo "Hello World" > /myVol/message.txt
CMD cat /myVol/message.txt
When I run the container (after a successful image build), I get "Container cannot find file in volume- "No such file or directory". When I open an interactive session to the container, I see the volume, but no file (from the RUN echo..) in it. Why?
CodePudding user response:
The most visible effect of VOLUME
is to prevent any subsequent RUN
command from modifying the VOLUME
directory; this is mentioned under "Notes about specifying volumes" in the linked documentation.
For most purposes, you don't need VOLUME
. In particular you do not need to declare a directory a VOLUME
in the Dockerfile just because you expect an operator will eventually mount something there; when a container is run Docker volumes and host directories can be mounted on any container path regardless of whether or not it's a VOLUME
.
You should be able to safely delete the VOLUME
line with no consequences, and the file you're creating will actually exist.