본문 바로가기
프로그램 활용/클라우드 가상화 도커

Mysql Container Volume 통해 Docker Volume을 이해 해보자

by 3604 2023. 8. 7.
728x90

출처: https://steemit.com/mysql/@jaerakson/mysql-container-volume-docker-volume

도커 볼륨은 아래의 4가지 테스트를 통해 이해 해보자

  1. Mysql Container 존재 할 경우 Volume 삭제 되지 않음
  2. Mysql Container 삭제 해도 Volume 삭제 되지 않음
  3. 불륨은 강제로 삭제 할 수 있다. ($ docker volume prune)
  4. 생성된 볼륨을 다른 Container 연결 할 수 있다

2번의 내용으로 알수 있는 사실은
mysql 컨테이너 삭제 하더라도
mysql 컨테이너의 볼륨은 삭제 되지 않았기 때문에
mysql 컨테이너 생성시 볼륨정보만 알면 복원 가능하다.
docker run -d -it --name mysql2 -p 5306:3306 -v /var/lib/docker/volumes/ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134/_data/:/var/lib/mysql mysql

아래의 내용 테스트 결과 입니다.

mysql Container 생성

$ docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD="1111" mysql

Mysql Container volume 정보

$ docker inspect -f '{{ .Mounts }}' mysql

[{bind /var/lib/docker/volumes/ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134/_data
/var/lib/mysql true }]

$ docker inspect mysql

"Mounts": [
{
"Type": "volume",
"Name": "ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134",
"Source": "/var/lib/docker/volumes/ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
Docker Volume List

$ docker volume ls

DRIVER VOLUME NAME
local 1d682128cd7dcf4425c327a3374a4a0b298f3bca3a0c13366245a0f03ecc1a4b
local ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134

Mysql Container Volume 삭제

$ docker volume rm ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134

Error response from daemon: unable to remove volume: remove ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134: volume is in use - [5659f4b24372dc10852712f233eadb7f8d02c7cf2e92fd22a378366a8877bc48]

  • Mysql Container 존재 할 경우 Volume 삭제 되지 않음

mysql Container 삭제

$ docker stop mysql && docker rm mysql

Docker Volume List

$ docker volume ls

DRIVER VOLUME NAME
local 1d682128cd7dcf4425c327a3374a4a0b298f3bca3a0c13366245a0f03ecc1a4b
local 91f69c085881c2093ff25cebb389295758813c94332ded256396b0182c004133
local ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134
local e8ae5983fafb5b1965d8da6ff350a876344cffed8cc8091e917ee48c0e557a59

  • Mysql Container 삭제 해도 Volume 삭제 되지 않음
    즉 새로은 Mysql Container 생성후 Volume Mount하게되면 데이터 복원 된다.

Mysql Container Volume 삭제

$ docker volume rm ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134

ce7a46c577197d33d12bbee41cffda7322f9faff551a37675af240c296036134

  • Mysql Container 없을 경우 Volume 삭제 된다

Docker Volume 전체 제거 ( 하지 말아야 할 작업 )

$ docker volume prune

WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y

생성된 볼륨을 다른 Container 연결

mysql container 생성

$ docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD="1111" mysql

mysql container volume 정보 확인

$ docker inspect -f '{{.Mounts}}' mysql

[{volume f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c /var/lib/docker/volumes/f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c/_data /var/lib/mysql local true }]

mysql 데이터 생성

$ docker exec -it mysql /bin/bash

root@7f12f68de19f:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database dockertest ;
Query OK, 1 row affected (0.00 sec)

mysql> use dockertest
Database changed
mysql> create table tb_1 (i int ,j int);
Query OK, 0 rows affected (0.21 sec)

mysql> insert into tb_1 (i,j) values (1,1),(1,2),(1,3);
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from tb_1;
+------+------+
| i | j |
+------+------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
+------+------+
3 rows in set (0.00 sec)

mysql> exit
Bye
root@7f12f68de19f:/# exit
exit

mysql container 중단

$ docker stop mysql

new mysql (mysql2) container 생성 및 기존 volume 연결

$ docker run -d -it --name mysql2 -p 5306:3306 -v /var/lib/docker/volumes/f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c/_data/:/var/lib/mysql mysql

54bf3e449943f230a6928b1a0a698d44ca50429d90ea7d2cf0777260ae5db74b

mysql2 container DB 데이터 확인

$ docker exec -it mysql2 /bin/bash

root@54bf3e449943:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dockertest |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> use dockertest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tb_1;
+------+------+
| i | j |
+------+------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
+------+------+
3 rows in set (0.00 sec)

mysql> exit
Bye
root@54bf3e449943:/# exit
exit

Mysql ,Mysql2 Container volume 정보 (volume경로 동일하다)

$ docker inspect -f '{{.Mounts}}' mysql

[{volume f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c /var/lib/docker/volumes/f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c/_data /var/lib/mysql local true }]

$ docker inspect -f '{{.Mounts}}' mysql2

[{bind /var/lib/docker/volumes/f64faeae7dd1dc5c93d857c27655426d87bdebcd0698081181058f9f03f2907c/_data /var/lib/mysql true }]

728x90