简体   繁体   English

Docker-如何在容器上运行.sql文件来启动mysql数据库?

[英]Docker - How can I initiate mysql database with a .sql file running on a container?

It is my first time working with docker. 这是我第一次与docker合作。 I want to create a remote container with test environment for my java application. 我想为我的Java应用程序创建一个带有测试环境的远程容器。 I also need mysql database. 我还需要mysql数据库。 Everything should run on one container (it's a requirement, not my idea). 一切都应该在一个容器上运行(这是必要条件,不是我的主意)。

I need to have a db initiated with .sql file (let's say file.sql located in the same directory as Dockerfile) 我需要使用.sql文件启动数据库(假设file.sql与Dockerfile位于同一目录中)

Thank you very much for help :) 非常感谢您的帮助:)

Here is part of my Dockerfile: 这是我的Dockerfile的一部分:

FROM ubuntu:16.04

#Install JRE
RUN     apt-get update && apt-get install default-jre -y

#Install JDK

RUN     apt-get update && apt-get install default-jdk -y

#Install Maven

RUN     apt-get update && apt-get install maven -y

#Install wget
RUN     apt-get update && apt-get install wget -y
#\&& rm -rf /var/lib/apt/lists/*


#Install unzip

RUN     apt-get update && apt-get install unzip -y

#Install glassfish

RUN     mkdir -p /opt && cd opt/
RUN     wget http://download.java.net/glassfish/4.1.1/release/glassfish-4.1.1.zip
RUN     unzip glassfish-4.1.1.zip

#Install mysql

ENV     MYSQL_USER=mysql\ MYSQL_DATA_DIR=/var/lib/mysql \ MYSQL_RUN_DIR=/run/mysqld \ MYSQL_LOG_DIR=/var/log/mysql

RUN     apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server \&& rm -rf ${MYSQL_DATA_DIR} \&& rm -rf /var/lib/apt/lists/*

First use a ADD to copy your .sql file in the image. 首先使用ADD复制映像中的.sql文件。 Then add a RUN command to restore your DB using mysql commands. 然后添加RUN命令以使用mysql命令还原数据库。

Thank you all for answers! 谢谢大家的回答!

I found a sollution if anyone will encouter the same problem it's here: 如果有人遇到同样的问题,我发现了解决方法:

RUN     echo "mysql-server mysql-server/root_password password password" | debconf-set-selections
RUN     echo "mysql-server mysql-server/root_password_again password password" | debconf-set-selections
RUN     apt-get update && apt-get -y install mysql-server && service mysql start

if mysql is still not starting check /etc/docker/daemon.json, make sure it looks like this: 如果mysql仍然没有启动,请检查/etc/docker/daemon.json,确保它看起来像这样:

{ "storage-driver":"overlay2" }

if you don't have this file execute: 如果没有此文件,请执行:

mkdir -p /etc/docker

echo '{"storage-driver":"overlay2"}' > /etc/docker/daemon.json

systemctl restart docker.service

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

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