一、Dockerfile

  1. # Dockerizing Mariadb: Dockerfile for building Mariadb images
  2. #
  3. FROM hub.c.163.com/public/centos:7.2.1511
  4. MAINTAINER yangbk <www.361way.com>
  5. ENV DATA_DIR /var/lib/mysql
  6. ENV http_proxy 10.212.186.250:3128
  7. # Install Mariadb
  8. RUN yum install y mariadb mariadbserver && \
  9. yum clean all
  10. ADD mysqld_charset.cnf /etc/my.cnf.d/
  11. COPY scripts /scripts
  12. RUN chmod +x /scripts/start
  13. EXPOSE 3306
  14. VOLUME [“/var/lib/mysql”]
  15. ENTRYPOINT [“/scripts/start”]

该代码使用文件已上传到:https://github.com/361way/docker/tree/master/mysql ,这里有注意下VOLUME命令,该命令的作用是映射容器的/var/lib/mysql 目录到本地的某个路径(即使该容器删除,相关数据文件依然存在),这个参数后面还会提到,该参数也是本篇着重要介绍的东西。

二、编译使用

使用如下命令编译该镜像文件

  1. docker build t mysql:5.5 .

编译完成后,可以直接按如下命令使用:

  1. docker exec it dockerid /bin/bash
  2. docker run d p 3306:3306 v host_dir:container_dir name mydb mysql:5.5

命令如下:

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mysql 5.5 e94e56744fd2 42 hours ago 488 MB
  4. httpd 2.4 982b038c1c62 7 days ago 409.5 MB
  5. [root@localhost ~]# docker run -d -p 3306:3306 –name mydb mysql:5.5
  6. d81e47d2b9993c7549319cc15ab44703b7f4534e87f38f19e1c17f7eb8a6396a
  7. [root@localhost ~]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. d81e47d2b999 mysql:5.5 “/scripts/start” 8 seconds ago Up 6 seconds 22/tcp, 0.0.0.0:3306->3306/tcp mydb
  10. 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查看的时候,可以发现有如下内容:

接下来我们在此容器里创建一个测试库:

  1. [root@localhost ~]# docker exec -it d81e47d2b999 /bin/bash
  2. [root@d81e47d2b999 /]# mysql -uroot -p
  3. Enter password:
  4. Welcome to the MariaDB monitor. Commands end with ; or \g.
  5. Your MariaDB connection id is 1
  6. Server version: 5.5.52MariaDB MariaDB Server
  7. Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  8. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
  9. MariaDB [(none)]> create database testdb;
  10. Query OK, 1 row affected (0.00 sec)
  11. MariaDB [(none)]> show databases;
  12. +——————–+
  13. | Database |
  14. +——————–+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | test |
  19. | testdb |
  20. +——————–+
  21. 5 rows in set (0.00 sec)
  22. MariaDB [(none)]> Bye
  23. [root@d81e47d2b999 /]# exit
  24. [root@localhost ~]# docker ps
  25. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  26. d81e47d2b999 mysql:5.5 “/scripts/start” 6 minutes ago Up 6 minutes 22/tcp, 0.0.0.0:3306->3306/tcp mydb
  27. 3e88f0ea84cf httpd:2.4 “/usr/bin/supervisord” 7 days ago Up 7 days 22/tcp, 0.0.0.0:8082->80/tcp web2

删除该容器后,发现在主机上原文件还在:

docker-mysql-volumes

三、docker volume

上面我们提到的volume,也可以使用如下方法确认其所在的位置:

  1. [root@localhost ~]# docker volume ls
  2. DRIVER VOLUME NAME
  3. local 348d2be96514243eb407655d8e0c403b67346dce70be5d76c071df188487d42d
  4. local 5ef16b9692e69d65734bb4942f936954d1a75ba081b8b771b2889221f62ca259
  5. local 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
  2. dcebc507f7a6a459ef24dc8c3718543772f22f2f3ec33053c3870b07d3446c55
  3. [root@localhost ~]# docker exec -it newdb /bin/bash
  4. [root@dcebc507f7a6 /]# mysql -uroot -p
  5. Enter password:
  6. Welcome to the MariaDB monitor. Commands end with ; or \g.
  7. Your MariaDB connection id is 1
  8. Server version: 5.5.52MariaDB MariaDB Server
  9. Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  10. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
  11. MariaDB [(none)]> show databases;
  12. +——————–+
  13. | Database |
  14. +——————–+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | test |
  19. | testdb |
  20. +——————–+
  21. 5 rows in set (0.00 sec)
  22. MariaDB [(none)]>

当然,这里我们也可以使用一个比较短一点的路径,路径不一定非得使用/var/lib/docker/volumes这样的位置。而且可以指定一个不存在的路径,当运行的时候会自动创建该目录,如下:

  1. docker run d p 3306:3306 v /mysqldata:/var/lib/mysql name newdb mysql:5.5

我们可以使用上面的命令启动一个容器,如果该容器删除了以后,再使用上面的命令重启一个容器就行了,数据依然存在。

四、小结

由于docker容器的数据是非持久的,如果想要持久的保存数据,可以使用VOLUME参数运行容器,其会在本地磁盘上使用一个目录同步容器里的数据,一旦容器出现意外,我们可以在秒级单位里将该应用重新启起来,而且原数据存在。

Leave a Reply

Your email address will not be published. Required fields are marked *