Takuya.N
Takuya.N

Reputation: 153

Cannot access to spring boot application running with docker-compose

I have a simple application composed by spring boot and mariadb. OS is mac OS, and using IntelliJ as IDE. Dockerfile for spring app is below;

FROM openjdk:14-jdk-alpine

RUN mkdir /app
WORKDIR /app
VOLUME /app

ENV JAR_TARGET "my_app-0.0.1-SNAPSHOT.jar"
ENTRYPOINT ["sh","-c","java -jar ./build/libs/${JAR_TARGET}"]

And docker-compose.yml which refers Dockerfile above is like;

version: "3.3"
services:
  app:
    build: docker/.
    ports:
      - "8080:8080"
    volumes:
      - .:/app
    depends_on:
      - db
    environment:
      spring.datasource.driverClassName: "org.mariadb.jdbc.Driver"
      spring.datasource.url: "jdbc:mariadb://db:3306/my_app_db"
      spring.datasource.username: "user"
      spring.datasource.password: "password"
      server.port: 8080
      server.address: 127.0.0.1

  db:
    image: mariadb
    ports:
      - "3306:3306"
    environment:
      - MYSQL_DATABASE=my_app_db
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=rootpassword,

By typing the command docker-compose up --build into terminal, docker-compose starts runnning and spring boot app "my_app" also seems to be running with the log ... Started MyAppApplicationKt in 25.161 seconds (JVM running for 27.301).

However, when I try to access that spring app by calling http://localhost:8080/api, it is not responding. For example, when I do curl with the address, it is timed out every time. I checked with IP 127.0.0.1 too, but it resulted in the same; not responding.

When I see docker container ps, both spring app and mariadb look like successfully running as below.

CONTAINER ID        IMAGE          COMMAND                  CREATED             STATUS              PORTS                    NAMES
b6abb2d330d9        my_app        "sh -c 'java -jar -D…"   5 minutes ago       Up 5 minutes        0.0.0.0:8080->8080/tcp   my_app_1
fb3b3cad20ad        mariadb       "docker-entrypoint.s…"   17 minutes ago      Up 5 minutes        0.0.0.0:3306->3306/tcp   my_app_db_1

I cannot understand why this spring app is not responding when I reach http://localhost:8080/api. Any help would be really appreciated. Thank you for reading!

Upvotes: 1

Views: 2260

Answers (1)

Takuya.N
Takuya.N

Reputation: 153

David Maze's comment did solve my problem. The docker container was not listening on 127.0.0.1 because I set inside process address on 127.0.0.1. I changed server.address in docker-compose.yml to 0.0.0.0, and it started listening on 127.0.0.1 for outside access. Thank you so much for all comments, especially David who saved me!

Upvotes: 2

Related Questions