使用准备好的数据库方案创建 docker mysql 容器

Tom*_*ino 31 mysql docker

我想在 mysql 之上创建一个 docker 图像,该图像已经包含我的应用程序所需的方案。

我尝试向 Dockerfile 添加行,将我的方案作为 sql 文件导入。我是这样做的(我的 Dockerfile):

FROM mysql

ENV MYSQL_ROOT_PASSWORD="bagabu"
ENV MYSQL_DATABASE="imhere"

ADD imhere.sql /tmp/imhere.sql

RUN "mysql -u root --password="bagabu" imhere < /tmp/imhere.sql"
Run Code Online (Sandbox Code Playgroud)

据我了解,这是行不通的,因为 mysql docker 映像不包含 mysql 客户端(最佳实践状态“不要添加东西只是因为它们会很好”)(我错了吗?)

这样做的好方法是什么?我想到了一些事情,但它们似乎都是凌乱的解决方法。

  1. 安装 mysql 客户端,做我必须做的事情,然后删除/清除它。
  2. 将 mysql 客户端二进制文件复制到映像中,执行我必须执行的操作,然后将其删除。
  3. 在另一个 sql server 中创建架构并直接复制 db 文件本身(这看起来很混乱,对我来说听起来像是一个受污染的问题池)

有什么建议?希望以一种易于以后维护并且也可能符合最佳实践的方式?

小智 27

为了测试目的,我必须这样做。

下面是我如何利用 dockerhub 上的实际 MySQL/MariaDB 图像和多阶段构建:

FROM mariadb:latest as builder

# That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init
RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"]

# needed for intialization
ENV MYSQL_ROOT_PASSWORD=root

COPY setup.sql /docker-entrypoint-initdb.d/

# Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume.
# https://docs.docker.com/engine/reference/builder/#volume :
#       Changing the volume from within the Dockerfile: If any build steps change the data within the volume after
#       it has been declared, those changes will be discarded.
RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db", "--aria-log-dir-path", "/initialized-db"]

FROM mariadb:latest

COPY --from=builder /initialized-db /var/lib/mysql
Run Code Online (Sandbox Code Playgroud)

完整的工作示例:https : //github.com/lindycoder/prepopulated-mysql-container-example

  • 我在整个互联网上找到的最佳答案,我的是 postgres,所以它有点困难,但终于让它工作了。它完美地工作! (3认同)

Gre*_*cki 24

您应该将 init 脚本放在安装为的目录中/docker-entrypoint-initdb.d- 请参阅MySQL Docker 映像文档中的“初始化新实例”部分。

  • 这适用于调出一个新容器并将其加载到我的架构中。这是一个很好的解决方法,但是如果我正在寻找一种方法来*创建*我自己的已经预先安装了架构的docker镜像呢? (2认同)

小智 7

归功于@Martin Roy

对 mysql 进行了细微的更改...

内容 Dockerfile

FROM mysql:latest as builder

# That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init
RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"]

# needed for intialization
ENV MYSQL_ROOT_PASSWORD=root

COPY setup.sql /docker-entrypoint-initdb.d/

# Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume.
# https://docs.docker.com/engine/reference/builder/#volume :
#       Changing the volume from within the Dockerfile: If any build steps change the data within the volume after
#       it has been declared, those changes will be discarded.
RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"]

FROM mysql:latest

COPY --from=builder /initialized-db /var/lib/mysql
Run Code Online (Sandbox Code Playgroud)

内容设置.sql

CREATE DATABASE myexample;

USE myexample;

CREATE TABLE mytable (myfield VARCHAR(20));

INSERT INTO mytable VALUES ('Hello'), ('Dolly');
Run Code Online (Sandbox Code Playgroud)

完整的工作示例:https : //github.com/iamdvr/prepopulated-mysql-container-example