首页 > 解决方案 > 使用 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

要完成我的设置说明:

我的网络计划是

network:
    version: 2
    ethernets:
      mainif:
        match:
          name: '*'
        dhcp4: yes

我将其更改为(源中描述的,有 2 个带有 eno1 和 eno2 的 nic)

network:
    version: 2
    ethernets:
      mainif:
        match:
          name: 'en*'
        dhcp4: yes

现在一切都很好

标签: dockernetworkingdocker-compose

解决方案


尝试在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" "-"

推荐阅读