0

Following case:

I want to build with docker-compose two containers. One is MySQL, the other is a .war File executed with springboot that is dependend on MySQL and needs a working db. After I build the mysql container, I want to fill the db with my mysqldump file, before the other container is built.

My first idea was to have it in my mysql Dockerfile as

    #RUN mysql -u root -p"$MYSQL_ROOT_PASSWORD"' < /appsb.sql

but of course it wants to execute it while building.

I have no idea how to do it in the docker-compose file as Command, maybe that would work. Or do I need to build a script?

docker-compose.yml

version: "3"  
services:  
  mysqldb:  
    networks:  
      - appsb-mysql  
    environment:  
      - MYSQL_ROOT_PASSWORD=rootpw  
      - MYSQL_DATABASE=appsb  
    build: ./mysql  


  app-sb:  
    image: openjdk:8-jdk-alpine  
    build: ./app-sb/  
    ports:  
      - "8080:8080"  
    networks:  
     - appsb-mysql  
    depends_on:  
    - mysqldb  

networks:
- appsb-mysql:

Dockerfile for mysqldb:

FROM mysql:5.7
COPY target/appsb.sql /
#RUN mysql -u root -p"$MYSQL_ROOT_PASSWORD"' < /appsb.sql

Dockerfile for the other springboot appsb:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/appsb.war /
RUN java -jar /appsb.war

  • 2
    You could use official `mysql` image and copy your init script in `/docker-entrypoint-initdb.d` which would be automatically executed on MySQL startup – Pierre B. Jul 10 '19 at 11:57
  • Thanks for your help, Pierre! After adding and running the script in the container, I could insert the db in it. – Stardust_000 Jul 10 '19 at 13:01

1 Answers1

0

Here is a similar issue (loading a dump.sql at start up) for a MySQL container: Setting up MySQL and importing dump within Dockerfile.

Option 1: import via a command in Dockerfile.

Option 2: exec. a bash script from docker-compose.yml

Option 3: exec. an import command from docker-compose.yml

David J Eddy
  • 1,999
  • 1
  • 19
  • 37
  • 1
    Thanks, that option with creating a script and adding it to the container after it was created worked. Now I have the problem with the connection between db and app container (so jdbc and flyway), but that is another story! – Stardust_000 Jul 10 '19 at 13:00