简体   繁体   中英

MySQL image ignores volume configuration of docker-compose.yml

Using the official MySQL Docker image, I don't understand how to mount the data directory to a specifc point on the host. The Dockerfile of the image sets

VOLUME /var/lib/mysql

so database data should be stored "somewhere" on the host. I want to be more specific in my docker-compose file, so I tried the following:

mysql:
  image: mysql:5.7
  environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_DATABASE=mydb
  volumes:
    - ./database/mysql:/var/lib/mysql

Starting with docker-compose up everything works fine, but the ./database/mysql directory on the host stays empty, whereas /var/lib/mysql in the container contains data. Is there a problem in my configuration? Or do I misunderstand how to use volumes?

docker-compose will always try to preserve data volumes, so that you don't lose any data within them. If you started with a data volume, then changed to a host volume, you may still get the data volume.

To correct this, run docker-compose stop && docker-compose rm -f , which will remove your containers and your data volumes (this will erase any data in your data volumes). On the next docker-compose up , you should see it using the host volume.

Edit: As of Compose 1.6 you can run docker-compose stop -v instead of the two commands above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM