Leem.fin
Leem.fin

Reputation: 42672

My confusion in docker volume usage in practice

I see some people define a volume like this (I only put part of the Dockefile below to explain my question clearly):

WORKDIR /usr/src/app
COPY . . 
VOLUME . /usr/src/app

So, in the VOLUME line, the . means the current directory which is the WORKDIR defined above. Then, 2nd part for the VOLUME is /usr/src/app. Is it a convention? It sounds like the same path defined as volume twice to me. Do I misunderstand it?

Another question is what is the benefit of defining volume /usr/src/app? Is the benefit that when making changes in the project it could be reflected in all containers from the same image (because the COPY . . copied the project folder to the container)?

Upvotes: 2

Views: 318

Answers (1)

Leonardo Dagnino
Leonardo Dagnino

Reputation: 3225

That seems like a redundancy - in Dockerfiles, VOLUME is just a declaration to copy files from the image to a volume mounted at that location. It cannot specify the directory in the host, that can only be done at runtime.

VOLUME can take multiple arguments, and declares volumes in all the locations passed - so if you specify the same directory multiple times, it should have no practical effect, despite having two equivalent paths listed.

It could make a difference if you changed the WORKDIR after that line, or in a subsequent image built from that one, since the relative path is resolved only when running the container.

Upvotes: 3

Related Questions