构建DB Docker镜像
mysql的代码文件
[root@Optimus /]# cd docker-training/
[root@Optimus docker-training]# ls
centos7 mysql php-fpm README.md wordpress
[root@Optimus docker-training]# cd mysql/
[root@Optimus mysql]# ls
Dockerfile mysqld_charset.cnf scripts
[root@Optimus mysql]#
dockerfile用于构建DB docker镜像
[root@Optimus mysql]# vim Dockerfile
#
# MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com>
# DOCKER-VERSION 1.6.2
#
# Dockerizing Mariadb: Dockerfile for building Mariadb images
#
FROM csphere/centos:7.1
MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com>
ENV DATA_DIR /var/lib/mysql
# 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"]
[FROM csphere/centos:7.1
] 构建完成后,具有centos7.1和Mariadb的功能。可以用php-fpm的镜像进行构建,但升级更新复杂。
[MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com>
]
[ENV DATA_DIR /var/lib/mysql
]设置一个DATA_DIR的环境变量,对应的目录是/var/lib/mysql
[RUN yum install -y mariadb mariadb-server && \ yum clean all
] 安装了mariadb和mariadb-server
[ADD mysqld_charset.cnf /etc/my.cnf.d/
] 添加mysql的字符文件到/etc/my.cnf.d/目录下
[COPY scripts /scripts
] 从本地复制文件scripts到container中的/scripts
[RUN chmod +x /scripts/start
] 给start文件加一个执行权限
[EXPOSE 3306
] 默认3306端口
[VOLUME ["/var/lib/mysql"]
] 通过docker镜像启动了docker容器,把docker容器删除以后,里面所有的数据都会随着docker container被删除,但是db container不需要一并把数据库也删除,所有需要通过VOLUME把数据库数据保留下来,container删除后数据仍然保留。VOLUME通过宿主机的文件目录去映射container的文件目录/var/lib/mysql。
[ENTRYPOINT ["/scripts/start"]
] 启动start脚本文件
start脚本
[root@Optimus mysql]# cd scripts/
[root@Optimus scripts]# ls
firstrun firstrun_maria start
启动container时主要执行start脚本,start是一个/bin/bash脚本
#!/bin/bash
set -e
#
# When Startup Container script
#
if [[ -e /scripts/firstrun ]]; then
# config mariadb
/scripts/firstrun_maria
rm /scripts/firstrun
else
# Cleanup previous mariadb sockets
if [[ -e ${DATA_DIR}/mysql.sock ]]; then
rm -f ${DATA_DIR}/mysql.sock
fi
fi
exec /usr/bin/mysqld_safe
[#!/bin/bash
] /bin/bash脚本
[set -e
]加了该命令以后,如果第一条命令执行错误则直接退出。
[if [[ -e /scripts/firstrun ]]; then
] 判断一下firstrun这个文件在不在。在构建db container的时候,需要在启动的时候做一些数据库初始化的工作,为数据库创建一个数据库用户,以及给数据库用户创建密码。因此在启动时,需要一个逻辑判断。
[/scripts/firstrun_maria
] 如果firstrun存在,就执行firstrun_maria脚本
[rm /scripts/firstrun
] 执行完firstrun_maria以后,把firstrun删掉。因为在第二次启动的时候不希望再执行类似的操作,直接启动mariadb即可。
[/scripts/firstrun_maria
] 运行firstrun_maria函数
[rm /scripts/firstrun
] 执行完后,删除firstrun
[exec /usr/bin/mysqld_safe
] 启动mysql
以上是制作db镜像的配置文件。
build db镜像
docker build -t csphere/mysql:5.5
必须先build centos7镜像,才能build db镜像。
查看当前本地docker镜像
docker images
[root@Optimus mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
csphere/mysql 5.5 e147ac2a588e 15 minutes ago 752.7 MB
csphere/php-fpm 5.4 1b08c94ce801 2 days ago 709.7 MB
csphere/centos 7.1 fd1f7619e63e 3 days ago 613 MB
centos centos7.1.1503 879c6d07c60e 6 weeks ago 212.1 MB
构建好docker镜像,下面启动mariadb的docker容器
docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
查看本地docker容器
[root@Optimus mysql]# docker ps -a
出错:显示3306端口被占用
[root@Optimus mysql]# docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
e63911262c9490d65be1bc9c1c575f2eebaacc0c3afa3874e0f4cd687b5fc717
Error response from daemon: Cannot start container e63911262c9490d65be1bc9c1c575f2eebaacc0c3afa3874e0f4cd687b5fc717: Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use
[root@Optimus mysql]# fuser -v -n tcp 3306
USER PID ACCESS COMMAND
3306/tcp: mysql 2928 F.... mysqld
[root@Optimus mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e63911262c94 csphere/mysql:5.5 "/scripts/start" 32 minutes ago dbserver
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 2 days ago Up 2 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 3 days ago Up 3 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 4 days ago Exited (127) 4 days ago naughty_engelbart
[root@Optimus mysql]#
更改为3307端口号,重新启动container,显示已经存在dbserver这个container了
[root@Optimus mysql]# docker run -d -p 3307:3306 --name debserver csphere/mysql:5.5
Error response from daemon: Conflict. The name "debserver" is already in use by container e63911262c94. You have to delete (or rename) that container to be able to reuse that name.
删除dbserver容器
[root@Optimus mysql]# docker rm debserver
debserver
重新run container,并且端口映射为3307
[root@Optimus mysql]# docker run -d -p 3307:3306 --name dbserver csphere/mysql:5.5
076cb9396ca0aaa9ce0418dc8c5950aa227b0f1e1def92021fcaab39502c255e
查看容器
[root@Optimus mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
076cb9396ca0 csphere/mysql:5.5 "/scripts/start" 13 minutes ago Up 13 minutes 22/tcp, 0.0.0.0:3307->3306/tcp dbserver
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 2 days ago Up 2 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 3 days ago Up 3 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 4 days ago Exited (127) 4 days ago
进入交互模式,登录到容器中/bin/bash目录下查看
[root@Optimus mysql]# docker exec -it dbserver /bin/bash
[root@076cb9396ca0 /]#
登录mysql,进入Mariadb中,可以进行任何数据库的操作
[root@076cb9396ca0 /]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.17 sec)
MariaDB [(none)]>
以上,一个具有Mariadb功能的docker容器创建成果。
退出mysql ,退出容器
MariaDB [(none)]> exit
Bye
[root@076cb9396ca0 /]# exit
exit
上面创建db容器的时候,没有加-v参数。下面,使用docker run -v命令重新构建db容器。
docker run -v 挂载VOLUME参数
删除容器
docker rm dbserver
不加任何参数时,不能删除正在运行的container
docker rm -f dbserver
强制删除
[root@Optimus mysql]# docker rm -f dbserver
dbserver
挂载-v和不挂载-v参数的区别
运行一个挂载-v参数的container
-v可以使用在数据库、Nginx以及需要保存文件至宿主机下的服务上。
docker run -d -p 3307:3006 -v host_dir:container_dir
docker run -d -p 3307:3006 -v 宿主机的目录/var/lib/docker/vfs/dir/mydata:容器的目录/var/lib/mysql
mydata目录和mysql的数据是一模一样的,并且是同步的
这次不使用--name
我使用了3307做端口映射,因为本地3306端口被占用。如果没有被占用,可以用3306:3306做端口映射
指定镜像 csphere/mysql:5.5
[root@Optimus /]# docker run -d -p 3307:3306 -v /var/lib/docker/vfs/dir/mysqta:/var/lib/mysql csphere/mysql:5.5
9ad32fb968fe93daa349091b2725d9dc0b4ce414a9a13f4cf7751f0b15857d8f
查看容器
[root@Optimus /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ad32fb968fe csphere/mysql:5.5 "/scripts/start" 29 minutes ago Up 29 minutes 22/tcp, 0.0.0.0:3307->3306/tcp romantic_brattain
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 5 days ago Up 5 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 6 days ago Up 6 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 7 days ago Exited (127) 7 days ago naughty_engelbart
[root@Optimus /]#
没有使用--name,自动匹配字符串 romantic_brattain 作为容器名称
测试:挂载了-v以后,创建一个数据库,然后删除容器,数据是否还存在?
进入容器
docker exec -it containerID/containerName /bin/bash
进入容器中,容器必须是UP状态,否则进不去。
[root@Optimus /]# docker exec -it 9ad /bin/bash
[root@9ad32fb968fe /]#
登录mysql
[root@9ad32fb968fe /]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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 |
+--------------------+
4 rows in set (0.10 sec)
创建数据库
MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.12 sec)
查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
退出
MariaDB [(none)]> exit
Bye
e[root@9ad32fb968fe /]# exit
exit
[root@Optimus /]#
查看一下刚才映射的目录下有没有mydb文件夹
[root@Optimus /]# ls /var/lib/docker/vfs/dir/mysqta/
aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 mydb mysql mysql.sock performance_schema test
存在mydb目录,说明宿主机已经和mysql的var目录下的数据同步
查看mysql镜像
[root@Optimus /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ad32fb968fe csphere/mysql:5.5 "/scripts/start" 5 hours ago Up 5 hours 22/tcp, 0.0.0.0:3307->3306/tcp romantic_brattain
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 5 days ago Up 5 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 6 days ago Up 6 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 7 days ago Exited (127) 7 days ago naughty_engelbart
[root@Optimus /]#
停止容器
[root@Optimus /]# docker stop 9ad
9ad
再次查看容器状态为停止状态
[root@Optimus /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ad32fb968fe csphere/mysql:5.5 "/scripts/start" 5 hours ago Exited (137) 37 seconds ago romantic_brattain
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 5 days ago Up 5 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 6 days ago Up 6 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 7 days ago Exited (127) 7 days ago naughty_engelbart
[root@Optimus /]#
查看UP状态下的container
[root@Optimus /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 5 days ago Up 5 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 6 days ago Up 6 days 0.0.0.0:2222->22/tcp base
[root@Optimus /]#
不用加参数,直接删除容器
[root@Optimus /]# docker rm 9ad
9ad
查看mysqta目录下的文件依然还存在
[root@Optimus /]# ls /var/lib/docker/vfs/dir/mysqta/
aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 mydb mysql mysql.sock performance_schema test
[root@Optimus /]#
创建新的container,依然还可以继续使用这个数据库。
[root@Optimus /]# docker run -d -p 3307:3306 --name newdb -v /var/lib/docker/vfs/dir/mysqta:/var/lib/mysql csphere/mysql:5.5
8e266cd27a63495291c8b48f06204426c6d42b7c37ef8b86a6a6c124a5f5bafa
[root@Optimus /]#
查看容器状态
[root@Optimus /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e266cd27a63 csphere/mysql:5.5 "/scripts/start" 31 seconds ago Up 29 seconds 22/tcp, 0.0.0.0:3307->3306/tcp newdb
e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 5 days ago Up 5 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 6 days ago Up 6 days 0.0.0.0:2222->22/tcp base
0d1f6225836b centos "/bin/bash" 7 days ago Exited (127) 7 days ago naughty_engelbart
[root@Optimus /]#
进入到容器中查看数据库
[root@Optimus /]# docker exec -it newdb /bin/bash
[root@8e266cd27a63 /]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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 |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.08 sec)
MariaDB [(none)]>
只要数据库文件目录存在,container可以随意删除重建,恢复很快。
构建DB容器完成。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。