From 6ee34c4d97fac691b28145b2f73a8168cdc5ef7d Mon Sep 17 00:00:00 2001 From: Dat PHAM HOANG Date: Mon, 21 Feb 2022 16:42:51 +0700 Subject: [PATCH] Package the webapp in a docker nginx lightweight container --- Dockerfile | 58 ++++++++++++++++++++-------------- env.file | 1 + lib/main.dart | 2 ++ lib/main/utils/app_config.dart | 3 +- pubspec.yaml | 4 +++ server/nginx.conf | 17 ++++++++++ 6 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 env.file create mode 100644 server/nginx.conf diff --git a/Dockerfile b/Dockerfile index b404ac7c3..446c23e14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,44 @@ #Stage 1 - Install dependencies and build the app FROM debian:latest AS build-env -# Install flutter dependencies -RUN apt-get update -RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libglu1-mesa fonts-droid-fallback python3 -RUN apt-get clean +ENV FLUTTER_CHANNEL="stable" +ENV FLUTTER_VERSION="2.10.0" +ENV FLUTTER_URL="https://storage.googleapis.com/flutter_infra_release/releases/$FLUTTER_CHANNEL/linux/flutter_linux_$FLUTTER_VERSION-$FLUTTER_CHANNEL.tar.xz" +ENV FLUTTER_HOME="/opt/flutter" -# Clone the flutter repo -RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter +ENV PATH "$PATH:$FLUTTER_HOME/bin" -# Set flutter path -# RUN /usr/local/flutter/bin/flutter doctor -v -ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}" +# Prerequisites +RUN apt update && apt install -y curl git unzip xz-utils zip libglu1-mesa \ + && mkdir -p $FLUTTER_HOME \ + && curl -o flutter.tar.xz $FLUTTER_URL \ + && tar xf flutter.tar.xz -C /opt \ + && rm flutter.tar.xz \ + && flutter doctor \ + && rm -rf /var/lib/{apt,dpkg,cache,log} -# Run flutter doctor -RUN flutter doctor -v -# Enable flutter web -RUN flutter channel master -RUN flutter upgrade -RUN flutter config --enable-web +# Set directory to Copy App +WORKDIR /app -# Copy files to container and build -RUN mkdir /app/ -COPY . /app/ -WORKDIR /app/ -RUN flutter build web +COPY . . -# Record the exposed port -EXPOSE 3000 +# Precompile tmail flutter +RUN cd core \ + && flutter pub get \ + && cd ../model \ + && flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs \ + && cd .. \ + && flutter pub get && flutter pub run intl_generator:extract_to_arb --output-dir=./lib/l10n lib/main/localizations/app_localizations.dart \ + && flutter pub get && flutter pub run intl_generator:generate_from_arb --output-dir=lib/l10n --no-use-deferred-loading lib/main/localizations/app_localizations.dart lib/l10n/intl*.arb \ + && 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 \ No newline at end of file +FROM nginx:stable +RUN chmod -R 755 /usr/share/nginx/html +COPY --from=build-env /app/server/nginx.conf /usr/share/nginx/html +COPY --from=build-env /app/build/web /usr/share/nginx/html + +# Record the exposed port +EXPOSE 80 + +ENTRYPOINT ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/env.file b/env.file new file mode 100644 index 000000000..9bf93db70 --- /dev/null +++ b/env.file @@ -0,0 +1 @@ +SERVER_URL=http://localhost \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 7a24f5a62..821499ae5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:core/core.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:get/get.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart'; @@ -14,6 +15,7 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); await MainBindings().dependencies(); await HiveCacheConfig().setUp(); + await dotenv.load(fileName: 'env.file'); runApp(TMailApp()); }); } diff --git a/lib/main/utils/app_config.dart b/lib/main/utils/app_config.dart index a4bcfc3fa..bd4b17aab 100644 --- a/lib/main/utils/app_config.dart +++ b/lib/main/utils/app_config.dart @@ -1,4 +1,5 @@ +import 'package:flutter_dotenv/flutter_dotenv.dart'; class AppConfig { - static const baseUrl = String.fromEnvironment('SERVER_URL', defaultValue: ''); + static String get baseUrl => dotenv.get('SERVER_URL', fallback: ''); } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index b3ea7d9b7..e5f15eb10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -58,6 +58,9 @@ dependencies: # either dartz: 0.10.1 + # flutter_dotenv + flutter_dotenv: 5.0.2 + # flutter_typeahead flutter_typeahead: 3.1.1 @@ -153,6 +156,7 @@ flutter: # - images/a_dot_ham.jpeg assets: - assets/images/ + - env.file # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware. diff --git a/server/nginx.conf b/server/nginx.conf new file mode 100644 index 000000000..758a646e5 --- /dev/null +++ b/server/nginx.conf @@ -0,0 +1,17 @@ +server { + + listen 80; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + + location = /50x.html { + root /usr/share/nginx/html; + } + +} \ No newline at end of file