简体   繁体   中英

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. Here's my docker file FROM 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

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.

From what i know CMD accepts only two commands and running the restoredb.sh after startup.sh replaces it. I just want to restore the mysql Database and run mysql and apache in foreground. I can't use docker-compose as per the requirement I have.

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:

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 . 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 files will be imported by default to the database specified by the MYSQL_DATABASE variable.

(from the MySQL page on DockerHub )

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.

Also, your startup.sh might work with some changes:

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

This will start the MySQL service first, then populate the DB. Then, finally, start up Apache.

One last thing, the preferred form of CMD is the exec form. This would have your CMD look like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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