gpt4 book ai didi

docker - 从另一个容器在容器中运行的Curl应用程序

转载 作者:行者123 更新时间:2023-12-02 20:43:59 29 4
gpt4 key购买 nike

我用以下方法创建了一个docker网络:

docker network create --driver bridge sample-network

接下来,我在该网络上启动两个容器:
  docker run -it --network sample-network -p 8080:8080 --name frontend-container frontend-image
docker run -it --network sample-network -p 8082:8080 --name backend-container backend-image

结果:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e5328faa21db frontend-image "docker-entrypoint.s…" 33 minutes ago Up 33 minutes 0.0.0.0:8080->8080/tcp frontend-container
e13d798edbec backend-image "java -jar backend-0…" About an hour ago Up About an hour 0.0.0.0:8082->8080/tcp backend-container
backend-container在哪里运行Spring Boot Web应用程序
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)

2019-10-21 18:29:40.487 INFO 1 --- [ main] hello.Application : Starting Application v0.1.0-SNAPSHOT on e13d798edbec with PID 1 (/backend-0.1.0-SNAPSHOT.jar started by root in /)
2019-10-21 18:29:40.489 INFO 1 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2019-10-21 18:29:41.289 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-21 18:29:41.321 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-21 18:29:41.321 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-10-21 18:29:41.399 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-21 18:29:41.399 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 873 ms
2019-10-21 18:29:41.609 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-21 18:29:41.758 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-21 18:29:41.761 INFO 1 --- [ main] hello.Application : Started Application in 1.604 seconds (JVM running for 1.889)

接下来,我想从 backend-container中 curl frontend-container:
$ docker exec -it frontend-container /bin/bash
bash-4.4# curl backend-container:8082
curl: (7) Failed to connect to backend-container port 8082: Connection refused

但是为什么我的连接被拒绝了?它们都在同一网络上。

最佳答案

您的两个容器都在8080端口上运行应用程序。因此,要通过网络连接到后端应用程序,您应该使用backend-container:8080作为主机。

看来您已将8082的端口backend-container发布到您的主机-但这并不意味着您可以从另一个容器连接到此端口上的应用程序-如果您想使用backend-container从主机访问localhost:8082,则可以使用。

有关 -p 选项如何工作的信息,请引用 container networking :

By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.

关于docker - 从另一个容器在容器中运行的Curl应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493302/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com