Why mysql backup is not restored into my docker container ?

I want to dockerize mysql database. I have .sh script for preparing environment, creating database, user and other things. Mysql dump restore command not working in my .sh script, but working good if i open container shell and exec command in it. I want working command in my .sh script.

Whats wrong in my script ?

  • Separation of 2 docker containers running the same web application
  • Accessing documentDB on host in docker contains
  • Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer
  • Share directory or volume with container from host
  • How can I use docker for drupal?
  • How can I run a complex docker command from host machine that runs in the docker host?
  • Dockerfile:

    FROM alpine:latest
    
    WORKDIR /app
    COPY startup.sh /startup.sh
    
    COPY backup.sql /app/
    
    RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/*
    COPY my.cnf /etc/mysql/my.cnf
    
    EXPOSE 3306
    

    startup.sh :

    #!/bin/sh
    
    if [ -d /app/mysql ]; then
      echo "[i] MySQL directory already present, skipping creation"
    else
      echo "[i] MySQL data directory not found, creating initial DBs"
    
      mysql_install_db --user=root > /dev/null
    
      if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
        MYSQL_ROOT_PASSWORD=111111
        echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD"
      fi
    
      MYSQL_DATABASE=myDb
    
      if [ ! -d "/run/mysqld" ]; then
        mkdir -p /run/mysqld
      fi
    
      tfile=`mktemp`
      if [ ! -f "$tfile" ]; then
          return 1
      fi
    
      cat << EOF > $tfile
    EOF
    
      if [ "$MYSQL_DATABASE" != "" ]; then
        echo "[i] Creating database: $MYSQL_DATABASE"
        echo "FLUSH PRIVILEGES;" >> $tfile
        echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
        echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;"
    
        echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
        echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
    
        echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
        echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
    
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" >> $tfile
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;"
    
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" >> $tfile
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;"
    
    
        echo "FLUSH PRIVILEGES;" >> $tfile
      fi
    
      /usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile
      rm -f $tfile
    fi
    
    
    
    exec /usr/bin/mysqld --user=root --console
    
    mysql -u root myDb < backup.sql # THIS LINE NOT WORKING! WHY ?
    

  • java.util.regex.PatternSyntaxException when using sbt docker:publishLocal
  • No /var/run/docker.sock on OS X
  • Travis Multi-Architecture Buiilds
  • Jenkins Dockerfile - unable to prepare context: unable to evaluate symlinks in Dockerfile
  • PhantomJS server running in Docker container
  • Why can't redis access the disk in docker-compose?
  • 2 Solutions collect form web for “Why mysql backup is not restored into my docker container ?”

    You’re not actually executing your script when the container starts. I’m not certain what you’re trying to accomplish with this, and I’m not supposed to ask for clarification, so I’ll just answer as best I can and hope this helps.

    Try something like:

    Dockerfile

    FROM alpine:latest
    
    WORKDIR /app
    COPY startup.sh /startup.sh
    
    COPY backup.sql /app/
    
    RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/*
    COPY my.cnf /etc/mysql/my.cnf 
    
    EXPOSE 3306
    
    ENTRYPOINT ["/startup.sh"]
    CMD /bin/bash -c "/usr/bin/mysqld --user=root --console && mysql -u root myDb < backup.sql"
    

    Then, you’d modify your startup script like this:

    startup.sh

    #!/bin/sh
    
    if [ -d /app/mysql ]; then
      echo "[i] MySQL directory already present, skipping creation"
    else
      echo "[i] MySQL data directory not found, creating initial DBs"
    
      mysql_install_db --user=root > /dev/null
    
      if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
        MYSQL_ROOT_PASSWORD=111111
        echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD"
      fi
    
      MYSQL_DATABASE=myDb
    
      if [ ! -d "/run/mysqld" ]; then
        mkdir -p /run/mysqld
      fi
    
      tfile=`mktemp`
      if [ ! -f "$tfile" ]; then
          return 1
      fi
    
      cat << EOF > $tfile
    EOF
    
      if [ "$MYSQL_DATABASE" != "" ]; then
        echo "[i] Creating database: $MYSQL_DATABASE"
        echo "FLUSH PRIVILEGES;" >> $tfile
        echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
        echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;"
    
        echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
        echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
    
        echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
        echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
    
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" >> $tfile
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;"
    
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" >> $tfile
        echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;"
    
    
        echo "FLUSH PRIVILEGES;" >> $tfile
      fi
    
      /usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile
      rm -f $tfile
    fi
    
    exec "$@"
    

    Take a look at the way the Dockerfile and docker-entrypoint.sh are set up in the official mysql Docker image repo.

    Try the helicopterizer for Backup and Restore for Docker Container.

    https://github.com/frekele/helicopterizer

    .

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.