Docker Community Forums

Share and learn in the Docker community.

Docker-compose volume

Good afternoon! I don’t understand how, in the official mysql image, when you start the container, the data that / var / lib / mysql (volume) are all files are automatically forwarded to the host machine, how does it work?

Would you please share your docker-compose.yml? Your post doesn’t mention important facts and leaves them to the imagination.

version: ‘3.3’

services:
test_db:
image: “mysql/mysql-server:5.7”
container_name: test_db
restart: always
ports:
- 13306:3306
volumes:
- ./db:/var/lib/mysql
environment:
- TZ=${TZ}
- MYSQL_USER=123456
- MYSQL_PASSWORD=123
- MYSQL_DATABASE=123
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_ROOT_HOST=%

This is where you declare a bind-mount, which results that everything inside the containre folder /var/lib/mysql will be stored in the subfolder db, relative to the the folder where you docker-compose.yml is located.

See: https://docs.docker.com/compose/compose-file/compose-file-v2/#volumes for the documentation in the docker compose reference v2; If you are interessted in further details check https://docs.docker.com/storage/bind-mounts/ - even though it does not focus on the docker-compose.yml syntax, they are the very same concepts.

1 Like

Hello Impkos!
volumes:

  • ./db:/var/lib/mysql
    You was mount db Folder to var/lib/mysql
    Means Yours In your db Folder use access var/lib/mysql all data.