Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 937
Sebastian vom Meer Avatar asked Oct 12 '15 18:10

Sebastian vom Meer


1 Answers

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.

like image 155
dnephin Avatar answered Nov 15 '22 06:11

dnephin