0

I am creating a docker image with tomcat and mySql. I have a .war file that I can push to the Tomcat and the docker image is working as expected.

But the application also need a database on mySQL in the same docker image (as I do not want to run multiple images as this is fairly small and is for a demonstration only).

I am using a tomcat image as base and install mySql on it. The base OS is Ubuntu.

Here is my 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"]

Without the mySql related items (create database etc) the docker build works and it runs well. But I am not able to understand how to create the database using my schema and metadata sql files.

Kangkan
  • 15,267
  • 10
  • 70
  • 113
  • [this](https://stackoverflow.com/questions/29145370/how-can-i-initialize-a-mysql-database-with-schema-in-a-docker-container) is a similar question, that has quite a comprehensive response. I think it might offer a solution that is of use to you. – Adam Slack Sep 18 '18 at 11:10

1 Answers1

2

You can run the SQL commands after container start up but not during you build it. One option would be to override the entrypoint and do it there. Another option would be to have a docker-compose first bringing up the plain mysql container and after that create db and schema with an additional container that runs the bash script. See i.e. here to get an idea about it. Another option is to pass the SQL related stuff as ENV setting as depicted in one of the answers in above link.

Community
  • 1
  • 1
hecko84
  • 1,224
  • 1
  • 16
  • 29