Skip to content

Project building

Plague Fox edited this page Jan 7, 2022 · 8 revisions

How to build project inside docker container:

docker run --rm -it -v ${PWD}:/project --workdir /project --user=root:root --name flutter_stable_web \
  plugfox/flutter:stable-web /bin/bash -c \
  "cd /project && flutter pub get && flutter pub run build_runner build && flutter build web --release"

How to build in GitHub CI pipeline:

name: BUILD

on:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    container: plugfox/flutter:stable-android-warmed
    steps:
      - uses: actions/checkout@v2

      - name: 🕊️ Get dependencies
        run: flutter pub get

      - name: ✨ Codegeneration
        run: flutter pub run build_runner build --delete-conflicting-outputs --release

      - name: 🏗️ Build apk
        run: flutter build apk --release --pub --shrink --target-platform android-arm,android-arm64,android-x64

How to build and serve flutter web with docker compose:

⚠️ BEWARE IN MY EXAMPLE I USE MONOREPO WITH SUBFOLDERS shared AND client ⚠️

  • Create ./dockerfiles/site.dockerfile
# Prepare web release
FROM plugfox/flutter:stable-web AS build

# USER root
WORKDIR /home

# Copy app source code and compile it
COPY --chown=101:101 client client
COPY --chown=101:101 shared shared

RUN set -eux; \
    # Change directory to client for monorepo
    cd client \
    # Ensure packages are still up-to-date if anything has changed
    && flutter pub get \
    # Codegeneration
    && flutter pub run build_runner build --delete-conflicting-outputs --release \
    # Localization
    #&& flutter pub global run intl_utils:generate
    # Build web release (opt --tree-shake-icons)
    && flutter build web --release --no-source-maps \
        --pwa-strategy offline-first \
        --web-renderer auto --base-href /

# Production from scratch
FROM nginx:alpine as production

COPY --from=build --chown=101:101 /home/client/build/web /usr/share/nginx/html

# Expose server
EXPOSE 80/tcp
  • Create ./site.compose.yml
version: '3.9'

services:
  site:
    hostname: my-site
    container_name: my-site
    image: registry.domain.tld/my-site
    restart: unless-stopped
    ports:
      - '80:80'
    build:
      context: ./
      dockerfile: dockerfiles/site.dockerfile
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80"]
      interval: 60s
      timeout: 10s
      retries: 3
    deploy:
      replicas: 1
      resources:
        reservations:
          cpus: '0.10'
          memory: 64M
        limits:
          cpus: '0.25'
          memory: 256M
  • Build and serve
docker compose -f ./site.compose.yml up -d site
  • And after all you can open localhost in browser