我想在 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 客户端(最佳实践状态“不要添加东西只是因为它们会很好”)(我错了吗?)
这样做的好方法是什么?我想到了一些事情,但它们似乎都是凌乱的解决方法。
有什么建议?希望以一种易于以后维护并且也可能符合最佳实践的方式?
小智 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
Gre*_*cki 24
您应该将 init 脚本放在安装为的目录中/docker-entrypoint-initdb.d
- 请参阅MySQL Docker 映像文档中的“初始化新实例”部分。
小智 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
归档时间: |
|
查看次数: |
103628 次 |
最近记录: |