既然目标是定制满足自己需要的dockerfile,那么就来看看mysql的dockerfile长什么样。

dockerfile选择的是 https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/Dockerfile

原因是这个比较短(捂脸)

关于dockerfile中的各种命令,可以查看官方文档,或者参考这篇: https://www.cnblogs.com/jie-fang/p/7927643.html

 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"]

咱们一行一行看。

FROM oraclelinux:7-slim

15行,这里是oracle自己的linux,不过看后面使用的是yum,我们自己配置的时候大概可以用centos代替。

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包的地址。

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都可以省略掉。

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如果进行简化的话也可以省略掉。

EXPOSE 3306 33060

32行规定了向主机暴露的端口号。如果不需要的话可以省略。另外注意,并不是expose了就可以用,还是需要在run的时候进行端口映射。

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到底干了什么。(就是下一篇的内容了!)

docker版mysql的使用和配置(2)——docker版mysql的dockerfile的更多相关文章

  1. MySQL数据库的同步配置+MySql读写分离

    使用mysql主从复制的好处有: 1.采用主从服务器这种架构,稳定性得以提升.如果主服务器发生故障,我们可以使用从服务器来提供服务. 2.在主从服务器上分开处理用户的请求,可以提升数据处理效率. 3. ...

  2. mysql主从服务器的配置

    使用mysql主从复制的好处有: 1.采用主从服务器这种架构,稳定性得以提升.如果主服务器发生故障,我们可以使用从服务器来提供服务. 2.在主从服务器上分开处理用户的请求,可以提升数据处理效率. 3. ...

  3. mysql 5.5 安装配置方法图文教程(转发)

    MySQL下载地址:http://dev.mysql.com/downloads/installer/ 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类型:Typical(典型 ...

  4. Ubuntu16.04中Mysql 5.7 安装配置

    记录在Ubuntu 16.04安装Mysql 5.7时遇到的一些问题. Mysql安装 使用如下命令进行安装: 1 sudo apt-get install mysql-server mysql-cl ...

  5. docker版mysql的使用和配置(1)——docker的基本操作

    最近实在是忙成狗,其他的内容等稍微闲一点了一起更新. 这篇主要是讲docker版的mysql的使用和配置信息.因为实习公司需要搞一个docker做测试环境用,还需要包括基本的依赖.最重要的是,因为这个 ...

  6. MySQL数据库学习笔记(一)----MySQL 5.6.21的安装和配置(setup版)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. MySQL安装 MySQL5.7.10免安装版配置,mysql5.7.10免安装版

    MySQL5.7.10免安装版配置,mysql5.7.10免安装版  最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: ...

  8. MySQL 8.0.x for Windows 解压缩版配置安装

    一.官网下载MySQL8.0.16 直达官网下载Community版:https://dev.mysql.com/downloads/mysql/ 然后拉倒下方点击对应版本位数下载 二.创建my.in ...

  9. Mysql系列(一)—— 基于5.7.22 解压版下载、安装、配置和卸载

    1.下载 从官网中直接获取自己想要的版本: MySQL Community Server 5.7.22 2.解压 将下载到的文件解压缩到自己喜欢的位置. 与mysql 5.6不同的是5.7版本中没有d ...

随机推荐

  1. Liunx运维(八)-LIunx磁盘与文件系统管理命令

    文档目录: 一.fdisk:磁盘分区工具 二.partprobe:更新内核的硬盘分区表信息 三.tune2fs:调整ext2/ext3/ext4文件系统参数 四.parted:磁盘分区工具 五.mkf ...

  2. 我是这样理解EventLoop的

    我是这样理解EventLoop的 一.前言   众所周知,在使用javascript时,经常需要考虑程序中存在异步的情况,如果对异步考虑不周,很容易在开发中出现技术错误和业务错误.作为一名合格的jav ...

  3. 第14章节 BJROBOT karto 算法构建地图【ROS全开源阿克曼转向智能网联无人驾驶车】

    建地图前说明:请确保你的小车已经校正好 IMU.角速度.线速度,虚拟机配置好 ROS 网络的前提进行,否则会造成构建地图无边界.虚拟机端无法正常收到小车主控端发布的话题数据等异常情况!! 1.把小车平 ...

  4. 翻译 | 30个 Python3 的最佳实践,技巧和窍门

    1.使用 Python3 如果你关注 Python 的话,应该会知道 Python 2 已经于今年(2020 年)1 月 1 日正式弃用了.这份教程的很多例子都是只支持 Python 3 的,如果你还 ...

  5. 在.NET Core中使用Channel(一)

    我最近一直在熟悉.net Core中引入的新Channel<T>类型.我想在它第一次发布的时候我了解过它,但是有关文章非常非常少,我不能理解它们与其他队列有什么不同. 在使用了一段时间后, ...

  6. [每日一题]面试官问:for in和for of 的区别和原理?

    关注「松宝写代码」,精选好文,每日一题 ​时间永远是自己的 每分每秒也都是为自己的将来铺垫和增值 作者:saucxs | songEagle 一.前言 2020.12.23 日刚立的 flag,每日一 ...

  7. go语言环境搭建以及配置VSCode

    Go语言学习笔记(环境安装)-day01 Go语言运行环境安装 下载Go安装包 安装包地址 安装Go语言运行环境 ​ 直接在下载好的目录双击运行*.msi的可执行文件,下一步进行安装,安装的目录最好是 ...

  8. 【Spring】Spring JdbcTemplate

    Spring JdbcTemplate 文章源码 JdbcTemplate 概述 它是 Spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.Spring 框架提供了很多的操 ...

  9. vim 手动添加脚本头部信息

    vim /root/.vimrc 8,1 全部 set autoindent set tabstop=5 set shiftwidth=4 function AddTitle() call setli ...

  10. 浅谈Go中的time.After

    go的一条哲学是 不要通过共享来实现通信,而是通信来实现共享 多协程之间通过 channel 来实现通信,而普遍会遇到的问题是,如何进行超时控制,资料一查询,需要配置select和time.After ...