2016-10-03 114 views
1

我確信我正在做一些簡單的錯誤,但是我無法在我的Mac上獲得Docker正確公開端口。下面是我在做什麼:無法訪問Docker容器外部的服務

FROM ruby:2.3.1 

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs 

RUN mkdir /myapp 
WORKDIR /myapp 
ADD Gemfile /myapp/Gemfile 
ADD Gemfile.lock /myapp/Gemfile.lock 
RUN bundle install 
ADD . /myapp 

EXPOSE 3000 

然後我打造形象:

> rails -v 
Rails 5.0.0.1 
> rails new dt6 
# ... rails creates a new app 
> cd dt6 

然後我在Rails應用程序的根目錄中添加此Dockerfile:

從頭主機上啓動並運行容器

> docker build -t dt6 . 

> docker run --rm -p 3000:3000 dt6 rails s 
=> Booting Puma 
=> Rails 5.0.0.1 application starting in development on http://localhost:3000 
=> Run `rails server -h` for more startup options 
Puma starting in single mode... 
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity 
* Min threads: 5, max threads: 5 
* Environment: development 
* Listening on tcp://localhost:3000 
Use Ctrl-C to stop 

這看起來像一個正常的鐵軌/美洲獅「我準備好開始服務」的消息。然而,從主機,我無法訪問Rails服務器:

> curl localhost:3000 
curl: (52) Empty reply from server 

我知道碼頭工人做東西有3000端口,因爲我得到一個隨機端口不同的信息:

> curl localhost:3001 
curl: (7) Failed to connect to localhost port 3001: Connection refused 

值得注意的是,導軌服務器正在運行。如果我連接到容器從裏面打導軌服務器,它的工作原理:

> docker exec -it dt6_container bash 
[email protected]:/myapp# curl localhost:3000 
<!DOCTYPE html> 
<html> 
<head> 
#... a normal html response from the rails server 

TLDR:運行在泊塢窗容器中軌服務器,當我從訪問服務器,我得到一個空響應主機。如果我希望能夠向其外部的容器中運行的Rails服務器發出http請求,那麼我做錯了什麼?

+0

在我看來,你所做的在Linux中可以正常工作,但不能在Mac或Windows中使用(或多或少)一個虛擬機模擬從https://www.viget.com/articles上的Linux – user2915097

+0

摘錄/ how-to-use-docker-on-os-x-the-missing-guide'curl $(boot2docker ip):49153'(這是一個例子) – user2915097

+1

您是使用Docker for Mac還是Docker機器/ Docker工具箱? –

回答

0

嘗試在EXPOSE 3000之後將CMD ["rails", "server", "-b", "0.0.0.0"]添加到您的泊塢窗文件中。