在docker中为mySQL创build数据库和模式

我正在用tomcat和mySql创build一个docker镜像。 我有一个.war文件,我可以推送到Tomcat和泊坞窗图像工作正常。

但是,应用程序也需要在同一个docker镜像中的mySQL上的数据库(因为我不想运行多个图像,因为这是相当小的,仅用于演示)。

我使用的是一个基于tomcat的图像,并在其上安装mySql。 基本操作系统是Ubuntu。

这是我的dockerfile:

#Get the base FROM davidcaste/debian-tomcat:tomcat8 #Add mySql RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections RUN apt-get -y update RUN apt-get -y install wget zip gcc RUN { \ echo mysql-community-server mysql-community-server/data-dir select ''; \ echo mysql-community-server mysql-community-server/root-pass password ''; \ echo mysql-community-server mysql-community-server/re-root-pass password ''; \ echo mysql-community-server mysql-community-server/remove-test-db select false; \ } | debconf-set-selections \ && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server RUN /etc/init.d/mysql start RUN wget http://github.com/xxxx/xxxx/blob/master/xxxx/src/main/resources/sql/create-schema.sql RUN cp create-schema.sql /usr/ RUN wget http://github.com/xxxx/xxxx/blob/master/xxxx/src/main/resources/sql/metadata.sql RUN cp metadata.sql /usr/ #RUN mysql -- this gives error #RUN create database test; -- this gives error #Get the Web Application from Nexus RUN wget "http://mynexus:8081/nexus/service/local/artifact/maven/redirect?g=org.my&a=my-app&r=repo&e=war&v=LATEST" --content-disposition -O app.war #Copy the war file RUN cp app.war /opt/tomcat/webapps/ EXPOSE 8080 CMD ["catalina.sh", "run"] 

没有mySql相关的项目(创build数据库等)dockerbuild设工程,它运行良好。 但是我无法理解如何使用我的模式和元数据sql文件来创build数据库。

您可以在容器启动后运行SQL命令,但不能在生成过程中运行。 一种select是覆盖入口点并在那里进行。 另一个select是先build立一个docker-compose,然后创build一个普通的mysql容器,然后用另外一个运行bash脚本的容器创builddb和schema。 请参阅ie以了解它。 另一个select是传递与SQL设置相关的东西,如上面链接中的一个答案所示。