Search code examples
mysqldocker-composedockerfilecontainers

Why can´t my Docker mysql Container communicate with my nodejs backend Container?


I cannot figure out why my mysql container cannot reach port 4306 of my nodejs backend container. Error:Connection refused on the mysql Container

The Ports should be mapped to the right ones on the localhost. Docker-compose-file

and the backend mysql database should connect to the right port on the container. Mysql-connection-in-backend.

Frontend-Dockerfile

Backend-Dockerfile

I am pretty new to Docker and the whole container and images stuff. I can reach the database on phpmyadmin on Port http://localhost:8098/ as I have mapped this port to Port 80 of the container. Also I can reach my frontend on port 3000(8080) and my backend on 3001(3001). I also tried to open the Port on the running mysql container with however even as root user of this container I don´t have permission. Here are also my frontend and backend Dockerfiles.

As I am not running any frontend-framework I installed http-server npm-package to host the frontend. It worked for a while, I dont know why, but now I just get the connection refused error. What am I doing wrong?


Solution

  • You connect to localhost:4306, which is your backend container, not to the database container db:4306. If you change it, you should be able to access your db