docker - 容器无法连接

标签 docker port docker-container

我的docker容器出现问题,其中一个是REST API服务器,另一个只是用来压缩前一个的测试容器。

我创建了一个具有特定IP地址范围的自定义docker网络,并且两个容器都已连接到该网络。

在Dockerfile(REST API服务器镜像)中,我公开了端口8090,并在运行容器时使用-p标志将其映射到主机端口8090。我确实检查了REST容器是否通过netstat -a暴露了端口,但事实并非如此。我不确定是什么问题,因为我看不到自己在做什么错!

用于REST API服务器的Dockerfile

FROM openjdk:8-alpine
WORKDIR /
EXPOSE 8090
COPY target/blockchain-0.0.1-SNAPSHOT.jar blockchain.jar
CMD java -jar blockchain.jar

REST容器创建
docker container run --rm -p 8090:8090 --ip 172.18.0.3 --net multichain-network --name multichain-app bc59

测试容器以运行Curl和响应
docker container run --rm -it --net multichain-network --ip 172.18.0.4 byrnedo/alpine-curl http://172.18.0.3:8090/api/blockchain/address/create
curl: (7) Failed to connect to 172.18.0.3 port 8090: Connection refused

Netstat -a响应
$ docker exec multichain-app netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:http-alt        0.0.0.0:*               LISTEN      
tcp        0      0 localhost:41183         0.0.0.0:*               LISTEN      
udp    34560      0 localhost:34462         0.0.0.0:*                           
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         STREAM     CONNECTED     456199

我不明白为什么端口8090没有显示在netstat命令中,因为我在Dockerfile中定义了端口,并且还指定了-p标志。有人可以帮我一下吗?

最佳答案

您必须将主机上的8090端口映射到容器上的8080端口,如下所示:

docker container run --rm -p 8090:8080 --ip 172.18.0.3 --net multichain-network --name multichain-app bc59

区别在这里:-p 8090:8080
the Docker documentation:

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

关于docker - 容器无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52119614/

相关文章:

docker - 当我同时使用主机卷(绑定(bind)挂载)和命名卷(一种 docker 托管卷)时发生了什么?

amazon-web-services - Docker 容器似乎是 'inherit' 主机 ec2 的实例配置文件。如何?

java - 'a'时间一个端口可以处理多少个请求

python - 一个命令可以杀死使用特定端口的进程

docker - 装入数据卷后,Riak容器无法启动

docker - 带有 cloudflare SSL 的 jwilder/nginx-proxy 没有

python - 在 Docker 中运行的 uwsgi 找不到初始化文件

java - 客户端无法从另一个IP连接

linux - 出现未知错误 : Chrome failed to start: exited abnormally.(未知错误:DevToolsActivePort 文件不存在)

Docker 上的 Wordpress : Could not create directory on mounted volume