0

使用Dockerfile构建MySQL,COPY进去一个sql脚本和sh脚本,想在构建mysql的新建一个表,但是新建不出来。我通过exec的方式进入构建好的镜像,然后运行sh脚本,可以正确执行sql脚本并建表。

Dockerfile

FROM mysql:5.7

#设置免密码登陆
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes 
#ENV MYSQL_ROOT_PASSWORD=MYSQL_ROOT_PASSWORD=123456
ENV MYSQL_USER=admin
ENV MYSQL_PASSWORD=123456ex

#导入启动脚本
COPY setup.sh /mysql/setup.sh
COPY patent.sql /mysql/patent.sql
#COPY admin.sql /mysql/admin.sql

#设置工作目录
WORKDIR /mysql

#设置容器启动时执行的命令
#CMD ["sh", "/mysql/setup.sh"]
RUN ["sh", "/mysql/setup.sh"]

setup.sh

#!/bin/bash
echo hello world
echo `service mysql status`
service mysql start
echo `service mysql status`
mysql  < /mysql/patent.sql
echo OVERRRRRR

sql脚本太长了,我不贴了,本机测试通过。

build命令行输出

Sending build context to Docker daemon  12.29kB
Step 1/9 : FROM mysql:5.7
 ---> 7faa3c53e6d6
Step 2/9 : ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
 ---> Using cache
 ---> 4fabf469312c
Step 3/9 : ENV MYSQL_USER=admin
 ---> Running in 5e3da3a31744
Removing intermediate container 5e3da3a31744
 ---> 267c74fe58c7
Step 4/9 : ENV MYSQL_PASSWORD=123456ex
 ---> Running in 6951eb6cc4e9
Removing intermediate container 6951eb6cc4e9
 ---> 6715c8261a90
Step 5/9 : COPY setup.sh /mysql/setup.sh
 ---> 27e89c10657f
Step 6/9 : COPY patent.sql /mysql/patent.sql
 ---> fdb4a2a1d8f7
Step 7/9 : COPY admin.sql /mysql/admin.sql
 ---> 68adb834142c
Step 8/9 : WORKDIR /mysql
 ---> Running in f1ac5eaab5a6
Removing intermediate container f1ac5eaab5a6
 ---> c1a8b1b18f01
Step 9/9 : RUN ["sh", "/mysql/setup.sh"]
 ---> Running in c45e2c484a60
hello world
MySQL Community Server 5.7.26 is not running.
2019-05-28T14:20:15.772624Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-28T14:20:16.640724Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-05-28T14:20:16.726558Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-28T14:20:16.788985Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b73b333d-8153-11e9-aa78-0242ac110002.
2019-05-28T14:20:16.793273Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-05-28T14:20:16.794347Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
..
MySQL Community Server 5.7.26 is started.
MySQL Community Server 5.7.26 is running.
OVERRRRRR
Removing intermediate container c45e2c484a60
 ---> ee4d3df6ed75
Successfully built ee4d3df6ed75
Successfully tagged mysql:v5
松子 8
2019-05-28 提问
1 个回答
0

容器里entrypoint.sh--init-file加入初始化SQL

撰写答案

推广链接