运行Dart Shelf Docker容器时的FileSystemException



我用dart create -t server-shelf . --force生成了一个dart项目。

在顶部文件夹中,我创建了一个json文件(my_data.json),其中包含一些模拟数据。在我的代码中,我使用json文件中的数据,如:

final _data = json.decode(File('my_data.json').readAsStringSync()) as List<dynamic>;

但是如果我尝试用docker run -it -p 8080:8080 myserver启动我的服务器,我得到:

FileSystemException: Cannot open file, path = 'my_data. 'json(操作系统错误:没有这样的文件或目录,errno = 2)

我Dockerfile:

# Use latest stable channel SDK.
FROM dart:stable AS build
# Resolve app dependencies.
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get
# Copy app source code (except anything in .dockerignore) and AOT compile app.
COPY . .
RUN dart compile exe bin/server.dart -o bin/server
# Build minimal serving image from AOT-compiled `/server`
# and the pre-built AOT-runtime in the `/runtime/` directory of the base image.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/
COPY my_data.json /app/my_data.json
# Start server.
EXPOSE 8080
CMD ["/app/bin/server"]

我想既然你没有设置WORKDIR开始建造FROM scratch的新形象。您可以通过再次将WORKDIR /app添加到您正在构建的用于运行应用程序的新映像的规范中来解决这个问题。它看起来像这样:

...
# Start server.
WORKDIR /app
EXPOSE 8080
CMD ["/app/bin/server"]

Replace

COPY my_data.json /app/my_data.json

COPY --from=build app/my_data.json app/

最新更新