Dockerize flutter web 应用程序

Dockerize flutter web application

我正在尝试Docker调整我的 Flutter Web 应用程序。我尝试过很多图像,比如使用 debian 并在其上下载并安装 flutter,使用像 haizen/flutter 或 cirrusci/flutter 这样的非官方图像。但在所有尝试中,它都返回了一个异常,当我 运行 手动构建时没有出现该异常。

这是我的Docker文件:

# Install flutter
FROM haizen/flutter:2.10.2-1-alpine AS build-env

# Run flutter doctor and enable web
RUN flutter doctor
RUN flutter config --enable-web

# Copy files to container and build
USER root
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN flutter build web

# Stage 2 - Create the run-time image
FROM nginx:1.21.1-alpine
COPY --from=build-env /app/build/web /usr/share/nginx/html

这就是问题所在,运行 在 GitHub 操作上。我也在我的机器上试过运行,但有同样的问题:

#15 24.20 Running "flutter pub get" in app...                                23.2s
#15 24.56 
#15 24.56  Building with sound null safety 
#15 24.56 
#15 24.64 Compiling lib/main.dart for the Web...                          
#15 43.49 Target dart2js failed: Exception: lib/classes/rotation.dart:1:8:
#15 43.49 Error: Error when reading '/home/user/development/flutter/packages/flutter/lib/services.Dart': Error reading '/home/user/development/flutter/packages/flutter/lib/services.Dart'  (No such file or directory)
#15 43.49 import 'package:flutter/services.Dart';
#15 43.49        ^
#15 43.49 lib/classes/rotation.dart:6:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.portraitUp,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:7:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.portraitDown,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:5:7:
#15 43.49 Error: Undefined name 'SystemChrome'.
#15 43.49       SystemChrome.setPreferredOrientations([
#15 43.49       ^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:13:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.landscapeLeft,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:14:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.landscapeRight,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:12:7:
#15 43.49 Error: Undefined name 'SystemChrome'.
#15 43.49       SystemChrome.setPreferredOrientations([
#15 43.49       ^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:20:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.portraitUp,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:21:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.portraitDown,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:22:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.landscapeLeft,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:23:9:
#15 43.49 Error: Undefined name 'DeviceOrientation'.
#15 43.49         DeviceOrientation.landscapeRight,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:19:7:
#15 43.49 Error: Undefined name 'SystemChrome'.
#15 43.49       SystemChrome.setPreferredOrientations([
#15 43.49       ^^^^^^^^^^^^
#15 43.49 Error: Compilation failed.
#15 43.49 
#15 43.49 Compiling lib/main.dart for the Web...                             18.8s
#15 43.50 Exception: Failed to compile application for the Web.
#15 ERROR: executor failed running [/bin/sh -c flutter build web]: exit code: 1
------
 > [build-env 7/7] RUN flutter build web:
#15 43.49         DeviceOrientation.landscapeRight,
#15 43.49         ^^^^^^^^^^^^^^^^^
#15 43.49 lib/classes/rotation.dart:19:7:
#15 43.49 Error: Undefined name 'SystemChrome'.
#15 43.49       SystemChrome.setPreferredOrientations([
#15 43.49       ^^^^^^^^^^^^
#15 43.49 Error: Compilation failed.
#15 43.49 
#15 43.49 Compiling lib/main.dart for the Web...                             18.8s
#15 43.50 Exception: Failed to compile application for the Web.
------
error: failed to solve: executor failed running [/bin/sh -c flutter build web]: exit code: 1
Error: buildx failed with: error: failed to solve: executor failed running [/bin/sh -c flutter build web]: exit code: 1

我不知道该怎么办,所有命令都运行我的电脑没有错误。但是当我尝试在 Docker 中构建时,会发生此错误。有人可以帮助我吗?

我已经解决了这个问题。在 rotation.dart 的第一行中, import 'package:flutter/services.Dart'; 扩展名首字母大写。在 Windows 中构建工作,但在 Docker 中不工作。