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

  • MAC | Access application deployed on Docker from another application on VirtualBox machine
  • How to make sbt-native-packager combine two layers of docker image
  • Running impala sql script from Docker file
  • How to add multiple interfaces to docker container and set ip addresses manually?
  • link command hangs when linking container to itself
  • How can I run Neo4j with larger heap size, specify -server and correct GC strategy
  • 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.

  • Docker image size and how much space should I expect 'dnu restore' to take up?
  • Use Docker in Linux VMWare VM with Volumes?
  • Custom Docker Swarm Scheduler
  • connecting to exposed docker container
  • Ansible: no package available for docker-ce
  • Docker container migration
  • One Solution collect form web for “Dockerfile VOLUME not working”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.