docker版mysql的使用和配置(2)——docker版mysql的dockerfile

既然目標是定製知足本身須要的dockerfile,那麼就來看看mysql的dockerfile長什麼樣。html

dockerfile選擇的是 https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/Dockerfilenode

緣由是這個比較短(捂臉)mysql

關於dockerfile中的各類命令,能夠查看官方文檔,或者參考這篇: https://www.cnblogs.com/jie-fang/p/7927643.htmllinux

 1 # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
 2 #
 3 # This program is free software; you can redistribute it and/or modify
 4 # it under the terms of the GNU General Public License as published by
 5 # the Free Software Foundation; version 2 of the License.
 6 #
 7 # This program is distributed in the hope that it will be useful,
 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
15 FROM oraclelinux:7-slim
16 
17 ARG MYSQL_SERVER_PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm
18 ARG MYSQL_SHELL_PACKAGE_URL=https://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/mysql-shell-8.0.12-1.el7.x86_64.rpm
19 
20 # Install server
21 RUN rpmkeys --import https://repo.mysql.com/RPM-GPG-KEY-mysql \
22   && yum install -y $MYSQL_SERVER_PACKAGE_URL $MYSQL_SHELL_PACKAGE_URL libpwquality \
23   && yum clean all \
24   && mkdir /docker-entrypoint-initdb.d
25 
26 VOLUME /var/lib/mysql
27 
28 COPY docker-entrypoint.sh /entrypoint.sh
29 COPY healthcheck.sh /healthcheck.sh
30 ENTRYPOINT ["/entrypoint.sh"]
31 HEALTHCHECK CMD /healthcheck.sh
32 EXPOSE 3306 33060
33 CMD ["mysqld"]

我們一行一行看。git

FROM oraclelinux:7-slim

15行,這裏是oracle本身的linux,不過看後面使用的是yum,咱們本身配置的時候大概能夠用centos代替。github

ARG MYSQL_SERVER_PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm
ARG MYSQL_SHELL_PACKAGE_URL=https://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/mysql-shell-8.0.12-1.el7.x86_64.rpm

17和18兩行,定義了兩個變量用來指向接下來須要用的rpm包的地址。redis

RUN rpmkeys --import https://repo.mysql.com/RPM-GPG-KEY-mysql \
  && yum install -y $MYSQL_SERVER_PACKAGE_URL $MYSQL_SHELL_PACKAGE_URL libpwquality \
  && yum clean all \
  && mkdir /docker-entrypoint-initdb.d

21~24行,這裏安裝了mysql包。若是咱們進行簡化的話,rpmkeys和libpwquality均可以省略掉。sql

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh

26~31行,這裏複製了須要的腳本文件,重點是entrypoint.sh。healthcheck若是進行簡化的話也能夠省略掉。docker

EXPOSE 3306 33060

32行規定了向主機暴露的端口號。若是不須要的話能夠省略。另外注意,並非expose了就能夠用,仍是須要在run的時候進行端口映射。shell

CMD ["mysqld"]

最後33行是dockerfile執行的啓動命令(的參數)。

根據上面的分析,咱們本身的dockerfile的mysql部分,大概應該是這樣的:

FROM centos:7.2.1511

ARG MYSQL_SERVER_PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm
ARG MYSQL_SHELL_PACKAGE_URL=https://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/mysql-shell-8.0.12-1.el7.x86_64.rpm
RUN yum install -y --nogpgcheck $MYSQL_SERVER_PACKAGE_URL $MYSQL_SHELL_PACKAGE_URL
COPY docker-entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["mysqld"]

而後就能夠先試試看了:

shell> docker build -t mysql_basic .
Sending build context to Docker daemon  10.24kB
Step 1/7 : FROM centos:7.2.1511
 ---> ddc0fb7d7a72
Step 2/7 : ARG MYSQL_SERVER_PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm
 ---> Running in 3ff12f470551
Removing intermediate container 3ff12f470551
 ---> a544211d9806
Step 3/7 : ARG MYSQL_SHELL_PACKAGE_URL=https://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/mysql-shell-8.0.12-1.el7.x86_64.rpm
 ---> Running in fa793935528a
Removing intermediate container fa793935528a
 ---> ecc70ab46823
Step 4/7 : RUN yum install -y --nogpgcheck $MYSQL_SERVER_PACKAGE_URL $MYSQL_SHELL_PACKAGE_URL
 ---> Running in 6f3dd9f850f2
Loaded plugins: fastestmirror
Examining /var/tmp/yum-root-2ratBJ/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm: mysql-community-server-minimal-5.7.23-1.el7.x86_64
Marking /var/tmp/yum-root-2ratBJ/mysql-community-server-minimal-5.7.23-1.el7.x86_64.rpm to be installed
Examining /var/tmp/yum-root-2ratBJ/mysql-shell-8.0.12-1.el7.x86_64.rpm: mysql-shell-8.0.12-1.el7.x86_64
Marking /var/tmp/yum-root-2ratBJ/mysql-shell-8.0.12-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server-minimal.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
Determining fastest mirrors
 * base: mirror.hmc.edu
 * extras: mirror.nodesdirect.com
 * updates: mirror.teklinks.com
