How to create populated MySQL Docker Image on build time

前端 未结 6 707
借酒劲吻你
借酒劲吻你 2020-12-09 03:59

I would like to create a MySQL Docker image with data already populated.

I want to create 3 layers like this:

        |---------------------|--------         


        
6条回答
  •  星月不相逢
    2020-12-09 04:17

    MegaWubs's answer is great, except for this "sleep 30" that forces you to guess initdb's execution time. To avoid this, I put a small shell script to be executed after every others in /docker-entrypoint-initdb.d :

    /docker-entrypoint-initdb.d/
      |- 01_my_data1.sql
      |- 02_my_data2.sql
      ...
      |- 99_last_processed_file.sh
    

    With 99_last_processed_file.sh :

    #!/usr/bin/env bash
    touch /tmp/server_can_shutdown.txt
    

    --

    In parallel, in Dockerfile, I run another script in replacement of Mortenn's "sleep && killall" :

    # Dockerfile
    # ...
    COPY wait_then_shutdown.sh /tmp/wait_then_shutdown.sh
    RUN /entrypoint.sh mysqld & /tmp/wait_then_shutdown.sh  # <-- 
    RUN rm /docker-entrypoint-initdb.d/*
    

    With wait_then_shutdown.sh :

    #!/usr/bin/env bash
    while [ ! -f /tmp/server_can_shutdown.txt ] # <-- created by 99_last_processed_file.sh
    do
      sleep 2
    done
    kill $(pidof mysqld)
    

    --

    And now, mysqld stops only when all other files are processed in /docker-entrypoint-initdb.d

提交回复
热议问题