如果$ docker-compose down使用以下命令运行,我似乎无法使MySQL数据持久化.yml
$ docker-compose down
.yml
version: '2' services: # other services data: container_name: flask_data image: mysql:latest volumes: - /var/lib/mysql command: "true" mysql: container_name: flask_mysql restart: always image: mysql:latest environment: MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this MYSQL_USER: 'test' MYSQL_PASS: 'pass' volumes_from: - data ports: - "3306:3306"
我的理解是,在我的data容器中,使用volumes: - /var/lib/mysql将其映射到我的本地计算机目录,在该目录中mysql将数据存储到容器中,由于这种映射,即使容器被破坏,数据也应持久存在。而且mysql容器只是进入db的客户端接口,可以看到本地目录,因为volumes_from:- data
data
volumes: - /var/lib/mysql
mysql
volumes_from:- data
编辑
改变了我.yml如下图所示,并创造了该目录./data,但现在当我运行docker-compose up --build的mysql容器不会开始抛出错误说
./data
docker-compose up --build
data: container_name: flask_data image: mysql:latest volumes: - ./data:/var/lib/mysql command: "true" mysql: container_name: flask_mysql restart: always image: mysql:latest environment: MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this MYSQL_USER: 'test' MYSQL_PASS: 'pass' volumes_from: - data ports: - "3306:3306" flask_mysql | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied) flask_mysql | 2016-08-26T22:29:21.182144Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). flask_mysql | 2016-08-26T22:29:21.185392Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
数据容器是一个多余的解决方法。数据量将为您解决问题。将您更改docker- compose.yml为:
docker- compose.yml
version: '2' services: mysql: container_name: flask_mysql restart: always image: mysql:latest environment: MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this MYSQL_USER: 'test' MYSQL_PASS: 'pass' volumes: - my-datavolume:/var/lib/mysql volumes: my-datavolume:
Docker将在/var/lib/docker/volumes文件夹中为您创建卷。只要您不输入内容,此音量就会持续存在docker-composedown -v
/var/lib/docker/volumes
docker-composedown -v