--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.1)(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.2)(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
--> Processing Dependency: libaio.so.1()(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
--> Processing Dependency: libnuma.so.1()(64bit) for package: mysql-community-server-minimal-5.7.23-1.el7.x86_64
---> Package mysql-shell.x86_64 0:8.0.12-1.el7 will be installed
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
---> Package numactl-libs.x86_64 0:2.0.9-7.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package        Arch   Version         Repository                          Size
================================================================================
Installing:
 mysql-community-server-minimal
                x86_64 5.7.23-1.el7    /mysql-community-server-minimal-5.7.23-1.el7.x86_64
                                                                           67 M
 mysql-shell    x86_64 8.0.12-1.el7    /mysql-shell-8.0.12-1.el7.x86_64    25 M
Installing for dependencies:
 libaio         x86_64 0.3.109-13.el7  base                                24 k
 numactl-libs   x86_64 2.0.9-7.el7     base                                29 k

Transaction Summary
================================================================================
Install  2 Packages (+2 Dependent packages)

Total size: 92 M
Total download size: 54 k
Installed size: 92 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               33 kB/s |  54 kB  00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : numactl-libs-2.0.9-7.el7.x86_64                              1/4 
  Installing : libaio-0.3.109-13.el7.x86_64                                 2/4 
  Installing : mysql-community-server-minimal-5.7.23-1.el7.x86_64           3/4 
  Installing : mysql-shell-8.0.12-1.el7.x86_64                              4/4 
  Verifying  : mysql-community-server-minimal-5.7.23-1.el7.x86_64           1/4 
  Verifying  : libaio-0.3.109-13.el7.x86_64                                 2/4 
  Verifying  : numactl-libs-2.0.9-7.el7.x86_64                              3/4 
  Verifying  : mysql-shell-8.0.12-1.el7.x86_64                              4/4 

Installed:
  mysql-community-server-minimal.x86_64 0:5.7.23-1.el7                          
  mysql-shell.x86_64 0:8.0.12-1.el7                                             

Dependency Installed:
  libaio.x86_64 0:0.3.109-13.el7        numactl-libs.x86_64 0:2.0.9-7.el7       

Complete!
Removing intermediate container 6f3dd9f850f2
 ---> e118192698af
Step 5/7 : COPY docker-entrypoint.sh /entrypoint.sh
 ---> 1aab375ae211
Step 6/7 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Running in 4c99517c3da0
Removing intermediate container 4c99517c3da0
 ---> 54f44871daeb
Step 7/7 : CMD ["mysqld"]
 ---> Running in f534cb3d8f0e
Removing intermediate container f534cb3d8f0e
 ---> dc0ae9f1dc87
Successfully built dc0ae9f1dc87
Successfully tagged mysql_basic:latest

看看image

shell> docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED              SIZE
mysql_basic                                             latest              dc0ae9f1dc87        About a minute ago   400MB

跑起來看看

shell> docker run -d --name mysqlbasic mysql_basic
shell> docker ps
CONTAINER ID        IMAGE                                                           COMMAND                  CREATED             STATUS              PORTS               NAMES
a7efdfdaa91d        mysql_basic                                                     "/entrypoint.sh mysq…"   5 seconds ago       Up 3 seconds                            mysqlbasic

再看看docker裏面的狀況

docker exec -it mysqlbasic bash
[root@a7efdfdaa91d /]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

哈哈access denied。上一篇說過的,第一次密碼是隨機生成的。那麼咱們來拿密碼試試。

shell> docker logs mysqlbasic
[Entrypoint] MySQL Docker Image 5.7.23-1.1.7
[Entrypoint] No password option specified for new database.
[Entrypoint]   A random onetime password will be generated.
[Entrypoint] Initializing database
[Entrypoint] Database initialized
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: 0D0horOvZOHufUpuvAr0L]UkOBX3

[Entrypoint] ignoring /docker-entrypoint-initdb.d/*

[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.

[Entrypoint] MySQL init process done. Ready for start up.

[Entrypoint] Starting MySQL 5.7.23-1.1.7

[root@a7efdfdaa91d /]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

成功的登陸了root帳號。這樣,咱們就把dockerfile裏關鍵的部分提取出來,而且能夠正常的build和run了。

接下來的問題,咱們不能每次都先找隨機生成的密碼,而後再登陸。因此咱們要把mysql的初始化和啓動服務的過程搞清楚。這樣才能根據本身的要求進行修改。

接下來是下一篇的內容了!

 

PS:其實這種方法是有其自己的問題的……好比,不少時候,咱們並不要把docker扔到後臺運行,而是直接在前臺運行一個bash。

那麼咱們來試試剛纔build的image。

shell> docker run -it --name mysqlbasic mysql_basic /bin/bash
[Entrypoint] MySQL Docker Image 5.7.23-1.1.7
[root@4e2ee3952247 /]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

首先,咱們和剛纔用docker logs打出的log對比一下,發現這裏Entrypoint只有一行。看來先後臺的docker啓動方式有一些不一樣。

那麼這種不一樣致使了什麼區別呢?嘗試一下mysql。哎呀竟然報了錯誤。

看來須要仔細研究一下entrypoint.sh到底幹了什麼。(就是下一篇的內容了!)

相關文章
相關標籤/搜索