Creating Volume Mount from Dockerfile

Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 1 5 min


Volumes are special directories in a container

Volumes can be declared in two different ways.

VOLUME /uploads
$ docker run -d -v /uploads myapp

In both cases, /uploads (inside the container) will be a volume.

Create a file with name volume and paste the following content to it:

FROM ubuntu:latest
RUN mkdir /data
RUN echo "Hello from Volume" > test
VOLUME /data

Execute the following commands:

docker build -f volume -t collabnix/volume:1 .

docker run collabnix/volume:1

docker volume ls

cd /var/lib/docker/volumes

ls -ltr


cd ./_data
cat test