docker容器中怎么創(chuàng)建一個(gè)MariaDB鏡像,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、大通ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的大通網(wǎng)站制作公司基于commit 命令 方式創(chuàng)建
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
創(chuàng)建交互型容器
[root@test01 ~]# docker run -it --name="mysql_server" centos /bin/bash
安裝mariadb服務(wù)
[root@e8126d0481d2 /]# yum -y install mariadb-server net-tools
初始化mariadb
[root@e8126d0481d2 /]# mysql_install_db --user=mysql
后臺(tái)啟動(dòng)mariadb服務(wù)
[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 -
創(chuàng)建mariadb登錄密碼,并可以指定ip登錄
[root@e8126d0481d2 /]# mysqladmin -u root password '123456' [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 '123456' with grant option; MariaDB [mysql]> update user set password=password('123456') where user='root' and host='e8126d0481d2'; MariaDB [mysql]> flush privileges; MariaDB [mysql]> exit
容器登錄驗(yàn)證
[root@e8126d0481d2 /]# mysql -u root -h 172.17.0.2 -p Enter password: MariaDB [(none)]> exit
創(chuàng)建容器啟動(dòng) 腳本
[root@e8126d0481d2 ~]# cat run.sh #!/bin/sh mysqld_safe
創(chuàng)建鏡像
[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
創(chuàng)建容器
[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
主機(jī)登錄驗(yàn)證
[root@test01 ~]# yum -y install mariadb [root@test01 ~]# mysql -u root --port=13306 -p MariaDB [(none)]>
基于Dockerfile方式創(chuàng)建
設(shè)置創(chuàng)建目錄和文件 [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 #使用的基礎(chǔ)鏡像 FROM centos:7.4.1708 #添加作者信息 MAINTAINER liuxin 842887233@qq.com #安裝mariadb數(shù)據(jù)庫(kù) RUN yum -y install mariadb-server #設(shè)置環(huán)境變量,便于管理 ENV MARIADB_USER root ENV MARIADB_PASS 123456 #讓容器支持中文 ENV LC_ALL en_US.UTF-8 #初始化數(shù)據(jù)庫(kù) ADD db_init.sh /root/db_init.sh RUN chmod 775 /root/db_init.sh RUN /root/db_init.sh #導(dǎo)出端口 EXPOSE 3306 #添加啟動(dòng)文件 ADD run.sh /root/run.sh RUN chmod 775 /root/run.sh #設(shè)置默認(rèn)啟動(dòng)命令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
創(chuàng)建鏡像
[root@test01 mariadb_dockerfile]# docker build -t liuxin/centos-mariadb:v1 ./
創(chuàng)建容器
[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
登錄驗(yàn)證
[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
關(guān)于docker容器中怎么創(chuàng)建一個(gè)MariaDB鏡像問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。