Dockerize flutter web application

Issue

I’m trying to Dockerize my flutter web application. I have tried with many images, like use debian and download and install flutter on it, use unnoficial images like haizen/flutter or cirrusci/flutter. But in all attempts it’s returning an exception that is not appearing when i’m running manually the build.

Here’s my Dockerfile:

# 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

And this is the problem, running on GitHub Actions. I’ve tried running it on my machine too, with the same problem:

#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

I don’t know what to do, all commands are running with no errors in my computer. But when i try to build in Docker this error occurs. Can someone help me?

Solution

I’ve solved this problem. In the first line of rotation.dart the import 'package:flutter/services.Dart'; the extension is with first letter uppercase. building in Windows work, but in Docker don’t.

Answered By – João Pedro

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *