我是Docker的新手并试图制作一个演示Rails应用程序.我做了一个看起来像这样的dockerfile:
FROM ruby:2.2
MAINTAINER marko@codeship.com
# 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"]
Run Code Online (Sandbox Code Playgroud)
然后我像这样构建它:
docker build -t demo .
Run Code Online (Sandbox Code Playgroud)
并调用命令启动服务器,该服务器在端口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
Run Code Online (Sandbox Code Playgroud)
然后我尝试找到正确的IP导航到:
Johns-MacBook-Pro:demo johnkealy$ docker-machine ip default
192.168.99.100
Run Code Online (Sandbox Code Playgroud)
我导航到http://192.168.99.100:8080并得到错误此站点无法达到192.168.99.100拒绝连接.
我能做错什么?
Cod*_*und 39
您需要使用以下选项发布公开的端口:
-P(大写)或--publish- all将告诉Docker使用来自主机的随机端口并将它们映射到暴露容器的端口.
-p(小写)或--publish = []将告诉Docker使用您手动设置的端口并将它们映射到公开容器的端口.
第二个选项是首选,因为您已经知道映射了哪些端口.如果您使用第一个选项,则需要docker inspect demo在" 端口"部分调用并检查主机使用的随机端口.
只需运行以下命令:
docker run -it -p 8080:8080 demo
Run Code Online (Sandbox Code Playgroud)
之后你的网址就可以了.
vim*_*hna 29
如果您在window 10 home上使用Docker工具包,则需要通过docker-machine ip命令访问该网页.一般是192.168.99.100:
假设您正在使用如下所示的发布命令运行.
docker run -it -p 8080:8080 demo
Run Code Online (Sandbox Code Playgroud)
使用Window 10 pro版本,您可以使用localhost或相应的环回127.0.0.1:8080等(Tomcat或任何您想要的)访问.这是因为您没有虚拟框,并且Docker直接在Window Hyper V上运行,并且可以直接访问环回.
验证窗口中的hosts文件是否有任何题外话.它应该有127.0.0.1映射到localhost
小智 11
我有同样的问题。我在 Windows Home 上使用 Docker Toolbox。而不是localhost我不得不使用http://192.168.99.100:8080/.
您可以使用以下命令获取正确的 IP 地址:
docker-machine ip
Run Code Online (Sandbox Code Playgroud)
上面的命令192.168.99.100为我返回。
小智 7
Dockerfile 中的命令EXPOSE允许您将容器的端口绑定到主机上的某个端口,但它不会执行任何其他操作。运行容器时,要绑定端口指定-p选项。
假设您公开了端口 5000。在运行容器时构建映像后,运行docker run -p 5000:5000 name. 这会将容器的端口 5000 绑定到您的笔记本电脑/计算机的端口 5000,并且该端口转发允许容器接收外部请求。
这应该可以做到。
小智 6
在 Docker 快速入门终端中运行以下命令:
$ docker-machine ip 192.168.99.100
Run Code Online (Sandbox Code Playgroud)