Docker Community Forums

Share and learn in the Docker community.

Initializing MySQL DB Fails


(Sdkeithk) #1

I’m building an image from this Dockerfile:

FROM mysql:5.6
ENV MYSQL_ROOT_PASSWORD=root1234
COPY init_db.sh /tmp/init_db.sh
COPY latest.sql /tmp/latest.sql
RUN chmod +x /tmp/init_db.sh
RUN /bin/bash -c /tmp/init_db.sh

With this init script:

#!/bin/bash
service mysql start
mysql -uroot -proot1234 -e "CREATE DATABASE my_db;"
mysql my_db < /tmp/latest.sql
service mysql stop

The build seems to go OK, but when I start up a container, the DB changes the script makes aren’t available.

It’s probably something simple. Any ideas? Thanks.


(Sam) #2

don’t u need to login again to execute the script? (when the 1st mysql command ended u were logged out)

mysql -uroot -proot1234 my_db < /tmp/latest.sql


(Sdkeithk) #3

Right you are. That’s actually in the script I’m using. The script runs without error. But when I start a container using the resulting image, the database in the container hasn’t been initialized. ???


(Sam) #4

run happens at build time, I suspect u want to change those to CMD to execute at run time.

CMD chmod +x /tmp/init_db.sh
CMD /bin/bash -c /tmp/init_db.sh

note that cmd will happen on EVERY Docker run, so hopefully its not a shared database

I just had to fix one of my images the same way