0

I'm trying to create a mysql image with Docker but it doesn't work correctly...

My goal is to create a custom image of MYSQL from Ubuntu Trusty. The image should execute the typical script to configure the database depending of the variables passed through command line but when I run 'docker run -d -e MYSQL_ROOT=docker -e MYSQL_DATABASE=wp ... ' the script works right but when it finished, the container stop.

I tried to 'nohup /usr/sbin/mysqld &' , exec '/usr/sbin/mysqld &' but nothing, the daemon die.

My dockerfile is the following:

FROM ubuntu:trusty

ENV DEBIAN_FRONTEND noninteractive

RUN \
  apt-get update && \
  apt-get -y install mysql-server-5.6 supervisor --no-install-recommends && \
   apt-get -y clean && \
   apt-get -y autoclean && \
   rm -rf /var/lib/apt/lists/* 

RUN \
   ln -sf /dev/stderr /var/log/mysql/error.log && \
   sed -i 's/127.0.0.1/0.0.0.0/' /etc/mysql/my.cnf

COPY config.sh /

VOLUME ["/var/lib/mysql"]

EXPOSE 3306 

ENTRYPOINT ["/config.sh"]

And the script 'config.sh' :

#!/bin/bash -x

/usr/sbin/mysqld &

sleep 5

if [ $MYSQL_ROOT_PASSWORD ]
  then
     mysql -u root -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('${MYSQL_ROOT_PASSWORD}') ;"
  else
    echo 'Error al establecer la contraseƱa de root.'
    exit 1
fi

if [ $MYSQL_DATABASE ]
  then
    mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE} ;"
  else
   echo 'Error al crear la base de datos.'
fi

if [ $MYSQL_USER ] && [ $MYSQL_PASSWORD ]
  then
    mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}' ; FLUSH PRIVILEGES ;"
   elif [ $MYSQL_USER ] 
      then
        mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_USER}' ; FLUSH PRIVILEGES ;"
   else
        echo 'No se pudo crear el usuario.'
fi
TuralAsgar
  • 1,275
  • 2
  • 13
  • 26

1 Answers1

0

This is most likely because the order you do the commands. You need the mysqld daemon in foreground, and you are putting it in background.

Just add something that waits forever at the end of your script:

#!/bin/bash -x

## YOUR INIT CODE HERE
## ...
## ...

tail -f /dev/null
Robert
  • 33,429
  • 8
  • 90
  • 94