Wednesday, 17 July 2019

Docker Command Part-14 By Raj Gupta

How to keep data permanent in docker container by using volume



[root@ip-172-31-93-105 ~]# docker volume ls
DRIVER              VOLUME NAME

Create mysql container without password

[root@ip-172-31-93-105 ~]# docker container run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
[root@ip-172-31-93-105 ~]# docker volume ls
DRIVER              VOLUME NAME
local               bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f



Volume are mounted to 

  "Volumes": {
                "/var/lib/mysql": {}
            },


above value we can get from command [root@ip-172-31-93-105 ~]# docker image inspect mysql


[root@ip-172-31-93-105 ~]# docker volume inspect bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f
[
    {
        "CreatedAt": "2019-07-17T08:36:28Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f/_data",
        "Name": "bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f",
        "Options": null,
        "Scope": "local"
    }
]

Volume are there in folder 

[root@ip-172-31-93-105 ~]# cd /var/lib/docker/volumes/bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f/_data
[root@ip-172-31-93-105 _data]# ls
auto.cnf       binlog.index  client-cert.pem  ibdata1      ibtmp1        mysql.ibd           public_key.pem   sys
binlog.000001  ca-key.pem    client-key.pem   ib_logfile0  #innodb_temp  performance_schema  server-cert.pem  undo_001
binlog.000002  ca.pem        ib_buffer_pool   ib_logfile1  mysql         private_key.pem     server-key.pem   undo_002
[root@ip-172-31-93-105 _data]#

[root@ip-172-31-93-105 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
52c3a40f6ef0        mysql               "docker-entrypoint.s…"   28 minutes ago      Up 28 minutes       3306/tcp, 33060/tcp   mysql1
[root@ip-172-31-93-105 ~]# docker container exec -it 52c3a40f6ef0 bash
root@52c3a40f6ef0:/#


Logging into mysql and create database

root@52c3a40f6ef0:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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 raj;
Query OK, 1 row affected (0.01 sec)

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> create database example;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| example            |
| information_schema |
| mysql              |
| performance_schema |
| raj                |
| sys                |
| test               |
+--------------------+
7 rows in set (0.01 sec)

mysql> exit
Bye
root@52c3a40f6ef0:/#
root@52c3a40f6ef0:/# exit
exit

----------------------------------------------------------------------------

Now we are going to create new container after deleting the container created in above step

[root@ip-172-31-93-105 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
52c3a40f6ef0        mysql               "docker-entrypoint.s…"   43 minutes ago      Up 43 minutes       3306/tcp, 33060/tcp   mysql1
[root@ip-172-31-93-105 ~]# docker container rm -f 52c3a40f6ef0
52c3a40f6ef0
[root@ip-172-31-93-105 ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ip-172-31-93-105 ~]# docker container run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
4b50bc1137e5b849b00cbdcbb8314c9c11f5116453e016f6111c75dde1e1b28e
[root@ip-172-31-93-105 ~]# docker container exec -it 4b5 bash
root@4b50bc1137e5:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit
Bye
root@4b50bc1137e5:/# exit
exit
[root@ip-172-31-93-105 ~]#


This means if you delete the container and if try to create a new container from same image also then it will not contains the same data because it will create with new volume
--------------------------------------------------------------------------

If you want to use same volume of your old container in place of new the use the below step...It will preserve your data

[root@ip-172-31-93-105 ~]# docker volume ls
DRIVER              VOLUME NAME
local               bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f   ----old
local               e700479341274ebe6837f6f4c20f564a6c93d91cf16490c5d825b2081c1ee07f -----new
[root@ip-172-31-93-105 ~]# docker container run -itd -v bcf7678ec0c851930453fad10b43763caebc4c2e71d6c758863484f8060ad96f:/var/lib/mysql mysql
0384601accbcc5e836a859ee075ff26c05bf3eb5a4f74bb36c509c63c6ed2aa9
[root@ip-172-31-93-105 ~]# docker container exec -it 0384 bash
root@0384601accbc:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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           |
+--------------------+
| example            |
| information_schema |
| mysql              |
| performance_schema |
| raj                |
| sys                |
| test               |
+--------------------+
7 rows in set (0.01 sec)

mysql>

No comments:

Post a Comment