2016-04-23 116 views
17

我是新來的Docker並試圖製作一個演示Rails應用程序。我做了一個dockerfile,看起來像這樣:碼頭集裝箱拒絕連接

FROM ruby:2.2 
MAINTAINER [email protected] 

# Install apt based dependencies required to run Rails as 
# well as RubyGems. As the Ruby image itself is based on a 
# Debian image, we use apt-get to install those. 
RUN apt-get update && apt-get install -y \ 
build-essential \ 
nodejs 

    # Configure the main working directory. This is the base 
    # directory used in any further RUN, COPY, and ENTRYPOINT 
    # commands. 
RUN mkdir -p /app 
WORKDIR /app 

    # Copy the Gemfile as well as the Gemfile.lock and install 
    # the RubyGems. This is a separate step so the dependencies 
    # will be cached unless changes to one of those two files 
    # are made. 
COPY Gemfile Gemfile.lock ./ 
RUN gem install bundler && bundle install --jobs 20 --retry 5 

# Copy the main application. 
COPY . ./ 

# Expose port 8080 to the Docker host, so we can access it 
# from the outside. 
EXPOSE 8080 

# The main command to run when the container starts. Also 
# tell the Rails dev server to bind to all interfaces by 
# default. 
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "8080"] 

我再建造它,像這樣:

docker build -t demo . 

,並調用命令來啓動它確實在服務器上啓動8080端口的服務器:

Johns-MacBook-Pro:demo johnkealy$ docker run -it demo 
=> Booting WEBrick 
=> Rails 4.2.5 application starting in development on http://0.0.0.0:8080 
=> Run `rails server -h` for more startup options 
=> Ctrl-C to shutdown server 
[2016-04-23 16:50:34] INFO WEBrick 1.3.1 
[2016-04-23 16:50:34] INFO ruby 2.2.4 (2015-12-16) [x86_64-linux] 
[2016-04-23 16:50:34] INFO WEBrick::HTTPServer#start: pid=1 port=8080 

然後我試圖找到正確的IP定位到:

Johns-MacBook-Pro:demo johnkealy$ docker-machine ip default 
192.168.99.100 

我導航到http://192.168.99.100:8080並得到錯誤本網站無法訪問192.168.99.100拒絕連接。

我會做什麼錯?

+0

嘗試使用發佈選項運行容器。 docker run -it demo --publish 8080:8080 –

+0

thanks,@jozef,但是我得到了「docker:來自守護進程的錯誤響應:容器命令'--publish'找不到或不存在..」 – jdkealy

回答

18

您需要使用以下選項來發布暴露的端口:

-P(大寫)或--publish-所有會告訴碼頭工人從您的主機使用隨機端口,並將它們映射到暴露的容器的端口。

-p(小寫)或--publish = []這將告訴Docker使用您手動設置的端口並將它們映射到外露容器的端口。

第二個選項是首選,因爲您已經知道映射了哪些端口。如果您使用第一個選項,則需要撥打docker inspect demo並檢查您的主機在端口部分使用哪些隨機端口。

只要運行以下命令:

docker run -it -p 8080:8080 demo 

之後,您的網址將工作。

9

如果您正在使用泊塢窗工具窗口10家您將需要通過泊塢窗機ip命令來訪問網頁。它通常是192.168.99.100:

假定您正在使用如下所示的發佈命令運行。

docker run -it -p 8080:8080 demo 

使用Window 10專業版可以訪問本地主機或相應的環回127.0.0.1:8080等(Tomcat或任何你想要的)。這是因爲您沒有虛擬框,並且Docker直接在Window Hyper V上運行,並且可以直接訪問環回。

驗證窗口中的hosts文件是否有任何離題。它應該有 127.0.0.1映射到本地主機

+0

非常感謝您的先生!這爲我節省了很多時間! – zsljulius

+0

@ zsljulius很好,它幫助你。 –