【问题标题】:Dockerfile initialize mysql database, run script not foundDockerfile初始化mysql数据库,找不到运行脚本
【发布时间】:2020-10-21 06:14:21
【问题描述】:

我是 docker 新手,正在尝试初始化 MySQL 数据库。我一直在关注以下资源: How can I initialize a MySQL database with schema in a Docker container?

我的文件夹结构:

我的 Dockerfile:

FROM mysql

# Copy the database schema to the /data directory
ADD files /tmp/

# init_db will create the default
# database from epcis_schema.sql, then
# stop mysqld, and finally copy the /var/lib/mysql directory
# to default_mysql_db.tar.gz
RUN chmod +x /tmp/init_db
RUN /tmp/init_db

# run_db starts mysqld, but first it checks
# to see if the /var/lib/mysql directory is empty, if
# it is it is seeded with default_mysql_db.tar.gz before
# the mysql is fired up


ENTRYPOINT "/tmp/files/run_db"

但是,当我尝试运行 init_db 脚本时出现错误:

docker-compose up
Building db
Step 1/5 : FROM mysql
 ---> b5c10a3624ce
Step 2/5 : ADD files /tmp/
 ---> Using cache
 ---> 442ac0d3181e
Step 3/5 : RUN chmod +x /tmp/init_db
 ---> Using cache
 ---> c7f2a0abc246
Step 4/5 : RUN /tmp/init_db
 ---> Running in 7b4f397586f3
/bin/sh: 1: /tmp/init_db: not found
ERROR: Service 'db' failed to build : The command '/bin/sh -c /tmp/init_db' returned a non-zero code: 127

当我在步骤 3 中执行 chmod 时,它能够找到该文件;但是,未能在第 4 步中运行。

init_db:

#!/bin/bash

# Initialize MySQL database.
# ADD this file into the container via Dockerfile.
# Assuming you specify a VOLUME ["/var/lib/mysql"] or `-v /var/lib/mysql` on the `docker run` command…
# Once built, do e.g. `docker run your_image /path/to/docker-mysql-initialize.sh`
# Again, make sure MySQL is persisting data outside the container for this to have any effect.

set -e
set -x

mysql_install_db

# Start the MySQL daemon in the background.
/usr/sbin/mysqld &
mysql_pid=$!

until mysqladmin ping >/dev/null 2>&1; do
  echo -n "."; sleep 0.2
done

# Permit root login without password from outside container.
mysql -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION"

# create the default database from the ADDed file.
mysql < /tmp/create_stock_data_db.sql

# create historical_data table
mysql < /tmp/create_historical_data.sql

# Tell the MySQL daemon to shutdown.
mysqladmin shutdown

# Wait for the MySQL daemon to exit.
wait $mysql_pid

# create a tar file with the database as it currently exists
tar czvf default_mysql.tar.gz /var/lib/mysql

# the tarfile contains the initialized state of the database.
# when the container is started, if the database is empty (/var/lib/mysql)
# then it is unpacked from default_mysql.tar.gz from
# the ENTRYPOINT /tmp/run_db script

我们将不胜感激。谢谢!

【问题讨论】:

    标签: mysql docker shell


    【解决方案1】:

    运行指令将在现有图像之上的新层中执行任何命令并提交结果。

    因此,每个运行指令都会创建一个新图像并启动MySQL 服务器。创建数据库应结合在同一条RUN 指令中。

    RUN chmod -R 775 /tmp/init_db

    【讨论】:

      【解决方案2】:

      mysql docker library 图像已提供初始化。

      Env MYSQL_ALLOW_EMPTY_PASSWORD=1 将允许 root 密码为空。

      您的两个 sql 文件需要放在 /docker-entrypoint-initdb.d 中,这些文件将在启动时加载。这可以作为一个卷公开或创建一个分层的 docker 文件。

      请参阅上面 docker 库链接上的“初始化新实例”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-26
        • 2014-11-14
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        • 2019-11-29
        • 2022-09-26
        • 1970-01-01
        相关资源
        最近更新 更多