1. 程式人生 > >docker之建立MariaDB映象

docker之建立MariaDB映象

一、基於commit命令方式建立

  1. docker的安裝
[[email protected] ~]# yum install docker
[[email protected] ~]# systemctl enable docker
[[email protected] ~]# systemctl start docker
  1. 下載本地映象
[[email protected] ~]# docker pull centos:7.4.1708
[[email protected] ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
  1. 建立互動型容器
[[email protected] ~]# docker run -it --name="mysql_server" centos /bin/bash

4.安裝mariadb服務

[[email protected]
/]# yum -y install mariadb-server net-tools
  1. 初始化mariadb
[[email protected] /]# mysql_install_db --user=mysql
  1. 後臺啟動mariadb服務
[[email protected] /]# mysqld_safe &
[1] 114
[[email protected] /]# 
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 [[email protected] /]# 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 -
  1. 建立mariadb登入密碼,並可以指定ip登入
[[email protected] /]# mysqladmin -u root password '123456'
[[email protected] /]# 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
  1. 容器登入驗證
[[email protected] /]# mysql -u root -h 172.17.0.2 -p
Enter password:
MariaDB [(none)]> exit
  1. 建立容器啟動指令碼
[[email protected] ~]# cat run.sh
#!/bin/sh

mysqld_safe
  1. 建立映象
[[email protected] ~]# 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
[[email protected] ~]# docker commit mysql_server mariadb:1.0
  1. 建立容器
[[email protected] ~]# docker run -d -p 13306:3306 mariadb:1.0 /root/run.sh
[[email protected] ~]# 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
  1. 主機登入驗證
[[email protected] ~]# yum -y install mariadb
[[email protected] ~]# mysql -u root --port=13306 -p
MariaDB [(none)]>

二、基於Dockerfile方式建立

  1. 設定建立目錄和檔案
[[email protected] ~]# mkdir mariadb_dockerfile && cd mariadb_dockerfile
[[email protected] mariadb_dockerfile]# touch db_init.sh 
[[email protected] mariadb_dockerfile]# touch run.sh
  1. 編輯Dockerfile等檔案
    Dockerfile
[[email protected] 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

[[email protected] 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

[[email protected] mariadb_dockerfile]# cat run.sh 
#!/bin/bash
mysqld_safe
  1. 建立映象
[[email protected] mariadb_dockerfile]# docker build -t liuxin/centos-mariadb:v1 ./
  1. 建立容器
[[email protected] mariadb_dockerfile]# docker run -d -p 13306:3306 liuxin/centos-mariadb:v1 /root/run.sh
[[email protected] 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
  1. 登入驗證
[[email protected] 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


作者:小小運維
連結:https://www.jianshu.com/p/e64726f90a49
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。