Ekaterina
Ekaterina

Reputation: 1892

Can't connect to Spring Boot app running on Docker locally

I stuck with the problem that can't open my REST Spring Boot app on localhost:8091 in browser.
Here is my docker-compose.xml (everything is deployed locally on Docker Desktop):

version: '3.3'

services:
  postgres:
    build:
      context: services/postgres
      dockerfile: Dockerfile.development
    command: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRESS_USER=postgres
      - POSTGRESS_DB=postgres
      - POSTGRESS_PASSWORD=qqq
      - POSTGRES_HOST_AUTH_METHOD=trust
    volumes:
      - "db-data:/var/lib/postgresql/data"
  
  
  app:
    build:
      context: services/app
      dockerfile: Dockerfile.development
    command: java -jar ./app.jar
    environment:
      - PORT=8091
    network_mode: host  
    image: 'my-java-app'
    ports:
      - 8091:8091
    depends_on:
      - postgres

  angular:
    build:
      context: services/angularfrontend
      dockerfile: Dockerfile.development
    image: 'my-angular-app'
    ports:
        - 80:80   
        
volumes:
   db-data:  

Spring Boot App starts normally on 8091 and connects to the database, but then I can't make calls to it's API from my local machine ("connection refused"). Angular app opens normally (on localhost:80), but can't make calls to localhost:8091 Spring Boot app. enter image description here enter image description here

Upvotes: 1

Views: 5056

Answers (2)

Catalin Cimpoeru
Catalin Cimpoeru

Reputation: 41

The call from angular service container to localhost:8091 fails, right? Try to override in your angular frontend container the call to the backend use app:8091 (this is how the backend service is called) instead of localhost:8091. In the 'angular' container localhost doesn't translate to 'app' container.

You can't get from a container into a different container using localhost. localhost inside a container will translate to the ip of that container.

Try to make in your angular application the call to the backend configurable, after that override that configuration in docker-compose using environment. Also do that for the springboot app application. I don't see in the environment that you override the call to the postgress. Expose that configuration in application.properties and override in docker-compose, after that remove network_mode: host

Upvotes: 1

Neo Anderson
Neo Anderson

Reputation: 6382

If you really want to use network_mode: host, you don't need to specify <source>:<dest> because the app is listening on 8091 directly on the host network:

...
app:
    build:
      context: services/app
      dockerfile: Dockerfile.development
    command: java -jar ./app.jar
    environment:
      - PORT=8091
    network_mode: host  
    image: 'my-java-app'
    depends_on:
      - postgres
...

If you want to run the java app like the other containers, simply remove this line from the compose file and the network mode will default to bridge:

network_mode: host

Upvotes: 0

Related Questions