The VOLUME
command in a Dockerfile
is quite legit, totally conventional, absolutely fine to use and it is not deprecated in anyway. Just need to understand it.
We use it to point to any directories which the app in the container will write to a lot. We don't use VOLUME
just because we want to share between host and container like a config file.
The command simply needs one param; a path to a folder, relative to WORKDIR
if set, from within the container. Then docker will create a volume in its graph(/var/lib/docker) and mount it to the folder in the container. Now the container will have somewhere to write to with high performance. Without the VOLUME
command the write speed to the specified folder will be very slow because now the container is using it's copy on write
strategy in the container itself. The copy on write
strategy is a main reason why volumes exist.
If you mount over the folder specified by the VOLUME
command, the command is never run because VOLUME
is only executed when the container starts, kind of like ENV
.
Basically with VOLUME
command you get performance without externally mounting any volumes. Data will save across container runs too without any external mounts. Then when ready simply mount something over it.
Some good example use cases:
- logs
- temp folders
Some bad use cases:
- static files
- configs
- code