Spring Boot database connection does not work when in Docker

后端 未结 1 1342
再見小時候
再見小時候 2021-01-29 05:28

I have a SpringBoot application, that runs perfectly on Tomcat and connects to a Postgres database.

e.g. application.properties

# approval         


        
相关标签:
1条回答
  • 2021-01-29 06:20

    Solution for Mac: Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on. Reference

    Solution for Linux: Actually what you really need to do is connect your container with Postgres running on host system, so for that we need to tell Docker to set network mode to host.

      docker run -it --network=host -p 8081:8081 --name containerName 'imageName'
    

    Or using docker-compose.yml:

    To start the containers just issue following command docker-compose up

    version: '3.7'
    
    services:
    
      configurator:
        container_name: 'nameOfContainer'
        image: 'imageName'
        network_mode: "host"
        ports:
          - "8081:8081"
        restart: on-failure
    

    For further learning please follow the docs.

    0 讨论(0)
提交回复
热议问题