dockerdockerfile

Understanding "VOLUME" instruction in DockerFile


Below is the content of my "Dockerfile"

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app

# Change working dir to /usr/src/app
WORKDIR /usr/src/app

VOLUME . /usr/src/app

RUN npm install

EXPOSE 8080

CMD ["node" , "server" ]

In this file I am expecting VOLUME . /usr/src/app instruction to mount contents of present working directory in host to be mounted on /usr/src/app folder of container.

Please let me know if this is the correct way?


Solution

  • The official docker tutorial says:

    A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:

    • Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point,
      that existing data is copied into the new volume upon volume
      initialization. (Note that this does not apply when mounting a host
      directory.)

    • Data volumes can be shared and reused among containers.

    • Changes to a data volume are made directly.

    • Changes to a data volume will not be included when you update an image.

    • Data volumes persist even if the container itself is deleted.

    In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app.

    When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image, you may but do not have to specify its mounting point (/opt) on the host machine. If you do not specify --volume argument then the mount point will be chosen automatically, usually under /var/lib/docker/volumes/.