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

时间:2019-10-21 19:58:33

标签: docker

我用以下方法创建了一个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中的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

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

1 个答案:

答案 0 :(得分:3)

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

似乎您将8082的端口backend-container发布到了主机-但这并不意味着您可以从另一个容器连接到该端口上的应用程序-如果您要访问该端口就可以使用来自主机的backend-container使用localhost:8082

有关-p选项的工作方式,请参见container networking

  

默认情况下,创建容器时,它不会将其任何端口发布到外界。要使端口可用于Docker外部的服务或未连接到容器网络的Docker容器,请使用--publish-p标志。这将创建一个防火墙规则,该规则将容器端口映射到Docker主机上的端口。