Node.js Application in Docker Cannot Connect to MySQL Container: ETIMEDOUT Error

I’m trying to connect a Node.js application (using Sequelize and MySQL) to a MySQL container via Docker. The MySQL container is running, but my application is unable to establish a connection to the database. The error I’m receiving is ETIMEDOUT when attempting to connect.

Details:

  • My Node.js application is running inside a Docker container.
  • The MySQL container is set up using Docker Compose and is accessible (I can see that it’s running).
  • The database container is named db, and the database is blog_db with the root password pHIdianARB.
  • When I try to test the database connection using a simple Node.js script (mysql2), the connection times out.
  • I’ve confirmed that the MySQL container is up and running, and I can connect to it manually from inside the application container using the mysql client.
  • I’ve checked my docker-compose.yml and .env files, and they seem to be correct. The application service depends on the database, and the network configuration appears fine.

Error Message:

arduino

Copiar código

Error: connect ETIMEDOUT
    at Connection._handleTimeoutError
    at listOnTimeout
    at process.processTimers

Steps Taken:

  1. Verified the docker-compose.yml file to ensure proper network setup and dependencies.
  2. Tried connecting to MySQL from inside the container with the mysql client and succeeded.
  3. Checked MySQL logs for any issues.
  4. Ensured correct environment variables (DB_HOST=db, DB_USER=root, etc.) are set.

Question:

What could be causing the ETIMEDOUT error when attempting to connect from my Node.js application container to the MySQL container, and how can I resolve it?

Share your compose file.

To extend @bluepuma77’s reply, we know nothing about the docker images you used or the app itself. If the connection works from an interactive terminal in the same container, the parameters are not passed correctly or the app ignores those parameters.

Sharing the compose file could help, but if there is something wrong with your application that is another story. You can test it by changing the code and using static parameters just until you figure out how to connect properly and then you can work on passing the right parameters from a compose file and .env file..