在Docker容器中打开端口

时间:2015-11-06 16:24:39

标签: node.js debugging docker port

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

因此,我试图向外界开放5858端口。但是,当我--link另一个容器到第一个容器(别名为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 <sclorg@redhat.com>

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的说明。

感谢。

1 个答案:

答案 0 :(得分:3)

-P自动将容器中的任何公开端口映射到主机上的随机端口,而-p允许显式映射端口。使用--link标志允许两个docker容器相互通信,但不会将端口暴露给外部世界(在docker专用网络之外)。