欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

docker之创建MariaDB镜像的方法

程序员文章站 2022-06-27 14:48:07
一、基于commit命令方式创建 docker的安装 [root@test01 ~]# yum install docker [root@test01 ~...

一、基于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

[root@e8126d0481d2 /]# mysql_install_db --user=mysql

后台启动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  -

创建mariadb登录密码,并可以指定ip登录

[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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。