App container cannot access mysql container

Hello. New to docker and the forum. I did a lot of digging and found people with similar issues to mine, but could not come up with an solid resolution for my case. I have a rather simple setup with two containers, one for my app, and one for my mysql database. I am using Docker 1.10.2 and docker-compose 1.6.0.

My docker-compose.yml:

    symfony:
      build: symfony/
      ports:
        - "8081:8081"
      command: php -S 0.0.0.0:8081 -t web
      volumes:
        - ./symfony/trunk:/var/www
      links:
        - mysql

    mysql:
      image: mysql:5.6
      ports:
        - "32795:3306"
      environment:
        MYSQL_ROOT_PASSWORD: some-root-password
        MYSQL_DATABASE: some-database
      volumes:
        - ./symfony/data:/docker-entrypoint-initdb.d

While the symfony container has it’s own Dockerfile, the mysql container does not, it just uses the standard mysql image. I can build and bring up the containers just fine with: docker-compose up -d

The application is running and accessible on port 8081. I can even log into the mysql database from the HOST machine with: mysql -u root -psome-root-password -h 0.0.0.0 -P 32795 some-database

The .sql file I put in symfony/data/ is loading. All is peachy keen. Except, my application cannot connect to the mysql database. Also, if I initiate a shell session on the mysql container (docker run -it mysql /bin/bash) I am unable to log into the database directly with: mysql -u root -psome-root-password -h 0.0.0.0

I’ve tried using “0.0.0.0” and “localhost” with port 3306 from within the shell session and within my application’s configuration, and neither work. Both the application and shell command give the oh-so-familiar errors:

# Using "0.0.0.0"
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '0.0.0.0' (111)
# Using "localhost"
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I am guessing that I am making some very simply noob mistake, but would be very grateful for any insight the community could offer. Thanks in advance.

EDIT:
For what it’s worth, there are no suspicious warnings/errors in the docker logs output for the mysql container. Additionally, when I access the mysql container with /bin/bash, I get the errors above when trying to connect immediately. If, however, I run /etc/init.d/mysql start, the mysql daemon will spin up, and i am then able to log in via: mysql -u admin

1 Like

After much head scratching, I figured out the problem. As it turns out, I need to configure my application’s database_host parameter not to “0.0.0.0” or 'localhost", but to “mysql” (the name of my database container)! I am not sure if this is documented anywhere and i just missed it or what, but I came to the solution after reading this: https://www.reddit.com/r/docker/comments/3sbxv6/why_on_earth_isnt_my_mysql_container_working_with/

Hopefully this help someone else out.

3 Likes

Thank you so much, @arderyp!!! You helped me solve the issue that was blocking me for a month!

You need to publish your mysql port to your host. The port can be a specific one or random. If random use docker-compose port mysql 3306 to get it at runtime (you can also see it in docker-compose ps ).

Since you’re using Sequel Pro, I guess you’re using a Mac, with a VM for docker server. If you’re using docker-machine, you can get the IP address with docker-machine ip <name> .

Credentials (Standard connection in Sequel Pro):

Host: IP of your docker machine host
Username: root
Password: mypassword
Database: mydatabase
Port: your specific port or randomly attributed one

Example for random host port (avoids conflict with other projects):

mysql:
  build: docker-build-contexts/mysql
  ports:
    - 3306
  environment:
    - MYSQL_DATABASE=mydatabase
    - MYSQL_ROOT_PASSWORD=mypassword

I actually use a script to open directly in Sequel Pro from the command line. You can get it at gist(dot)github(dot)com/helderco/e9d8d072a362ad818f6a (Remove the dots with real “.”)

This is how I use it:

db-open mydatabase

It launches a new window already opened in that database in my container. Note that I assume password is root (my convention in development). It also assumes you use docker-machine. The database is optional, and you can set the other credentials manually with arguments.