Reputation: 40464
I was reading the following https://developer.atlassian.com/blog/2016/06/common-dockerfile-mistakes/ and got caught up on a section:
Volumes in your image are added when you run your container, not when you build it. You should never interact with your declared volume in your build process as it should only be used when you run your container.
Example: Creating a file in my build process and then running cat on the file once the image is run works fine:
FROM ubuntu:12.04 RUN echo "Hello Charlie!" > /test.txt CMD ["cat", "/test.txt"] $ docker run test-build-volume Hello Charlie!
If I attempt to do the same thing for a file stored in a volume then it won't work:
FROM ubuntu:12.04 RUN echo "Hello Charlie!" > /var/data/test.txt CMD ["cat", "/var/data/test.txt"] $ docker run test-build-volume cat: can't open '/var/data/test.txt': No such file or directory
I don't see what is wrong with the second example? Is it incorrect (typo?). The only difference I see is /test.txt
replaced with /var/data/test.txt
, I fail to see the problem they should work the same? Is there something fundamental in Docker I'm missing?
Upvotes: 1
Views: 11654
Reputation: 25152
GSUgambit is right about echo
silently failing, but (deleted the relative post).
IMHO the example is not verifiable, it lacks the details of how to Start a container with a volume, doesn't make clear that /var/data
doesn't exist at the time of build and that it is later created when you run a container (see case 1 below).
A few details which might help:
Source
, Destination
fields & 2 different cases:When you docker inspect
a container with a volume notice the two fields:
Source
(host location of files)Destination
(container location of files)for example:
"Mounts": [
{
"Type": "volume",
"Name": "myvol2",
"Source": "/var/lib/docker/volumes/myvol2/_data",
"Destination": "/app",
...
}
],
destination
location doesn't existIn this case, docker run ...
with a volume option will create a new destination directory in the container, for example:
$ docker run --rm -it -v test_volume:/this_folder_will_be_created ubuntu
root@6b80b9895dcc:/# cd /this_folder_will_be_created/
root@6b80b9895dcc:/this_folder_will_be_created#
destination
location existsPopulate a volume using a container
If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as
/app/
above), the directory’s contents are copied into the volume.
Upvotes: 1
Reputation: 538
/var/data does not exists. Your docker build must be giving this warning.
Try replace /var/data with /var/log This will work.
Upvotes: 0