2015-11-06 100 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

相關問題