如何在使用 docker 创建 flutter build web 时注入 URL

How inject URL while creating flutter build web using docker

我正在尝试在创建 flutter build web 时注入 URL。对于 Inject URL 我正在使用环境变量。 Docker 文件成功完全创建 docker 图像和 docker 容器。但是当我在浏览器上打开 Web 应用程序时,环境变量是空的。我不明白我错过了什么。以前我没有 docker 的经验那是我第一次。请指导我做错了什么。

要构建 docker 图像,我正在使用命令。

docker build . -t test-application

到运行docker容器我使用命令

docker run -ti -p 5000:4040 -e API_URL=example.com test-application

我的 docker 文件。

# Install dependencies
FROM debian:latest AS build-env
RUN  apt-get update 
RUN  apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3 psmisc
RUN  apt-get clean

# Clone the flutter repo
RUN  git clone https://github.com/flutter/flutter.git -b stable /usr/local/flutter

# Set flutter path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
    
# Enable flutter web
# RUN  flutter channel stable
# RUN  flutter upgrade
# RUN  flutter config --enable-web

# Run flutter doctor
RUN  flutter doctor -v

# Copy the app files to the container
COPY . /usr/local/bin/app

# Set the working directory to the app files within the container
WORKDIR /usr/local/bin/app

# Get App Dependencies
RUN  flutter clean

# Get App Dependencies
RUN  flutter pub get

# Build the app for the web
 RUN  flutter build web --release --dart-define API_URL=${API_URL:-}

# Document the exposed port
EXPOSE 4040

# Set the server startup script as executable
RUN  ["chmod", "+x", "/usr/local/bin/app/server/server.sh"]

# Start the web server
ENTRYPOINT [ "/usr/local/bin/app/server/server.sh" ]

我的server.sh文件

#!/bin/bash
# Welcome
echo 'Server start script initialized'
    
# Set the port
PORT=4040

# Kill anything that is already running on that port
echo 'Cleaning port' $PORT
fuser -k 4040/tcp

# Change directories to the release folder
cd build/web/

# Start the server
echo 'Starting server on port' $PORT
python3 -m http.server $PORT

# Exit
echo 'Server exited...'

获取我的环境变量

static const baseUrl =
      "${const String.fromEnvironment("API_URL")}/public";

要使用构建参数,您需要在 dockerfile 中添加 ARG 语句。要在 运行 时也可用,您可以添加一个 ENV 语句,它使用构建参数

的值设置一个环境变量
# Install dependencies
FROM debian:latest AS build-env

# Add the following two lines
ARG API_URL
ENV API_URL=$API_URL

RUN  apt-get update 
RUN  apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3 psmisc
RUN  apt-get clean

# Clone the flutter repo
RUN  git clone https://github.com/flutter/flutter.git -b stable /usr/local/flutter

# Set flutter path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
    
# Enable flutter web
# RUN  flutter channel stable
# RUN  flutter upgrade
# RUN  flutter config --enable-web

# Run flutter doctor
RUN  flutter doctor -v

# Copy the app files to the container
COPY . /usr/local/bin/app

# Set the working directory to the app files within the container
WORKDIR /usr/local/bin/app

# Get App Dependencies
RUN  flutter clean

# Get App Dependencies
RUN  flutter pub get

# Build the app for the web
 RUN  flutter build web --release --dart-define API_URL=${API_URL:-}

# Document the exposed port
EXPOSE 4040

# Set the server startup script as executable
RUN  ["chmod", "+x", "/usr/local/bin/app/server/server.sh"]

# Start the web server
ENTRYPOINT [ "/usr/local/bin/app/server/server.sh" ]

然后当你构建时,你像这样添加你想要的值

docker build --build-arg API_URL=example.com -t test-application .

你不需要在 运行 时指定它,除非你想覆盖它,所以你的 运行 命令变成了。当然,覆盖它只会覆盖环境变量,不会更改在构建时放入图像的内容。

docker run -ti -p 5000:4040 test-application