Docker Community Forums

Share and learn in the Docker community.

Cannot connect to MySQL from other container

I have a MySQL container running (version 8, but tried with 5.x) where I can access the port 3307 from an external Java program (not in docker). That’s OK for a test to make sure the database is accessible in at least one case, but I need it to accessible from another docker container.

However, for whatever reason, need help with this, I cannot get my service running in one docker container to talk to the MySQL container running in docker.

My ports configuration for MySQL in docker-compose.yml is simply:

- 3307:3306

When docker loads MySQL via the docker-compose file, the last line says:

“[System] [MY-011323] [Server] X Plugin ready for connections. Socket: ‘/var/run/mysqld/mysqlx.sock’ bind-address: ‘::’ port: 33060”

the line above that, says:

“[Server] /usr/sbin/mysqld: ready for connections. Version: ‘8.0.13’ socket: ‘/var/run/mysqld/mysqld.sock’ port: 3306 MySQL Community Server - GPL.”

Yet, regardless of what ports I use in the JDBC connection string, I get:

“Could not create connection to database server. Attempted reconnect 3 times. Giving up.”

I’ve tried:

“jdbc:mysql://” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:3307/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

and other combinations.

I appear to be confused what the “bind address” is and how to properly use it. I’ve searched on Stackoverflow, but the examples there are confusing.

Would appreciate advice on how to configure mysql access from another container.

Thanks very much.

  • mike

You should have both the containers running on the same network and declare a host-name for the database. That way, docker will take care of the ip-address lookup for you.

The easiest way to do that is to use a docker-compose.yml file and start the containers as services. This is an example from another answer I posted earlier.

version: "3"
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
      MYSQL_ROOT_PASSWORD: TopSecretPassword
    image: adminer
    restart: always
      - 8088:8080  
  image: tomcat
      - "8080:8080"
      - ./target/DockerExample.war:/usr/local/tomcat/webapps/ROOT.war
      - ./target/DockerExample:/usr/local/tomcat/webapps/ROOT

This will create 3 services and if you access adminer on port 8088, you will see that it finds a MySQL-server on host db. That is the hostname you can use in your other application as well. So instead of an ip, you just use db as you would use any FQDN to a server.

Thanks for your help. Very useful.

Here’s my docker-compose file:

version: ‘3’
context: .
dockerfile: Dockerfile
- “8089:8080”
- /Users/mike/Library/apache-tomcat-9.0.7/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
- db
image: mysql
container_name: test-mysql-docker
- 3307:3306
- ./ZipCodeLookup.sql:/docker-entrypoint-initdb.d/ZipCodeLookup.sql

How would you modify it so the app can access mysql?


  • mike

PS I’m not sure “db” would resolve in a JDBC connection string.

I made a note of the internal network created on docker startup and also tried …

“jdbc:mysql://miketest_default:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:3307/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:8080/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:8089/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;


Problem was that I wasn’t actually removed cached versions of the build WAR file so none of my multiple experiments was actually using new builds. Need to use the “rmi” argument with docker down I’m told. In any case, the correct working JDBC was to use the mysql container name from the docker-compose file above and port 3306.

Hope this helps someone else.

Thanks to all.


Can you please show us how you solved your issue? for example in this url jdbc:mysql://miketest_default:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”; what is miketest_default, is it the name of network or container?


miketest_default will be the container name of mysql docker.

The --link flag is considered a legacy feature, you should use user-defined networks.

You can run both containers on the same network:

docker run -d --name php_container --network my_network my_php_image

docker run -d --name mysql_container --network my_network my_mysql_image
Every container on that network will be able to communicate with each other using the container name as hostname.