Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose persistent data MySQL

I can't seem to get MySQL data to persist if I run $ docker-compose down with the following .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" 

My understanding is that in my data container using volumes: - /var/lib/mysql maps it to my local machines directory where mysql stores data to the container and because of this mapping the data should persist even if the containers are destroyed. And the mysql container is just a client interface into the db and can see the local directory because of volumes_from: - data

Attempted this answer and it did not work. Docker-Compose Persistent Data Trouble

EDIT

Changed my .yml as shown below and created a the dir ./data but now when I run docker-compose up --build the mysql container wont start throws error saying

  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. 
like image 904
Adam Avatar asked Aug 26 '16 22:08

Adam


People also ask

Is MySQL docker persistent?

According to Docker official documentation: Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).

Is docker compose persistent?

Whichever you choose, once you have set up a volume to the folder where the data is stored in the container, if you do a docker-compose down , and then a docker-compose up , your data will not be erased and it will become persistent.

Where does MySQL docker store data?

The MySQL Docker image is configured to store all its data in the /var/lib/mysql directory. Mounting a volume to this directory will enable persistent data storage that outlives any single container instance.

Is MySQL a persistent database?

By default, MySQL stores its data files in the /var/lib/mysql directory. In order to persist data with a MySQL service we must mount a persistent volume into that directory.


2 Answers

The data container is a superfluous workaround. Data-volumes would do the trick for you. Alter your docker-compose.yml to:

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 will create the volume for you in the /var/lib/docker/volumes folder. This volume persist as long as you are not typing docker-compose down -v

like image 145
Ohmen Avatar answered Sep 18 '22 07:09

Ohmen


There are 3 ways:

First way

You need specify the directory to store mysql data on your host machine. You can then remove the data container. Your mysql data will be saved on you local filesystem.

Mysql container definition must look like this:

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:  - /opt/mysql_data:/var/lib/mysql ports:   - "3306:3306" 

Second way

Would be to commit the data container before typing docker-compose down:

docker commit my_data_container docker-compose down 

Third way

Also you can use docker-compose stop instead of docker-compose down (then you don't need to commit the container)

like image 37
Bukharov Sergey Avatar answered Sep 17 '22 07:09

Bukharov Sergey