2017-05-31 159 views
3

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

FROM ruby:2.2 

# 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 3000 to the Docker host, so we can access it 
# from the outside. 
EXPOSE 3000 

# 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"] 

然後我建立了它(沒有錯誤):

docker build -t demo . 

,然後運行它(也無錯誤):

docker run -itP demo 
=> Booting Puma 
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3000 
=> Run `rails server -h` for more startup options 
Puma starting in single mode... 
* Version 3.8.2 (ruby 2.2.7-p470), codename: Sassy Salamander 
* Min threads: 5, max threads: 5 
* Environment: development 
* Listening on tcp://0.0.0.0:9292 
Use Ctrl-C to stop 

當我在一個單獨的終端運行一個docker ps命令來確定端口,我得到:

CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS      NAMES 
55e8224f7c15  demo    "bundle exec rails..." About an hour ago Up About an hour 0.0.0.0:32772->3000/tcp ecstatic_bohr 

但是,當我嘗試使用Chrome或通過curl命令連接到http://localhost:32772http://192.168.99.100:32772時,我收到「連接被拒絕」。

當我通過bundle exec rails server命令在我的本地計算機上的泊塢窗外運行應用程序時,它工作正常。請注意,我在我的Win7機器上使用Docker Toolbox

我該做什麼錯?

+0

您公開端口3000,但您試圖達到32772? –

+0

我在容器中暴露3000,然後從我收集的內容(至少每個「0.0.0.0:32772->3000/tcp」通過「docker ps」顯示)映射到我的本地主機上的32772) – Donald

+0

我通常使用碼頭工人與我的碼頭文件一起撰寫,以確保事物以自動和一致的方式進行設置。在您的dockerfile中,您將使用「ports」選項並執行類似端口的操作:「8000:8000」left 8000是docker host,右邊是8000 docker container。 – bkunzi01

回答

0

上述招數worked--

我不得不使用http://<VM IP ADDRESS>:32772組合(本地主機:32772沒有工作),我不得不解決我暴露的端口匹配的9292

的TCP監聽端口

我仍然不明白爲什麼TCP偵聽端口默認爲9292而不是3000,但我會分開研究。

謝謝你的幫助!

1

我花了幾個小時在這個以及thread真的很有幫助。我現在正在通過虛擬機的IP地址訪問這些服務。

你可以讓你的虛擬機的運行地址:

docker-machine ls 

然後嘗試使用主機映射端口37772,這樣的事情來訪問服務:

http://<VM IP ADDRESS>:32772 

希望這有助於。

+0

試過這個 - 「docker-machine ls」給了我一個URL 「TCP://192.168.99.100:2376」。但是當我在地址欄中輸入「http://192.168.99.100:32772」時,我仍然遇到同樣的錯誤! – Donald

+1

我看到你在Dockerfile上公開端口3000,但你的服務似乎在端口9292上監聽。我認爲這些都必須匹配。 – elvin