Where should I mention volumes?


I’m trying to learn from scratch so I’m building my own images to gain some experience but i am confused about how to declare volumes.

I create volumes for “data containers” so that I can share the volume with another container who will use the share to add persistent data (/var/www, /var/lib/mysql, …)

Something like

docker create -it --name=db-data --volume=/var/lib/mysql test/centos7:7.1.1503 bash
docker run -d --name=db -p 3306:3306 --volumes-from=db-data -e MYSQL_ROOT_PASSWORD="test" test/mariadb55:5.5.41 mysqld_safe

But now that I am playing with Dockerfiles, I see there is also mention of volumes

VOLUME /var/lib/mysql

I do not understand the difference between a volume specified in a Dockerfile and one specified on the CLI.

Can you help?

VOLUME in CLI is almost same as one in Dockerfile.

VOLUME in Dockerfile has following properties:

  1. Can specify arguments in a JSON array or string
    e.g. VOLUME [“myvol”, “myvol2”]
  2. Cannot map volumes to host directories
  3. Volumes are initialized when the container is executed

I hope this helped.

Thanks for your answer! I will have to play with it I guess.

My confusion comes from when we use volumes-from. Having it already specified at the image level gives the impression it might squash the volumes-from parameter with the volume mentioned in the Dockerfile of the image.

I guess I need to play around with it. I’m enjoying it though!

Hi Edoardo,

volumes-from is used against the data-container and is only available while running the contianer, however VOLUME in Dockerfile only allows to mount the data folder.
Even if you specify mount folder via Dockerfile, you can still use volumes-from with the docker CLI during the runtime ( docker run ).