darxtrix
darxtrix

Reputation: 2040

Dockerfile VOLUME not working

I am really stuck with the usage of docker VOLUME's. I have a plain dockerfile:

FROM ubuntu:latest  
VOLUME /foo/bar
RUN touch /foo/bar/tmp.txt

I ran $ docker build -f dockerfile -t test . and it was successful. After this, I interactively ran a shell into the docker container associated with the run of the created test image. That is, I ran $ docker run -it test

Observations:

/foo/bar is created but empty.

docker inspect test mounting info:

"Volumes": {
  "/foo/bar": {}
}

It seems that it is not mounting at all. The task seems pretty straight but am I doing something wrong ?

EDIT : I am looking to persist the data that is created inside this mounted volume directory.

Upvotes: 4

Views: 12557

Answers (1)

michael_bitard
michael_bitard

Reputation: 4212

The VOLUME instruction must be placed after the RUN.

As stated in https://docs.docker.com/engine/reference/builder/#volume :

Note: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

If you want to know the source of the volume created by the docker run command:

docker inspect --format='{{json .Mounts}}' yourcontainer

will give output like this:

[{
  "Name": "4c6588293d9ced49d60366845fdbf44fac20721373a50a1b10299910056b2628",
  "Source": "/var/lib/docker/volumes/4c6588293d9ced49d60366845fdbf44fac20721373a50a1b10299910056b2628/_data",
  "Destination": "/foo/bar",
  "Driver": "local",
  "Mode": "",
  "RW": true,
  "Propagation": ""
}]

Source contains the path you are looking for.

Upvotes: 6

Related Questions