Docker 使用Dockerfile构建MySQL镜像(十五)

Docker 使用Dockerfile构建MySQL镜像(十五)

Scroll Down

本片讲解使用Docker 使用Dockerfile构建MySQL镜像。

上篇是
《Docker Dockerfile指令说明(十二)》
《Docker Dockerfile创建镜像说明(十三)》
《Docker 使用Dockerfile构建nginx镜像(十四)》

1_fqwE_Sk2pCO5orZTRoivgg.png

在此篇文章出了不少亏 :

  • 1.基础镜像版本不一样,结果完全不一样,开始用的ubuntu:latest镜像,一直出现故障,运行后密码无法登录!更换ubuntu:16.04 解决问题。
  • 2.使用docker run的方式配置root密码不起作用,确实一直没有起作用。知道原因的麻烦留言一下!

这种方式制作的镜像MySQL密码为空!!!进去后自己设置!!!

1、下载镜像

docker pull ubuntu:16.04

2、创建工作目录

mkdir /www/mysql
cd /www/mysql/

3、创建并编辑Dockerfile文件

FROM ubuntu:16.04                      # 基础景象
LABEL maintainer="dongzao"             # 镜像制作信息
ARG MYSQL_VERSION=5.7.29               # 指定MySQL版本
ENV DEBIAN_FRONTEND noninteractive     # 

RUN groupadd -r mysql && useradd -r -g mysql mysql      # 创建MySQL用户组和用户

RUN apt-get update && apt-get install -y --no-install-recommends \      # 安装更新系统
    mysql-server \                                                      # 安装MySQL
    openssl \                                                           # 安装openssl
    && apt autoremove -y \                                              # 删除无关的包
    && apt-get clean && rm -rf /var/lib/apt/lists/*                     

RUN mkdir -p /var/lib/mysql /var/run/mysqld \                           
    && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \
    # Ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
    && chmod 777 /var/run/mysqld

VOLUME /var/lib/mysql                                                   # 设置工作目录

USER mysql                                                              # 指定容器用户
EXPOSE 3306 33060                                                       # 开放端口
CMD ["mysqld"]                                                          # 运行MySQL

安装过程:

[root@docker mysql]# docker build -t mysql_1 .
Sending build context to Docker daemon  664.8MB
Step 1/11 : FROM ubuntu:16.04
16.04: Pulling from library/ubuntu
0a01a72a686c: Pull complete 
cc899a5544da: Pull complete 
19197c550755: Pull complete 
716d454e56b6: Pull complete 
Digest: sha256:3f3ee50cb89bc12028bab7d1e187ae57f12b957135b91648702e835c37c6c971
Status: Downloaded newer image for ubuntu:16.04
 ---> 96da9143fb18
Step 2/11 : LABEL maintainer="dongzao"
 ---> Running in 597ce489d809
Removing intermediate container 597ce489d809
 ---> 9e268e8947d7
Step 3/11 : ARG MYSQL_VERSION=5.7.29
 ---> Running in 74d275ae1314
Removing intermediate container 74d275ae1314
 ---> c32ef4fe9dcf
Step 4/11 : ENV DEBIAN_FRONTEND noninteractive
 ---> Running in 432ffa44a50b
Removing intermediate container 432ffa44a50b
 ---> 9b6f4f8f2f88
Step 5/11 : RUN groupadd -r mysql && useradd -r -g mysql mysql
 ---> Running in e771280f6b2c
Removing intermediate container e771280f6b2c
 ---> b76e4d82e1c0
Step 6/11 : RUN apt-get update && apt-get install -y --no-install-recommends     mysql-server     openssl     && apt autoremove -y     && apt-get clean && rm -rf /var/lib/apt/lists/*
 ---> Running in 36d1e84c84bb
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
-----省略-----
Get:18 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8807 B]
Fetched 16.3 MB in 1min 55s (141 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
-----省略-----
0 upgraded, 33 newly installed, 0 to remove and 17 not upgraded.
Need to get 31.2 MB of archives.
After this operation, 227 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libssl1.0.0 amd64 1.0.2g-1ubuntu4.15 [1084 kB]
-----省略-----
Processing triggers for systemd (229-4ubuntu21.23) ...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.
Removing intermediate container 36d1e84c84bb
 ---> 7ffb83b53313
Step 7/11 : RUN mkdir -p /var/lib/mysql /var/run/mysqld     && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld     && chmod 777 /var/run/mysqld
 ---> Running in 0ac881dac82a
Removing intermediate container 0ac881dac82a
 ---> b710de321863
Step 8/11 : VOLUME /var/lib/mysql
 ---> Running in 143b7a96fbc9
Removing intermediate container 143b7a96fbc9
 ---> 78e2cff83043
Step 9/11 : USER mysql
 ---> Running in a85fa8bac1a8
Removing intermediate container a85fa8bac1a8
 ---> 4f2699e709ae
Step 10/11 : EXPOSE 3306 33060
 ---> Running in ae84e3f808cd
Removing intermediate container ae84e3f808cd
 ---> 2f55d3cde2b3
Step 11/11 : CMD ["mysqld"]
 ---> Running in 897e02a986ce
Removing intermediate container 897e02a986ce
 ---> d42e2ede1e72
Successfully built d42e2ede1e72
Successfully tagged mysql_1:latest

生成的镜像

image.png

4、创建容器

docker run -itd -p 3306:3306 --name dongzao mysql_1

image.png

5、登陆容器进入MySQL

docker exec -it dongzao bash

image.png

6、MySQL设置密码

mysql> alter user 'root'@'localhost' identified by 'dongzao';   # 设置新密码
mysql> flush privileges;            #刷新
mysql> quit                         #退出

image.png

这样就完成了!

illust_77706341_20191114_201636.jpg