简体   繁体   English

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

[英]Curl application running in a container from another container

I have created a docker network with:我创建了一个 docker 网络:

docker network create --driver bridge sample-network

Next I start two containers on that 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

and the result:结果:

$ 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

Where the backend-container runs a spring boot web application backend-container运行 spring 启动 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)

Next I would like to curl the backend-container from the frontend-container :接下来我想 curl 来自frontend-containerbackend-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

But why do I get a connection refused?但为什么我的连接被拒绝? They are both on the same network.他们都在同一个网络上。

Both of your containers run applications on port 8080 .您的两个容器都在端口8080上运行应用程序。 So to connect to the backend application over your network you should use backend-container:8080 as host.因此,要通过网络连接到后端应用程序,您应该使用backend-container:8080作为主机。

It seems that you published port 8082 of backend-container to your host - but that does not mean that you can connect to app on this port from another container - it would work if you wanted to access backend-container from host using localhost:8082 .您似乎已将backend-container的端口8082发布到您的主机 - 但这并不意味着您可以从另一个容器连接到此端口上的应用程序 - 如果您想使用localhost:8082从主机访问backend-container ,它会起作用.

On how-p option works refer tocontainer networking :关于-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.要使端口可用于 Docker 之外的服务,或未连接到容器网络的 Docker 容器,请使用--publish-p标志。 This creates a firewall rule which maps a container port to a port on the Docker host.这将创建一个防火墙规则,将容器端口映射到 Docker 主机上的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM