一、基于commit命令方式创建
docker的安装
[root@test01 ~]# yum install docker [root@test01 ~]# systemctl enable docker [root@test01 ~]# systemctl start docker
下载本地镜像
[root@test01 ~]# docker pull centos:7.4.1708 [root@test01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
创建交互型容器
[root@test01 ~]# docker run -it --name="MysqL_server" centos /bin/bash
4.安装mariadb服务
[root@e8126d0481d2 /]# yum -y install mariadb-server net-tools
初始化mariadb
后台启动mariadb服务
[root@e8126d0481d2 /]# MysqLd_safe & [1] 114 [root@e8126d0481d2 /]# 180210 13:45:27 MysqLd_safe Logging to '/var/log/mariadb/mariadb.log'. 180210 13:45:27 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL [root@e8126d0481d2 /]# netstat -tunpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
[root@e8126d0481d2 /]# MysqLadmin -u root password 'kingsoft' [root@e8126d0481d2 /]# MysqL -u root -p Enter password: MariaDB [(none)]> show databases; MariaDB [(none)]> use MysqL; MariaDB [MysqL]> select Host from user where user='root'; MariaDB [MysqL]> grant all privileges on *.* to 'root'@'%' identified by 'kingsoft' with grant option; MariaDB [MysqL]> update user set password=password('kingsoft') where user='root' and host='e8126d0481d2'; MariaDB [MysqL]> flush privileges; MariaDB [MysqL]> exit
容器登录验证
[root@e8126d0481d2 /]# MysqL -u root -h 172.17.0.2 -p Enter password: MariaDB [(none)]> exit
创建容器启动脚本
[root@e8126d0481d2 ~]# cat run.sh #!/bin/sh MysqLd_safe
创建镜像
[root@test01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e8126d0481d2 centos "/bin/bash" 11 minutes ago Exited (0) 8 seconds ago MysqL_server [root@test01 ~]# docker commit MysqL_server mariadb:1.0
创建容器
[root@test01 ~]# docker run -d -p 13306:3306 mariadb:1.0 /root/run.sh [root@test01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eed3e88a1261 mariadb:1.0 "MysqLd_safe" 4 seconds ago Up 3 seconds 0.0.0.0:13306->3306/tcp romantic_hamilton
主机登录验证
[root@test01 ~]# yum -y install mariadb [root@test01 ~]# MysqL -u root --port=13306 -p MariaDB [(none)]>
二、基于Dockerfile方式创建
设置创建目录和文件
[root@test01 ~]# mkdir mariadb_dockerfile && cd mariadb_dockerfile [root@test01 mariadb_dockerfile]# touch db_init.sh [root@test01 mariadb_dockerfile]# touch run.sh
编辑Dockerfile等文件
Dockerfile
[root@test01 mariadb_dockerfile]# cat Dockerfile #使用的基础镜像 FROM centos:7.4.1708 #添加作者信息 MAINTAINER liuxin 842887233@qq.com #安装mariadb数据库 RUN yum -y install mariadb-server #设置环境变量,便于管理 ENV MARIADB_USER root ENV MARIADB_PASS kingsoft #让容器支持中文 ENV LC_ALL en_US.UTF-8 #初始化数据库 ADD db_init.sh /root/db_init.sh RUN chmod 775 /root/db_init.sh RUN /root/db_init.sh #导出端口 EXPOSE 3306 #添加启动文件 ADD run.sh /root/run.sh RUN chmod 775 /root/run.sh #设置默认启动命令 CMD ["/root/run.sh"]
db_init.sh
[root@test01 mariadb_dockerfile]# cat db_init.sh #!/bin/bash MysqL_install_db --user=MysqL sleep 3 MysqLd_safe & sleep 3 #MysqLadmin -u "$MARIADB_USER" password "$MARIADB_PASS" MysqL -e "use MysqL; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;" h=$(hostname) MysqL -e "use MysqL; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';" MysqL -e "flush privileges;"
run.sh
[root@test01 mariadb_dockerfile]# cat run.sh #!/bin/bash MysqLd_safe
创建镜像
[root@test01 mariadb_dockerfile]# docker build -t liuxin/centos-mariadb:v1 ./
创建容器
[root@test01 mariadb_dockerfile]# docker run -d -p 13306:3306 liuxin/centos-mariadb:v1 /root/run.sh [root@test01 mariadb_dockerfile]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7743527ac603 liuxin/centos-mariadb:v1 "/root/run.sh" 5 seconds ago Up 3 seconds 0.0.0.0:13306->3306/tcp nostalgic_mirzakhani
登录验证
[root@test01 mariadb_dockerfile]# MysqL -uroot -h 127.0.0.1 --port=13306 -p Enter password: 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)]> exit
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。