Dart Server for Flutter on Web in Docker

Issue

I’m trying to build a dart server that can serve flutter web apps. I’ve tried it a few different ways, building from scratch, using shelf (and shelf_static), and lastly GetX, but I can’t get them to run in a docker container. For instance, I currently have a simple structure:

-myApp/
  -web/
  -Dockerfile
  -pubspec.yaml
  -server.dart

I took the basic flutter demo app (with the floating button & counter), added web support, then did $ flutter build web, and took the build/web folder and copied it here as just web. Pubspec only includes get_server and build_runner, the server.dart file is only:

import 'package:get_server/get_server.dart';
void main() => runApp(GetServer(home: FolderWidget('web')));

And then my Dockerfile looks like this (based on this github repo):

################
FROM google/dart:2.10

RUN apt -y update && apt -y upgrade

WORKDIR /app
COPY pubspec.yaml /app/pubspec.yaml
RUN dart pub get
COPY . .
RUN dart pub get --offline

RUN dart pub run build_runner build --delete-conflicting-outputs
RUN dart compile exe /app/server.dart -o /app/server

########################
FROM subfuzion/dart:slim
COPY --from=0 /app /app
EXPOSE 8080
ENTRYPOINT ["/app/server"]

If I run the server from the command line:

$ dart server.dart

It runs perfectly. But if I build docker and run it:

$ docker build -t myApp .
$ docker run -d -p 8080:8080 myapp

It just says that the page can’t be found. Does anyone know what I’m doing wrong? Or have suggestions for how to host a Flutter Web App using a Dart Server (it doesn’t have to be GetX).

Solution

Turns out I’m just an idiot and was trying to serve the website from the same directory on my computer as in my docker container (suprise! that didn’t work).
I’ve changed my server.dart file to be:

import 'package:get_server/get_server.dart';
void main() => runApp(GetServer(home: FolderWidget('./app/web')));

And my docker file is now:

################
FROM google/dart:2.10

WORKDIR /app
COPY pubspec.yaml /app/pubspec.yaml
RUN dart pub get
COPY . .
RUN dart pub get --offline
RUN dart compile exe bin/server.dart -o bin/server

########################
FROM subfuzion/dart:slim
COPY --from=0 /app/bin/server /app/bin/server
COPY --from=0 /app/web /app/web
EXPOSE 8080
ENTRYPOINT ["/app/bin/server"]

And it works like a charm.

Answered By – Grey

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.