一、Dockerfile
- # Dockerizing Mariadb: Dockerfile for building Mariadb images
- #
- FROM hub.c.163.com/public/centos:7.2.1511
- MAINTAINER yangbk <www.361way.com>
- ENV DATA_DIR /var/lib/mysql
- ENV http_proxy 10.212.186.250:3128
- # Install Mariadb
- RUN yum install –y mariadb mariadb–server && \
- yum clean all
- ADD mysqld_charset.cnf /etc/my.cnf.d/
- COPY scripts /scripts
- RUN chmod +x /scripts/start
- EXPOSE 3306
- VOLUME [“/var/lib/mysql”]
- ENTRYPOINT [“/scripts/start”]
该代码使用文件已上传到:https://github.com/361way/docker/tree/master/mysql ,这里有注意下VOLUME命令,该命令的作用是映射容器的/var/lib/mysql 目录到本地的某个路径(即使该容器删除,相关数据文件依然存在),这个参数后面还会提到,该参数也是本篇着重要介绍的东西。
二、编译使用
使用如下命令编译该镜像文件
- docker build –t mysql:5.5 .
编译完成后,可以直接按如下命令使用:
- docker exec –it dockerid /bin/bash 或
- docker run –d –p 3306:3306 –v host_dir:container_dir —name mydb mysql:5.5
命令如下:
- [root@localhost ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- mysql 5.5 e94e56744fd2 42 hours ago 488 MB
- httpd 2.4 982b038c1c62 7 days ago 409.5 MB
- [root@localhost ~]# docker run -d -p 3306:3306 –name mydb mysql:5.5
- d81e47d2b9993c7549319cc15ab44703b7f4534e87f38f19e1c17f7eb8a6396a
- [root@localhost ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- d81e47d2b999 mysql:5.5 “/scripts/start” 8 seconds ago Up 6 seconds 22/tcp, 0.0.0.0:3306->3306/tcp mydb
- 3e88f0ea84cf 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查看的时候,可以发现有如下内容:
接下来我们在此容器里创建一个测试库:
- [root@localhost ~]# docker exec -it d81e47d2b999 /bin/bash
- [root@d81e47d2b999 /]# mysql -uroot -p
- Enter password:
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 1
- Server version: 5.5.52–MariaDB MariaDB Server
- Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
- Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
- MariaDB [(none)]> create database testdb;
- Query OK, 1 row affected (0.00 sec)
- MariaDB [(none)]> show databases;
- +——————–+
- | Database |
- +——————–+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- | testdb |
- +——————–+
- 5 rows in set (0.00 sec)
- MariaDB [(none)]> Bye
- [root@d81e47d2b999 /]# exit
- [root@localhost ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- d81e47d2b999 mysql:5.5 “/scripts/start” 6 minutes ago Up 6 minutes 22/tcp, 0.0.0.0:3306->3306/tcp mydb
- 3e88f0ea84cf 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,也可以使用如下方法确认其所在的位置:
- [root@localhost ~]# docker volume ls
- DRIVER VOLUME NAME
- local 348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d
- local 5ef16b9692e69d65734bb4942f936954d1a75ba081b8b771b2889221f62ca259
- local 979b4df31dcabb6ab93955643076c83505c4c09342a57269f017fd5581a93941
- [root@localhost ~]#
- [root@localhost ~]#
- [root@localhost ~]# docker volume inspect 348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d
- [
- {
- “Name”: “348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d”,
- “Driver”: “local”,
- “Mountpoint”: “/var/lib/docker/volumes/348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d/_data”,
- “Labels”: null,
- “Scope”: “local”
- }
- ]
上面我们已经将刚刚的容器删除了,接下来我们使用-v参数指定源和目标路径,启动一个新的容器,会发现数据库依然存在:
- [root@localhost ~]# docker run -d -p 3306:3306 -v /var/lib/docker/volumes/348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d/_data:/var/lib/mysql –name newdb mysql:5.5
- dcebc507f7a6a459ef24dc8c3718543772f22f2f3ec33053c3870b07d3446c55
- [root@localhost ~]# docker exec -it newdb /bin/bash
- [root@dcebc507f7a6 /]# mysql -uroot -p
- Enter password:
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 1
- Server version: 5.5.52–MariaDB MariaDB Server
- Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
- Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
- MariaDB [(none)]> show databases;
- +——————–+
- | Database |
- +——————–+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- | testdb |
- +——————–+
- 5 rows in set (0.00 sec)
- MariaDB [(none)]>
当然,这里我们也可以使用一个比较短一点的路径,路径不一定非得使用/var/lib/docker/volumes这样的位置。而且可以指定一个不存在的路径,当运行的时候会自动创建该目录,如下:
- docker run –d –p 3306:3306 –v /mysqldata:/var/lib/mysql —name newdb mysql:5.5
我们可以使用上面的命令启动一个容器,如果该容器删除了以后,再使用上面的命令重启一个容器就行了,数据依然存在。
四、小结
由于docker容器的数据是非持久的,如果想要持久的保存数据,可以使用VOLUME参数运行容器,其会在本地磁盘上使用一个目录同步容器里的数据,一旦容器出现意外,我们可以在秒级单位里将该应用重新启起来,而且原数据存在。