docker中如何创建MariaDB镜像?本篇文章重点为大家讲解一下docker容器创建MariaDB镜像具体方法,有需要的小伙伴可以参考一下。
公司主营业务:网站设计、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出珠海免费做网站回馈大家。
[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
[root@e8126d0481d2 /]# yum -y install mariadb-server net-tools
[root@e8126d0481d2 /]# mysql_install_db --user=mysql
[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 '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
[root@e8126d0481d2 /]# mysql -u root -h 172.17.0.2 -p
Enter password:
MariaDB [(none)]> exit
[root@e8126d0481d2 ~]# cat run.sh#!/bin/shmysqld_safe
[root@test01 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe8126d0481d2 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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESeed3e88a1261 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 -pMariaDB [(none)]>
设置创建目录和文件
[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 123456
#让容器支持中文
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
文章标题:Docker创建MariaDB镜像具体方法
网站URL:http://www.shufengxianlan.com/qtweb/news16/176066.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联