Docker小结(三)配置mysql数据库
一、Dockerfile
1FROM centos:7.2.1511
2MAINTAINER yangbk
3ENV DATA_DIR /var/lib/mysql
4ENV http_proxy 10.212.186.250:3128
5# Install Mariadb
6RUN yum install -y mariadb mariadb-server && \
7 yum clean all
8ADD mysqld_charset.cnf /etc/my.cnf.d/
9COPY scripts /scripts
10RUN chmod +x /scripts/start
11EXPOSE 3306
12VOLUME ["/var/lib/mysql"]
13ENTRYPOINT ["/scripts/start"]
该代码使用文件已上传到:https://github.com/361way/docker/tree/master/mysql ,这里有注意下VOLUME命令,该命令的作用是映射容器的/var/lib/mysql 目录到本地的某个路径(即使该容器删除,相关数据文件依然存在),这个参数后面还会提到,该参数也是本篇着重要介绍的东西。
二、编译使用
使用如下命令编译该镜像文件
1docker build -t mysql:5.5 .
编译完成后,可以直接按如下命令使用:
1docker exec -it dockerid /bin/bash 或
2docker run -d -p 3306:3306 -v host_dir:container_dir --name mydb mysql:5.5
命令如下:
1[root@localhost ~]# docker images
2REPOSITORY TAG IMAGE ID CREATED SIZE
3mysql 5.5 e94e56744fd2 42 hours ago 488 MB
4httpd 2.4 982b038c1c62 7 days ago 409.5 MB
5[root@localhost ~]# docker run -d -p 3306:3306 --name mydb mysql:5.5
6d81e47d2b9993c7549319cc15ab44703b7f4534e87f38f19e1c17f7eb8a6396a
7[root@localhost ~]# docker ps
8CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d81e47d2b999 mysql:5.5 "/scripts/start" 8 seconds ago Up 6 seconds 22/tcp, 0.0.0.0:3306->3306/tcp mydb
103e88f0ea84cf httpd:2.4 "/usr/bin/supervisord" 7 days ago Up 7 days 22/tcp, 0.0.0.0:8082->80/tcp web2
使用docker inspect d81e47d2b999查看的时候,可以发现有如下内容:
接下来我们在此容器里创建一个测试库:
1[root@localhost ~]# docker exec -it d81e47d2b999 /bin/bash
2[root@d81e47d2b999 /]# mysql -uroot -p
3Enter password:
4Welcome to the MariaDB monitor. Commands end with ; or \g.
5Your MariaDB connection id is 1
6Server version: 5.5.52-MariaDB MariaDB Server
7Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
8Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
9MariaDB [(none)]> create database testdb;
10Query OK, 1 row affected (0.00 sec)
11MariaDB [(none)]> show databases;
12+--------------------+
13| Database |
14+--------------------+
15| information_schema |
16| mysql |
17| performance_schema |
18| test |
19| testdb |
20+--------------------+
215 rows in set (0.00 sec)
22MariaDB [(none)]> Bye
23[root@d81e47d2b999 /]# exit
24[root@localhost ~]# docker ps
25CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26d81e47d2b999 mysql:5.5 "/scripts/start" 6 minutes ago Up 6 minutes 22/tcp, 0.0.0.0:3306->3306/tcp mydb
273e88f0ea84cf httpd:2.4 "/usr/bin/supervisord" 7 days ago Up 7 days 22/tcp, 0.0.0.0:8082->80/tcp web2
删除该容器后,发现在主机上原文件还在:
三、docker volume
上面我们提到的volume,也可以使用如下方法确认其所在的位置:
1[root@localhost ~]# docker volume ls
2DRIVER VOLUME NAME
3local 348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d
4local 5ef16b9692e69d65734bb4942f936954d1a75ba081b8b771b2889221f62ca259
5local 979b4df31dcabb6ab93955643076c83505c4c09342a57269f017fd5581a93941
6[root@localhost ~]#
7[root@localhost ~]#
8[root@localhost ~]# docker volume inspect 348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d
9[
10 {
11 "Name": "348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d",
12 "Driver": "local",
13 "Mountpoint": "/var/lib/docker/volumes/348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d/_data",
14 "Labels": null,
15 "Scope": "local"
16 }
17]
上面我们已经将刚刚的容器删除了,接下来我们使用-v参数指定源和目标路径,启动一个新的容器,会发现数据库依然存在:
1[root@localhost ~]# docker run -d -p 3306:3306 -v /var/lib/docker/volumes/348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d/_data:/var/lib/mysql --name newdb mysql:5.5
2dcebc507f7a6a459ef24dc8c3718543772f22f2f3ec33053c3870b07d3446c55
3[root@localhost ~]# docker exec -it newdb /bin/bash
4[root@dcebc507f7a6 /]# mysql -uroot -p
5Enter password:
6Welcome to the MariaDB monitor. Commands end with ; or \g.
7Your MariaDB connection id is 1
8Server version: 5.5.52-MariaDB MariaDB Server
9Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
10Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
11MariaDB [(none)]> show databases;
12+--------------------+
13| Database |
14+--------------------+
15| information_schema |
16| mysql |
17| performance_schema |
18| test |
19| testdb |
20+--------------------+
215 rows in set (0.00 sec)
22MariaDB [(none)]>
当然,这里我们也可以使用一个比较短一点的路径,路径不一定非得使用/var/lib/docker/volumes这样的位置。而且可以指定一个不存在的路径,当运行的时候会自动创建该目录,如下:
1docker run -d -p 3306:3306 -v /mysqldata:/var/lib/mysql --name newdb mysql:5.5
我们可以使用上面的命令启动一个容器,如果该容器删除了以后,再使用上面的命令重启一个容器就行了,数据依然存在。
四、小结
由于docker容器的数据是非持久的,如果想要持久的保存数据,可以使用VOLUME参数运行容器,其会在本地磁盘上使用一个目录同步容器里的数据,一旦容器出现意外,我们可以在秒级单位里将该应用重新启起来,而且原数据存在。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/docker-volume/5462.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.