制作基于 mysql 的镜像

直接通过 commit 提交包含数据的MySQL镜像是无效的.

因为 mysql 的镜像使用了卷,而commit不会提交这些数据。

解决方案:

或许可以通过推荐方法来完成这个这件事情

创建一个 Dockerfile 文件。

# @see https://hub.docker.com/_/mysql
FROM mysql:8.0.28

# 创建数据库后按照此文件里面的配置进行初始化,文件将按字母顺序执行
COPY ./docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

# 初始化一个数据库
ENV MYSQL_DATABASE=myDatabase

# 设置root账户密码
ENV MYSQL_ROOT_PASSWORD=123456

myDatabase 是你的数据库名称

123456 是你的 root 账户密码

紧接着,在 ./docker-entrypoint-initdb.d 目录里,放入你你用来初始化数据库的文件,比如 .sql 文件。

摘抄官网原文:When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

译文:当容器第一次启动时,将创建一个具有指定名称的新数据库,并使用提供的配置变量进行初始化。此外,它将执行在/docker-entrypoint-initdb.d中找到的扩展名为.sh、.sql和.sql.gz的文件。文件将按字母顺序执行。您可以通过将SQL转储挂载到该目录中,并使用贡献的数据提供自定义映像来轻松填充mysql服务。默认情况下,SQL文件将被导入到由MYSQL_DATABASE变量指定的数据库中。

参考网站:mysql – Official Image | Docker Hub