Docker警告:使用主机网络模式时,已发布的端口将被丢弃

huangapple go评论82阅读模式
英文:

Docker WARNING: Published ports are discarded when using host network mode

问题

我有一个Springboot应用程序,我已经将其Docker化。
我将其暴露在端口8081上,可以按预期访问。

http://<ipaddress>:8081

问题

Docker容器中的Springboot应用程序需要连接到同一主机上的PostgreSQL数据库(不在容器中),但似乎它无法访问主机网络。

> 连接到 localhost:5432 被拒绝

Docker命令:

docker run -t --rm -d -p 8081:8081 --name nexct-approval-service-container nexct-approval-service-image

因此,我已经阅读了连接到网络的方法,您可以使用:

--network host

然而,这样做会停止允许访问应用程序本身(端口8081):

警告:使用主机网络模式时,发布的端口将被丢弃

问题

如何允许访问端口8081上的SpringBoot应用程序,并允许Springboot应用程序访问主机网络,以便连接到数据库?


更新

我的数据库连接在Spring Boot中定义:

application.properties

spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=

MultipleDBConfig.java

@Configuration
@ComponentScan(basePackages = "com.nexct")
public class MultipleDBConfig {

    @Bean(name = "datasource1")
    @ConfigurationProperties("spring.datasource1")
    @Primary
    public DataSource dataSource1(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "datasource2")
    @ConfigurationProperties("spring.datasource2")
    public DataSource dataSource2(){
        return DataSourceBuilder.create().build();
    }
}
英文:

I have a Springboot app that I have Dockerized.
I have it exposed on port 8081, and can access it as expected.

http://<ipaddress>:8081

Problem

The Springboot app in the docker container needs to connect to a postgres database on the same host (not in a container), but it appears like it does not gave access to the host network.

> Connection to localhost:5432 refused

Docker cmd:

docker run -t --rm -d -p 8081:8081 --name nexct-approval-service-container nexct-approval-service-image

So I have read in order to connect to the network, you can use:

--network host 

However, then it stops allowing access to the application itself (port 8081):

WARNING: Published ports are discarded when using host network mode

Question

How can I allow access to the SpringBoot app on port 8081 and allow the Springboot app access to the host network so it can connect to the database?


UPDATE

My database connection is defined in Spring Boot:

application.properties

spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=

MultipleDBConfig.java

@Configuration
@ComponentScan(basePackages = "com.nexct")
public class MultipleDBConfig {

    @Bean(name = "datasource1")
    @ConfigurationProperties("spring.datasource1")
    @Primary
    public DataSource dataSource1(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "datasource2")
    @ConfigurationProperties("spring.datasource2")
    public DataSource dataSource2(){
        return DataSourceBuilder.create().build();
    }
}

答案1

得分: 17

在我的服务中,我将对 localhost 的引用替换为 host.docker.internal,然后我能够通过 -p 发布容器的端口,并且无需使用 --network host 就能够连接到本地主机上的服务。我的服务引用了一个包含主机名的 .env 文件,因此我只需创建另一个带有更新主机名的 .env 文件。

英文:

In my service, I replaced the references to localhost with host.docker.internal, and I was able to publish the container's ports with -p and connect to services on localhost without having to use --network host. My services references a .env file that has the hostnames, so I just created another .env file with the updated hostname.

答案2

得分: 12

你不能在主机网络模式下发布端口。

>注意:当使用主机模式网络时,由于容器没有自己的IP地址端口映射不会生效,并且**-p、--publish、-P和--publish-all选项会被忽略**,而是产生一个警告:

主机网络模式

>我如何允许通过8081端口访问SpringBoot应用,并允许SpringBoot应用访问主机网络,以便连接数据库?

然而,通过将主机网络分配给容器,你仍然无法访问主机网络。要访问主机网络,你可以使用主机IP或者在Mac和Windows上使用特殊DNS。

host.docker.internal:DB_PORT

或者如果你在Linux上,你可以使用主机IP

HOST_IP:DB_PORT

或者你可以尝试以下方法(在Ubuntu上有效)

docker run -it --rm -e HOST_IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0.9.]\+\).*//p') image_name

现在在你的应用程序中使用HOST_IP作为主机名。

英文:

You can not publish port in the network mode host.

>Note: Given that the container does not have its own IP-address when using host mode networking, port-mapping does not take effect, and the -p, --publish, -P, and --publish-all option are ignored, producing a warning instead:

host networking

>How can I allow access to the SpringBoot app on port 8081 and allow the Springboot app access to the host network so it can connect to the database?

Still, you can not reach to Host network by assigning host network to the container. To reach host Network you can use Host IP or use special DNS for mac and window.

host.docker.internal:DB_PORT

Or you can use Host IP if you are on linux

HOST_IP:DB_PORT

or you can try (works on ubuntu)

docker run -it --rm -e HOST_IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*//p') image_name

Now use HOST_IP as a host name in your application.

答案3

得分: 5

你应该将连接字符串中的 localhost 更改为:

172.17.0.1,这是容器网络的 IP 地址。

然后再次检查。

英文:

You should change : localhost in connection string to :

172.17.0.1 its an IP address of containers network

Then check again.

huangapple
  • 本文由 发表于 2020年7月24日 15:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定