我正在使用 tomcat 和 mySql 创建一个 docker 镜像。我有一个可以推送到 Tomcat 的 .war 文件,并且 docker 镜像按预期工作。
但该应用程序还需要在同一个 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 相关项目(创建数据库等),docker build 可以正常运行并且运行良好。但是我无法理解如何使用我的模式和元数据 sql 文件创建数据库。
最佳答案
您可以在容器启动后运行 SQL 命令,但不能在构建期间运行。一种选择是覆盖入口点并在那里进行。另一种选择是让 docker-compose 首先启动普通的 mysql 容器,然后使用运行 bash 脚本的附加容器创建数据库和模式。 参见即 here了解一下。另一种选择是将与 SQL 相关的内容作为 ENV 设置传递,如上面链接中的一个答案中所述。
关于mysql - 在 docker 中为 mySQL 创建数据库和模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039305/