Docker - Flutter Web 部署 - 无法访问站点

Docker - Flutter Web deployment - Site cannot be reached

我正在使用 Docker 和 Flutter Web。我刚刚完成 Docker 的设置并为其撰写。 我的 Dockerfile

FROM ubuntu:18.04

ARG PROJECT_DIR=/srv/api
ENV PATH=/opt/flutter/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

RUN apt-get update && \
    apt-get install -y \
        xz-utils \
        git \
        openssh-client \
        curl && \
    apt-get upgrade -y && \
    rm -rf /var/cache/apt

RUN curl -L https://storage.googleapis.com/flutter_infra/releases/stable/linux/flutter_linux_v1.7.8+hotfix.4-stable.tar.xz | tar -C /opt -xJ

RUN apt-get install -y lib32stdc++6

WORKDIR ${PROJECT_DIR}
COPY ./ ./

RUN flutter doctor
RUN flutter upgrade
RUN flutter packages pub global activate webdev
RUN flutter packages upgrade

我的docker-compose.yaml

version: '3.1'
services:
  api:
    container_name: flutter-web-test
    restart: always
    image: flutter-web-test
    build:
      context: ./
    volumes:
      - //var/run/docker.sock:/var/run/docker.sock
    command: ["flutter", "pub", "global", "run", "webdev", "serve", "web:5001"]

执行 docker-compose -f docker-compose.yaml builddocker-compose -f docker-compose.yaml up 后,我收到服务器应该已启动。

flutter-web-test | web

flutter-web-test | [WARNING] Throwing away cached asset graph due to Dart SDK update.


flutter-web-test | [INFO] Cleaning up outputs from previous builds. completed, took 76ms

flutter-web-test | [INFO] Building new asset graph completed, took 2.5s

flutter-web-test | [INFO] Checking for unexpected pre-existing outputs. completed, took 2ms

flutter-web-test | [INFO] Serving `web` on http://127.0.0.1:5001

但是,当我尝试连接到 http://127.0.0.1:5001 - 我看到 This site cannot be reached window。

如果我在本地 运行 它,它工作得很好。我相信我一定在 Docker 中跳过了某些内容,但找不到问题。任何人都可以帮助发现问题吗?

您的 docker-compose 文件

中缺少 port
version: '3.1'
services:
  api:
    container_name: flutter-web-test
    restart: always
    image: flutter-web-test
    port:
      - "5001":"5001"
    build:
      context: ./
    volumes:
      - //var/run/docker.sock:/var/run/docker.sock
    command: ["flutter", "pub", "global", "run", "webdev", "serve", "web:5001"]

webdev 服务器应该正在侦听 0.0.0.0 like this

服务 ports 应映射到主机,docker 守护程序路径应为 /var/run/docker.sock:

version: '3.1'
services:
  api:
    container_name: flutter-web-test
    restart: always
    image: flutter-web-test
    ports:
      - "5001:5001"
    build:
      context: ./
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command: ["flutter", "pub", "global", "run", "webdev", "serve", "--hostname", "0.0.0.0:5001"]