Mount volume Dockerfile or Docker compose

Hello my fellow Dockerteers,

I am new to Docker and currently trying to figure out the best approach to building a MySQL container to be used with a volume for persistent data.

From my research and studies I seem to be getting different advice from different sources on how to best do this.

It seems I can achieve this by both a Dockerfile or docker-compose.yml file.

So my understanding mount volumes can be done via these methods:

  • For Dockerfile:

VOLUME /var/lib/mysql

  • For docker-compose.yml file:

volumes:
./my-vol:/var/lib/mysql

So which to use, and to me it seems they both can do the similar things so why do both exist?
Although it seems with Dockerfile, it is a bit more limited as you cannot seem to specify the source drive, only the target drive?

So please recommend me the best practice/recommended method?