简体   繁体   English

如何在 dockerfile 中恢复 mysql db

[英]How can mysql db be restored in dockerfile

I'm trying to restore MySQL DB to a ubuntu docker container which has Apache and MySQL services. I'm trying to restore MySQL DB to a ubuntu docker container which has Apache and MySQL services. Here's my docker file FROM ubuntu这是我的 docker 文件,来自 ubuntu

RUN apt-get update -y
ENV DATABASE_SERVER 'IP'
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install apache2 -y && apt-get install php7.4 -y && apt-get install mysql-server >
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_RUN_DIR /var/www/html/
COPY ./startup.sh /var/www/
COPY ./db_test.php /var/www/html
COPY ./my_sql_secure.sh /var/www/
COPY ./backup.sql /var/www/html/
RUN bash /var/www/my_sql_secure.sh
COPY ./restoredb.sh /var/www/
CMD bash /var/www/startup.sh
EXPOSE 80

Here's my startup.sh这是我的startup.sh

apache2 -DFOREGROUND | service mysql start | mysql -uroot sentrifugo < /var/www/html/backup.sql

If I run startup.sh without "mysql -uroot sentrifugo < /var/www/html/backup.sql", the script properly brings up mysql service but when I run with it doesn't run.如果我在没有“mysql -uroot sentrifugo < /var/www/html/backup.sql”的情况下运行 startup.sh,脚本会正确启动 mysql 服务,但是当我使用它运行时它不会运行。

From what i know CMD accepts only two commands and running the restoredb.sh after startup.sh replaces it.据我所知,CMD 只接受两个命令,并在 startup.sh 替换它之后运行 restoreb.sh。 I just want to restore the mysql Database and run mysql and apache in foreground.我只想恢复 mysql 数据库并在前台运行 mysql 和 apache。 I can't use docker-compose as per the requirement I have.根据我的要求,我不能使用 docker-compose。

Could someone please tell me what can be done to achieve it.有人可以告诉我可以做些什么来实现它。

Thanks a lot in advance非常感谢提前

It really depends on what image you are building FROM , assuming that it is an official MySQL image, you just COPY your backup.sql into the seed folder:这实际上取决于您要构建的图像FROM ,假设它是官方 MySQL 图像,您只需将您的backup.sql COPY到种子文件夹中:

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 .此外,它将执行 /docker-entrypoint-initdb.d 中的扩展名为.sh.sql.sql.gz/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 转储安装到该目录中轻松填充 mysql 服务,并提供带有贡献数据的自定义图像。 SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable. SQL 文件将默认导入到 MYSQL_DATABASE 变量指定的数据库中。

(from the MySQL page on DockerHub ) (来自DockerHub 上的 MySQL 页面

So, change:所以,改变:

COPY ./backup.sql /var/www/html/

...to: ...至:

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

If you are using a custom image, and it seems that you may be, the you might want to have an ENTRYPOINT that executes that import script on startup.如果您使用的是自定义图像,并且您可能会使用自定义图像,您可能希望有一个在启动时执行该导入脚本的ENTRYPOINT

Also, your startup.sh might work with some changes:此外,您的startup.sh可能会进行一些更改:

service mysql start && \
mysql -uroot sentrifugo < /var/www/html/backup.sql;
apache2 -DFOREGROUND

This will start the MySQL service first, then populate the DB.这将首先启动 MySQL 服务,然后填充数据库。 Then, finally, start up Apache.然后,最后,启动 Apache。

One last thing, the preferred form of CMD is the exec form.最后一件事, CMD的首选形式是exec形式。 This would have your CMD look like this:这将使您的CMD看起来像这样:

CMD ["/bin/bash","/var/www/startup.sh"]

(ref: https://docs.docker.com/engine/reference/builder/#cmd ) (参考: https://docs.docker.com/engine/reference/builder/#cmd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM