Riz
Riz

Reputation: 6686

can't connect to docker container localhost connection refused

very new to docker. following this tutorial: https://medium.com/thecodefountain/develop-a-spring-boot-and-mysql-application-and-run-in-docker-end-to-end-15b7cdf3a2ba

I followed all the instructions (my application is called accessing-data-mysql) but I think I should have two containers running: one for mysql and one for the application. But when running docker container ls I only see the mysql container listed. Below I am creating a docker container for my application's image and linking it to the running instance of mysql container.

PS C:\projects\project1> docker run -d -p 8089:8089 --name accessing-data-mysql --link mysql-standalone:mysql accessing-data-mysql
82f499c6897d1f6bd2eeaabe4aa25ae786508146929a7039785e4ca37d691435
PS C:\projects\project1> docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
62029a53b9d4        mysql               "docker-entrypoint.s…"   16 minutes ago      Up 16 minutes       3306/tcp, 33060/tcp   mysql-standalone
PS C:\projects\project1> docker run -d -p 8089:8089 --name accessing-data-mysql --link mysql-standalone:mysql accessing-data-mysql

my docker file:

FROM openjdk:12
ADD target/user-mysql.jar user-mysql.jar
EXPOSE 8089
ENTRYPOINT ["java", "-jar", "user-mysql.jar"]

when connecting via browser to localhost:8089, I get connection refused error. Not even sure if the service is running.

below is the result of running docker logs:

PS C:\projects\project1> docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                      PORTS                 NAMES
82f499c6897d        accessing-data-mysql   "java -jar accessing…"   50 minutes ago      Exited (0) 50 minutes ago                         accessing-data-mysql
62029a53b9d4        mysql                  "docker-entrypoint.s…"   About an hour ago   Up About an hour            3306/tcp, 33060/tcp   mysql-standalone
PS C:\projects\project1> docker logs accessing-data-mysql
Hibernate ORM core version 5.4.27.Final
PS C:\projects\project1>

EDIT: When I run locally directly from Idea, I see the error: No such host is known (mysql-standalone), which is the mysql url I configured to connect to docker mysql. As soon as I change the mysql url to localhost:3306, it can connect. Does this mean that somehow the my-sql docker instance is not accepting connections?

Upvotes: 1

Views: 2755

Answers (1)

jordanvrtanoski
jordanvrtanoski

Reputation: 5557

Your container is not up and running. You need to see the container as up when you do docker ps. You should see something like this:

~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
62029a53b9d4        mysql               "docker-entrypoint.s…"   16 minutes ago      Up 16 minutes       3306/tcp, 33060/tcp   mysql-standalone
XXXXXXXXXXXX        openjdk             "java"                   XX minutes ago      Up xx minutes       8089/tcp              accessing-data-mysql

If you are not able to see the second container, it means it's not running. You will need to find the reason for not running by getting the logs with docker logs accessing-data-mysql and see why the second container is not starting.

Also, consider creating a docker-compose for both containers and establish a separate network. This is not required, your example can work without this, but it makes the things much easier for management and also for troubleshooting.

Upvotes: 1

Related Questions