2015-11-06 101 views
1

我试图在一个Docker容器中以调试模式运行node.js应用程序,并将另一个容器中的调试器附加到在第一个容器中运行的应用程序中。在Docker容器中打开端口

因此,我试图打开端口5858到外面的世界。但是,当我将第一个容器的另一个容器(别名为firstContainer),并运行nmap -p 5858 firstContainer,我发现端口5858已关闭。第一个容器告诉我,node.js应用程序正在侦听端口5858,我已经暴露了Dockerfile中的端口,并且还将端口绑定到我机器上的相应端口(尽管我不是确定这是必要的)。当我在端口8080上运行nmap时,一切都成功了。

如何打开Docker容器上的端口5858,以便我可以将调试器连接到此端口?

的Dockerfile是:

FROM openshift/base-centos7 

# This image provides a Node.JS environment you can use to run your Node.JS 
# applications. 

MAINTAINER SoftwareCollections.org <[email protected]> 

EXPOSE 8080 5858 

ENV NODEJS_VERSION 0.10 

LABEL io.k8s.description="Platform for building and running Node.js 0.10 applications" \ 
     io.k8s.display-name="Node.js 0.10" \ 
     io.openshift.expose-services="8080:http" \ 
     io.openshift.tags="builder,nodejs,nodejs010" 

RUN yum install -y \ 
    https://www.softwarecollections.org/en/scls/rhscl/v8314/epel-7-x86_64/download/rhscl-v8314-epel-7-x86_64.noarch.rpm \ 
    https://www.softwarecollections.org/en/scls/rhscl/nodejs010/epel-7-x86_64/download/rhscl-nodejs010-epel-7-x86_64.noarch.rpm && \ 
    yum install -y --setopt=tsflags=nodocs nodejs010 && \ 
    yum clean all -y 

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH 
COPY ./s2i/bin/ $STI_SCRIPTS_PATH 

# Each language image can have 'contrib' a directory with extra files needed to 
# run and build the applications. 
COPY ./contrib/ /opt/app-root 

# Drop the root user and make the content of /opt/app-root owned by user 1001 
RUN chown -R 1001:0 /opt/app-root 
USER 1001 

# Set the default CMD to print the usage of the language image 
CMD $STI_SCRIPTS_PATH/usage 

运行带:

docker run -P -p 5858:5858 -p 8080:8080 --name=firstContainer nodejs-sample-app 

从/采取从here说明构建。

谢谢。

+0

如果你刚刚分享了你的'Dockerfile',那会更有帮助,因为现在我们所有的都是你的描述,就像导航盲人一样。既然你给了我们一点点继续,尝试:你似乎使用桥模式;执行'docker run -P ...'(注意大写),然后用'docker ps'查看你的容器端口'5858'已映射到哪个主机端口。我们从那里拿... –

+0

@MichaelHausenblas我的歉意,我会更新描述。 – Citronen

回答

3

-P自动将容器内的任何暴露端口映射到主机上的随机端口,而-p允许显式映射端口。使用--link标志允许两个码头集装箱相互通信,但是没有做任何事情将端口暴露给外部世界(在码头专用网络之外)。

+0

如果我运行命令'docker run -it -name sandbox -h sandbox -link firstContainer:firstContainer linuxconfig/sandbox/bin/bash ',然后运行'nmap -p 5858 firstContainer'命令,我发现港口仍然关闭。这可能与防火墙有关吗? – Citronen

+0

我不认为你应该将一个容器链接到它自己,而且为了使'-P'标志正常工作,端口必须通过Dockerfile中的EXPOSE PORT命令暴露在容器中。防火墙不应该通过'docker ps'影响docker列表。 – carter

+0

我很抱歉,我没有将容器链接到自身,我正在链接第二个运行shell的容器。我公开了我列出的Dockerfile中的端口。不确定为什么'5858'端口被关闭。 – Citronen

相关问题