簡體   English   中英

Docker 在使用主機網絡模式時不會在容器外部公開應用程序,但它在容器內運行良好

[英]Docker is not exposing application outside container when used host network mode however it works well within container

我有一個 spring 啟動應用程序,它在內部調用 redis。 I have redis running on 6379 inside docker container (also be accessed outside container through redis client) But When I expose the application as below it complains that redis connection failure. 在 8086 上運行

docker run -d --name containerName -p 8086:8086 containerName:version但是應用程序可以從瀏覽器訪問,為http://localhost:8086但是,當像下面這樣運行 --net=host 時,它工作得非常好,警告為警告:使用主機網絡模式時丟棄已發布的端口

docker 運行 --rm -it -d --net=host --publish-all=true --name containerName containerName:version

但是無法從瀏覽器http://localhost:8086訪問應用程序

異常堆棧跟蹤

線程“pool-2-thread-1”org.springframework.data.redis.RedisConnectionFailureException 中的異常:無法獲得 Jedis 連接; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:282) at org.springframework.data .redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:476) at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:134)

原因:java.net.ConnectException:在 java.base/java.net.PlainSocketImpl.socketConnect(Native Method) 處拒絕連接(拒絕連接

注意:我在 windows 機器上運行 docker 容器

我有 redis 和應用程序容器單獨運行所以這里是解決方案

1: redis is running on 6379 docker run --rm -it --name redis -p 6379:6379 redis

2: My service which is using redis under the hood needs to configure redis host as spring.redis.host=redis Here redis is nothing but the name of redis container

docker 運行 --rm -d --link redis --name reader -p 8086:8086 imageName:version

我可以從瀏覽器http://localhost:8086訪問應用程序 --link redis 映射 redis 主機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM