docker之创建MariaDB镜像的方法
一、基于commit命令方式创建
docker的安装
[root@test01~]#yuminstalldocker [root@test01~]#systemctlenabledocker [root@test01~]#systemctlstartdocker
下载本地镜像
[root@test01~]#dockerpullcentos:7.4.1708 [root@test01~]#dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE docker.io/centos7.4.17083afd47092a0e3monthsago196.6MB
创建交互型容器
[root@test01~]#dockerrun-it--name="mysql_server"centos/bin/bash
4.安装mariadb服务
[root@e8126d0481d2/]#yum-yinstallmariadb-servernet-tools
初始化mariadb
[root@e8126d0481d2/]#mysql_install_db--user=mysql
后台启动mariadb服务
[root@e8126d0481d2/]#mysqld_safe& [1]114 [root@e8126d0481d2/]# 18021013:45:27mysqld_safeLoggingto'/var/log/mariadb/mariadb.log'. 18021013:45:27mysqld_safeStartingmysqlddaemonwithdatabasesfrom/var/lib/mysql [root@e8126d0481d2/]#netstat-tunpl ActiveInternetconnections(onlyservers) ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programname tcp000.0.0.0:33060.0.0.0:*LISTEN-
创建mariadb登录密码,并可以指定ip登录
[root@e8126d0481d2/]#mysqladmin-urootpassword'kingsoft' [root@e8126d0481d2/]#mysql-uroot-p Enterpassword: MariaDB[(none)]>showdatabases; MariaDB[(none)]>usemysql; MariaDB[mysql]>selectHostfromuserwhereuser='root'; MariaDB[mysql]>grantallprivilegeson*.*to'root'@'%'identifiedby'kingsoft'withgrantoption; MariaDB[mysql]>updateusersetpassword=password('kingsoft')whereuser='root'andhost='e8126d0481d2'; MariaDB[mysql]>flushprivileges; MariaDB[mysql]>exit
容器登录验证
[root@e8126d0481d2/]#mysql-uroot-h172.17.0.2-p Enterpassword: MariaDB[(none)]>exit
创建容器启动脚本
[root@e8126d0481d2~]#catrun.sh #!/bin/sh mysqld_safe
创建镜像
[root@test01~]#dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES e8126d0481d2centos"/bin/bash"11minutesagoExited(0)8secondsagomysql_server [root@test01~]#dockercommitmysql_servermariadb:1.0
创建容器
[root@test01~]#dockerrun-d-p13306:3306mariadb:1.0/root/run.sh [root@test01~]#dockerps CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES eed3e88a1261mariadb:1.0"mysqld_safe"4secondsagoUp3seconds0.0.0.0:13306->3306/tcpromantic_hamilton
主机登录验证
[root@test01~]#yum-yinstallmariadb [root@test01~]#mysql-uroot--port=13306-p MariaDB[(none)]>
二、基于Dockerfile方式创建
设置创建目录和文件
[root@test01~]#mkdirmariadb_dockerfile&&cdmariadb_dockerfile [root@test01mariadb_dockerfile]#touchdb_init.sh [root@test01mariadb_dockerfile]#touchrun.sh
编辑Dockerfile等文件
Dockerfile
[root@test01mariadb_dockerfile]#catDockerfile #使用的基础镜像 FROMcentos:7.4.1708 #添加作者信息 MAINTAINERliuxin842887233@qq.com #安装mariadb数据库 RUNyum-yinstallmariadb-server #设置环境变量,便于管理 ENVMARIADB_USERroot ENVMARIADB_PASSkingsoft #让容器支持中文 ENVLC_ALLen_US.UTF-8 #初始化数据库 ADDdb_init.sh/root/db_init.sh RUNchmod775/root/db_init.sh RUN/root/db_init.sh #导出端口 EXPOSE3306 #添加启动文件 ADDrun.sh/root/run.sh RUNchmod775/root/run.sh #设置默认启动命令 CMD["/root/run.sh"]
db_init.sh
[root@test01mariadb_dockerfile]#catdb_init.sh #!/bin/bash mysql_install_db--user=mysql sleep3 mysqld_safe& sleep3 #mysqladmin-u"$MARIADB_USER"password"$MARIADB_PASS" mysql-e"usemysql;grantallprivilegeson*.*to'$MARIADB_USER'@'%'identifiedby'$MARIADB_PASS'withgrantoption;" h=$(hostname) mysql-e"usemysql;updateusersetpassword=password('$MARIADB_PASS')whereuser='$MARIADB_USER'andhost='$h';" mysql-e"flushprivileges;"
run.sh
[root@test01mariadb_dockerfile]#catrun.sh #!/bin/bash mysqld_safe
创建镜像
[root@test01mariadb_dockerfile]#dockerbuild-tliuxin/centos-mariadb:v1./
创建容器
[root@test01mariadb_dockerfile]#dockerrun-d-p13306:3306liuxin/centos-mariadb:v1/root/run.sh [root@test01mariadb_dockerfile]#dockerps CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 7743527ac603liuxin/centos-mariadb:v1"/root/run.sh"5secondsagoUp3seconds0.0.0.0:13306->3306/tcpnostalgic_mirzakhani
登录验证
[root@test01mariadb_dockerfile]#mysql-uroot-h127.0.0.1--port=13306-p Enterpassword: WelcometotheMariaDBmonitor.Commandsendwith;or\g. YourMariaDBconnectionidis1 Serverversion:5.5.56-MariaDBMariaDBServer Copyright(c)2000,2017,Oracle,MariaDBCorporationAbandothers. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. MariaDB[(none)]>exit
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。