【发布时间】:2019-12-08 05:10:51
【问题描述】:
我在使用 Docker 时遇到了几个问题。
- 主机 (Windows 10) 无法访问容器托管的任何资源。
- 容器本身也无法看到彼此。
我的配置:
version: '3.7'
services:
chrome:
build: ./chrome
container_name: chrome_container
ports:
- "9223:9223"
dotnetapp:
depends_on:
- chrome
build: ./dotnetapp
container_name: live_container
environment:
- ASPNETCORE_ENVIRONMENT=Production
stdin_open: true
tty: true
Dockerfile 用于 chrome(它所做的一切 - 启动 Chrome 并进行调试,在端口 9223 上启用):
FROM debian:buster-slim
# preparation
RUN apt-get update; apt-get clean
RUN apt-get install -y wget
RUN apt-get install -y gnupg2
# installing xvfb
RUN apt-get install -y xvfb
# installing Chrome
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -y google-chrome-beta
EXPOSE 9223
COPY ./docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["test"]
docker-entrypoint.sh
#!/bin/bash
set -e
if [ "$1" = 'test' ]; then
rm -f /tmp/.X99-lock
fi
xvfb-run -e /dev/stdout --server-args='-screen 0, 2560x1440x16' google-chrome --window-size=2560,1440 --no-sandbox --remote-debugging-port=9223 --user-data-dir=remote-profile https://www.google.com/
Dockerfile 用于第二个应用程序(仅用于 docker 内部网络测试)
FROM mcr.microsoft.com/dotnet/core/runtime:2.2
# run app
CMD ["/bin/bash"]
所以,现在,详细说明第 1 点:
我可以通过运行:docker-compose up --build chrome 来启动 chrome 容器。
然后,在主机系统上,我尝试打开浏览器 localhost:9223 或 http://172.17.0.2:9223/,在这两种情况下,我都会收到“无法访问页面错误”。附:我从docker inspect 命令获得了IP。
另一方面,如果我尝试进入正在运行的容器docker exec -it chrome_container bash 并执行命令curl localhost:9223,那么它会显示成功结果。
此时,如果我尝试使用其他地址,例如 curl chrome:9223 或 curl http://chrome:9223 或curl chrome_container:9223 或 curl http://chrome_container:9223,那么它们也会失败。根据文档 - 内部网络中的所有容器都应为accessible by the service host name。在我的场景中,这完全是错误的。
我也尝试不依赖docker compose 像这样docker run -p 9223:9223 -it --rm all_chrome 启动图像,但结果是一样的。该资源在主机系统中不可用。
现在,详细说明问题 2。
当我像这样docker-compose up 运行这两个应用程序时。并通过docker exec -it live_container bash 登录第二个应用程序。然后尝试使用上述 URL 访问第一个容器 - 所有这些都失败(curl localhost:9223 或 curl 0.0.0.0:9223 或 curl chrome:9223 或 curl http://chrome:9223 或curl chrome_container:9223 或 curl http://chrome_container:9223)。
我尝试了几次重新启动 Docker 并尝试了不同的端口。我怎样才能弄清楚这些事情?
如何访问主机系统9223端口的资源?
为什么第二个服务不能使用主机名看到第一个服务,如文档中的here?
我在 Windows 10 上使用 Docker。
编辑:更多细节。
- 通过 localhost 访问时 - 显示以下错误:
- 通过 IP 访问时显示以下错误:
所以在主机系统(win 10)上通过 localhost 访问时似乎发生了一些事情。
【问题讨论】:
-
运行独立容器时
docker container ps -a会显示什么? -
@michalk 这是输出:
CONTAINER ID (51b90eefcf91), IMAGE (all_chrome), COMMAND ("docker-entrypoint.s…" ), CREATED ( About a minute ago), STATUS (Up About a minute), PORTS (0.0.0.0:9223->9223/tcp), NAMES (naughty_boyd)。使用命令启动:docker run -p 9223:9223 -it --rm all_chrome。更多细节。当我尝试使用localhost:9223访问资源时 - 错误是:This page isn’t working. localhost didn’t send any data. ERR_EMPTY_RESPONSE。当我使用 IP 时:This site can’t be reached 172.17.0.2 took too long to respond.. -
@michalk 我在问题底部添加了 2 个屏幕截图。请看一看。
-
@michalk 找到了解决方案!
-
很好,请考虑为您自己的问题添加答案以供将来参考。
标签: docker curl docker-compose