docker - 使用 docker-compose 生成 Docker 容器时无法访问
问题描述
我在 ubuntu 20.04 服务器上测试 docker 19.03
当我启动一个 nginx 容器时
docker run --name nginx2 -p 80:80 --rm nginx
一切都很好:可以使用 Ubuntu 服务器的 ip 访问网络服务器
但是当我用 docker-compose 启动容器时
services:
nginxProxy:
container_name: nginxProxy
image: nginx:latest
ports:
- '80:80'
网络服务器没有响应
我解决了这个问题:它与 netplan 有关,在这里找到了解决方案https://github.com/docker/for-linux/issues/924
要完成我的设置说明:
- 我使用包 docker.io(来自 ubuntu)安装了第一个 docker,并发现了暴露的问题。
- 我现在从头开始重新安装了我的 Ubuntu 20.04,并使用包 docker-ce(来自 docker)安装了 docker,但无法连接到任何容器。docker0 网桥已关闭。
我的网络计划是
network:
version: 2
ethernets:
mainif:
match:
name: '*'
dhcp4: yes
我将其更改为(源中描述的,有 2 个带有 eno1 和 eno2 的 nic)
network:
version: 2
ethernets:
mainif:
match:
name: 'en*'
dhcp4: yes
现在一切都很好
解决方案
尝试在docker-compose.yml
. 我刚刚在 Ubuntu 20.04 上尝试过,它打开了默认的 Nginx 登录页面。这是我使用的文件:
version: "3"
services:
nginxProxy:
container_name: nginxProxy
image: nginx:latest
ports:
- '80:80'
此外,您粘贴的输出听起来像预期的那样工作。我不知道您在哪里运行服务器,但它也可能与某些防火墙问题或配置错误有关。根据@Rezwan 的建议,为了不实时跟踪正在发生的事情,请添加-d
选项:
docker-compose up -d
并尝试通过在浏览器中键入其 IP 地址来访问 Web 服务器。这是我的设置的日志输出:
Creating nginxProxy ... done
Attaching to nginxProxy
nginxProxy | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginxProxy | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginxProxy | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginxProxy | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginxProxy | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginxProxy | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginxProxy | /docker-entrypoint.sh: Configuration complete; ready for start up
nginxProxy | ip.add.re.ss- - [04/Dec/2020:22:28:37 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
推荐阅读
- javascript - 无法更正错误“失败 1 - 表格标记应有 3 行并检查表格和 tr 的 CSS 要求”
- python-3.x - QTableWidget 上的 PYQT5 setCellWidget() 会减慢 UI
- python - 角色动画发生得非常快
- node.js - writeFile 没有创建新文件
- python - 只需要从列表中选择至少存在一个值的行
- javascript - 尝试通过推送数组来创建数据库
- c++ - 有没有办法在 C++ 中定义一个类时“注册”一个类?
- javascript - 在 javascript 中搜索字符串时跳过或忽略部分字符串
- azure - 通过策略启用 Azure NSG FlowLogs
- c++ - 尝试完美的正方形你能给点小费吗