Skip to content

Instantly share code, notes, and snippets.

@MillerAdulu
Last active January 31, 2024 18:44
Show Gist options
  • Save MillerAdulu/a4a754e4f2564a1a157705810cad6f0b to your computer and use it in GitHub Desktop.
Save MillerAdulu/a4a754e4f2564a1a157705810cad6f0b to your computer and use it in GitHub Desktop.
Minimal Flutter Web Docker Deployment

Motivation

This is for when you need to deploy your Flutter app in a more controlled environment that's not firebase.

Usage

docker build --pull --rm -f "Dockerfile" -t my-cool-app:latest "."
docker run --rm -d -p 8080:80/tcp my-cool-app:latest 

*Could be any other port that's not 8080 as long as the port is reachable on a server

# Install Operating system and dependencies
FROM ubuntu:22.04 as build-env
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback python3
RUN apt-get clean
ENV DEBIAN_FRONTEND=dialog
ENV PUB_HOSTED_URL=https://pub.flutter-io.cn
ENV FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
# download Flutter SDK from Flutter Github repo
RUN git clone https://github.com/flutter/flutter.git -b stable --depth 1 /usr/local/flutter
# Set flutter environment path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
# Run flutter doctor
RUN flutter doctor
# Copy files to container and build
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN dart run build_runner build --delete-conflicting-outputs
RUN flutter build web --release --target=lib/main_production.dart
# --target=*** needed if your file isn't named `main.dart`
# Otherwise, RUN cp lib/main_production.dart lib/main.dart && flutter build web --release
# Stage 2
FROM nginx:1.25.3-alpine
COPY --from=build-env /app/build/web /usr/share/nginx/html
# Expose port 80
EXPOSE 80
# Start Nginx
CMD ["nginx", "-g", "daemon off;"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment