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

  • Docker logs retrieval from MongoDB
  • increase max_allowed_packet size in mysql docker
  • Docker API to automate pull and run
  • Deploying and Accessing Database Inside Docker Container
  • How to stop/relaunch docker container without losing the changes?
  • Running Docker in Docker with Flask Server as “outer” container
  • 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.

  • Communication between linked docker containers
  • ManageIQ web application inside docker container can’t connect to Hawkular outside container
  • Copy files from ftp to docker container
  • Kafka Docker network_mode
  • “Error response from daemon: 404 page not found” While using docker command
  • Docker container running tomcat - could not access the server using the host IP address
  • 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.