Docker Community Forums

Share and learn in the Docker community.

App container cannot access mysql container

(Arderyp) #1

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:

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

      image: mysql:5.6
        - "32795:3306"
        MYSQL_ROOT_PASSWORD: some-root-password
        MYSQL_DATABASE: some-database
        - ./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 -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

I’ve tried using “” 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 ""
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '' (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.

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

Can't connect to MySQL server using docker-composer
(Arderyp) #2

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 “” 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:

Hopefully this help someone else out.

(Florianjourda) #3

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

(Wovo) #4

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):

  build: docker-build-contexts/mysql
    - 3306
    - 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.

With Regards,

(Rickysingh) #5

MySQL was created by a Swedish company, MySQL AB, founded by David Axmark, Allan Larsson and Michael “Monty” Widenius. Original development of MySQL by Widenius and Axmark began in 1994. The first version of MySQL appeared on 23 May 1995. It was initially created for personal usage from mSQL based on the low-level language ISAM, which the creators considered too slow and inflexible. They created a new SQL interface, while keeping the same API as mSQL. By keeping the API consistent with the mSQL system, many developers were able to use MySQL instead of the (proprietarily licensed) mSQL antecedent. You can easily resolve this problem by visiting RouterLogin I think they are explaining it more clearly and step by step just follow their steps.