Netstat不显示Docker公开的端口

时间:2018-11-05 18:47:33

标签: docker docker-compose dockerfile port netstat

由于某些原因,netstat没有列出docker公开的端口。如建议的here,我将EXPOSE用于端口8080和5050。但是从主机看不到它们。

Dockerfile

...

FROM openjdk:11-jre-slim

COPY --from=build /usr/src/app/api/target/track-metadata-api-*.jar /app/track-metadata-api.jar

WORKDIR /app

EXPOSE 8080 5050

CMD java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5050 -jar track-metadata-api.jar

docker ps

$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                                            NAMES
a6d3381a992d        track-metadata_track-metadata   "/bin/sh -c 'java -a…"   7 minutes ago       Up 7 minutes        0.0.0.0:5050->5050/tcp, 0.0.0.0:8080->8080/tcp   track-metadata_track-metadata_1

netstat和curl

$ sudo netstat --all | grep 8080 # returns nothing
$ curl http://localhost:8080/v1/track-metadata/filtered
[{"authorName":"AC/DC","duration":208,"id":1,"tags":"#rock","trackName":"Highway to Hell"},{"authorName":"Sum41","duration":209,"id":2,"tags":"#rock","trackName":"War"},{"authorName":"Ziggy Marley","duration":220,"id":3,"tags":"#ragge","trackName":"Beach in Hawaii"}]

Docker和Ubuntu版本

$ docker --version 
Docker version 18.06.1-ce, build e68fc7a
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.10
Release:    18.10

代号:宇宙

2 个答案:

答案 0 :(得分:3)

问题出在netstat命令中,看来我在添加-np标志之后,列出了端口。

$ sudo netstat -anp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      16341/docker-proxy

答案 1 :(得分:1)

为了公开端口,当使用-P运行映像时,必须传递docker run(将所有公开的端口发布到随机端口)标志,或者传递-p标志并指定特定端口,详细的here