Unable to connect to mysql docker container

I have a Dockerfile that I am working on that pulls Mysql 5.6 and configures it (mostly with a bash and sql script). I am able to build and run it but when I try to connect to the database in the container I always get:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I have tried accessing the mysql database by using:

  • How to make current environment as image docker in ubunu 14.04?
  • Scripts and Dockerfile
  • Cannot download Docker images behind a proxy
  • Share connection details with container and host
  • Error “No module named MySQLdb” on docker
  • Docker containers having trouble exposing ports to host on Mac
  • mysql -u root -p

    mysql -u root -h 127.0.0.1 -p

    I have tried everything I could think of and looked up articles on the internet but nothing works. Can someone tell me why? Here is my Dockerfile and bash script respectively:

    FROM mysql:5.6
    
    MAINTAINER Ryan K.
    USER root
    
    ADD mysqlAddUser.sh /tmp/
    CMD ["/tmp/mysqlAddUser.sh"]
    ADD foo.sql /docker-entrypoint-initdb.d/foo.sql
    
    EXPOSE 3306
    
    ## Starting mysqld and running Database Scripts
    CMD ["/usr/bin/mysqld_safe"]
    

    Bash script:

    #!/bin/bash
    DATABASE_PASSWORD=test
    
    /usr/bin/mysqld_safe &
    mysqladmin --login-path=local -uroot -p"$DATABASE_PASSWORD"
    mysqladmin password "$DATABASE_PASSWORD"
    
    mysql -uroot -p"$DATABASE_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASSWORD') WHERE User='root'"
    mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
    mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"
    mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
    mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES"
    
    DB_ROOT_PASS=TEST
    DB=portal
    
    mysql --login-path=local -uroot -p"$DB_ROOT_PASS"
    mysql -uroot -p"$DATABASE_PASWORD" -e "CREATE DATABASE portal";
    mysql -uroot -p"$DATABASE_PASSWORD" -e "CREATE USER portaluser@'localhost' IDENTIFIED BY 'testing'";
    mysql -uroot -p"$DATABASE_PASSWORD" -e "GRANT ALL PRIVILEGES ON portal.* TO portaluser@'localhost'";
    mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES";
    
    mysql -uroot -p"$DATABASE_PASSWORD" $DB < /tmp/foo.sql
    

  • How can i access the website running inside a Docker container using IP address of container
  • Docker + rails 4 :, weird error during : RUN bundle exec rake assets:precompile
  • Docker-Compose: how to mapping volume from host into container?
  • How can I tell if I'm logged in to a private Docker registry from a script?
  • How to test HTML pages served by docker with Windows vitrualbox guest?
  • My Docker container does not have IP address. Why?
  • One Solution collect form web for “Unable to connect to mysql docker container”

    Was struggling with the same issue with mysql in a docker container. Sometimes I could connect with the mysql client, but more often not. Switched to mariadb and had the same problem.

    What seems to have fixed it for me is to add some sleep commands in my scripts that create, start and destroy the docker containers. After commands like ‘docker run’ and ‘docker stop’ I added ‘sleep 10’ and that seems to help.

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