gpt4 book ai didi

ruby - Docker 容器发送空响应

转载 作者:IT老高 更新时间:2023-10-28 21:35:35 26 4
gpt4 key购买 nike

希望有人能告诉我我错过了什么?这是一个使用 webrick 的 ruby​​ 应用程序,我正在尝试将应用程序容器化。在 Mac OSX 10.12.3 Sierra 上运行。这是我的 Dockerfile

FROM ruby:2.4.0-alpine
RUN apk add --no-cache gcc musl-dev libstdc++ g++ make
RUN gem install jekyll bundler redcarpet
RUN mkdir -p /usr/app/jekyll
COPY . /usr/app/jekyll
WORKDIR /usr/app/jekyll
EXPOSE 4000:4000
CMD ["jekyll", "serve"]

这是构建图像的方式

docker build -t chb0docker/cheat .

如果我直接在主机上运行服务,它运行良好

Violas-MacBook-Pro:progfun-wiki cbongiorno$ jekyll serve &
[1] 49286
Violas-MacBook-Pro:progfun-wiki cbongiorno$ Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
Source: /Users/cbongiorno/development/progfun-wiki
Destination: /Users/cbongiorno/development/progfun-wiki/_site
Incremental build: disabled. Enable with --incremental
Generating...
done in 0.409 seconds.
Auto-regeneration: enabled for '/Users/cbongiorno/development/progfun-wiki'
Configuration file: /Users/cbongiorno/development/progfun-wiki/_config.yml
Server address: http://127.0.0.1:4000/
Server running... press ctrl-c to stop.

验证服务器是否已启动:

Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl localhost:4000 2> /dev/null | wc -l
40

现在在 docker 中运行它:

Violas-MacBook-Pro:progfun-wiki cbongiorno$ fg
jekyll serve
^C
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker run -d --name jekyll -p 4000:4000 chb0docker/cheat
e766e4acb007033583885b1a3c52dc3c2dc51c6929c8466f3a4ff958f76ebc5f

验证过程

Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e766e4acb007 chb0docker/cheat "jekyll serve" 19 minutes ago Up 32 seconds 0.0.0.0:4000->4000/tcp jekyll
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' jekyll
172.17.0.3
Violas-MacBook-Pro:progfun-wiki cbongiorno$ docker logs jekyll
Configuration file: /usr/app/jekyll/_config.yml
Configuration file: /usr/app/jekyll/_config.yml
Source: /usr/app/jekyll
Destination: /usr/app/jekyll/_site
Incremental build: disabled. Enable with --incremental
Generating...
done in 0.329 seconds.
Auto-regeneration: enabled for '/usr/app/jekyll'
Configuration file: /usr/app/jekyll/_config.yml
Server address: http://127.0.0.1:4000/
Server running... press ctrl-c to stop.

现在尝试获取一些数据

Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl localhost:4000 2> /dev/null | wc -l
0
Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl 172.17.0.3:4000 2> /dev/null | wc -l
0
Violas-MacBook-Pro:progfun-wiki cbongiorno$ curl 0.0.0.0:4000 2> /dev/null | wc -l
0

但是,如果我们执行上面的 GET(使用 wget 而不是 curl 因为它没有安装在这个容器上)我们可以看到容器内一切正常

 docker exec -it jekyll /usr/bin/wget -q -O - localhost:4000 | wc -l
40

这是应用程序问题吗?

最佳答案

看起来 Jekyll 正在绑定(bind)到 localhost。像这样启动容器:

docker run -d --name jekyll -p 127.0.0.1:4000:4000

或者让 Jekyll 绑定(bind)到 0.0.0.0:

CMD ["jekyll", "serve", "--host", "0.0.0.0"]

关于ruby - Docker 容器发送空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41964172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com