Dart 包 - 无法在 docker 中创建文件
Dart Package - Can't create file inside docker
[ERROR] 2022-02-24 20:23:09.936180 0:00:00.020812 POST /
FileSystemException: Cannot create file, path = '/app/images/pK4t0DSTW93okzNBQrbf.png' (OS Error: No such file or directory, errno = 2)
package:shelf/src/middleware/logger.dart 30 logRequests.<fn>.<fn>.<fn>
这是我在尝试 return url 我使用 json 请求上传的存储图像时遇到的错误。
我正在尝试使用 dart 构建一个 API 服务器并收到此错误
import 'package:path/path.dart' as path;
File file = await File(
path.join(path.dirname(Platform.script.toFilePath()), "storage",
"userProfiles", "${Manager.getRandomString(20)}.png"),
).create();
这段代码直接工作正常,但是当我 运行 它在 docker 里面时,它给了我那个错误。
我的 docker 文件看起来像
# 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/
# Start server.
EXPOSE 8080
CMD ["/app/bin/server"]
在最终容器中,您正在将可执行文件复制到 /app/bin/server
,这意味着您的文件也应该位于 /app/bin/storage/
中的某个位置。由于您从未在容器中创建此目录,因此它根本不存在。
如果要在容器内创建文件,请手动确保在写入文件之前创建父目录。如果您已经希望这些文件在容器中可用,则需要将它们复制到 DOCKERFILE
.
中的容器中
File file = await File(path.join(
path.dirname(Platform.script.toFilePath()),
"storage",
"userProfiles",
"${Manager.getRandomString(20)}.png"))
.create(recursive: true);
每当在 docker 环境中创建文件时,在 .create() 中添加 .create(recursive: true)
这是唯一的解决方案。
[ERROR] 2022-02-24 20:23:09.936180 0:00:00.020812 POST /
FileSystemException: Cannot create file, path = '/app/images/pK4t0DSTW93okzNBQrbf.png' (OS Error: No such file or directory, errno = 2)
package:shelf/src/middleware/logger.dart 30 logRequests.<fn>.<fn>.<fn>
这是我在尝试 return url 我使用 json 请求上传的存储图像时遇到的错误。
我正在尝试使用 dart 构建一个 API 服务器并收到此错误
import 'package:path/path.dart' as path;
File file = await File(
path.join(path.dirname(Platform.script.toFilePath()), "storage",
"userProfiles", "${Manager.getRandomString(20)}.png"),
).create();
这段代码直接工作正常,但是当我 运行 它在 docker 里面时,它给了我那个错误。
我的 docker 文件看起来像
# 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/
# Start server.
EXPOSE 8080
CMD ["/app/bin/server"]
在最终容器中,您正在将可执行文件复制到 /app/bin/server
,这意味着您的文件也应该位于 /app/bin/storage/
中的某个位置。由于您从未在容器中创建此目录,因此它根本不存在。
如果要在容器内创建文件,请手动确保在写入文件之前创建父目录。如果您已经希望这些文件在容器中可用,则需要将它们复制到 DOCKERFILE
.
File file = await File(path.join(
path.dirname(Platform.script.toFilePath()),
"storage",
"userProfiles",
"${Manager.getRandomString(20)}.png"))
.create(recursive: true);
每当在 docker 环境中创建文件时,在 .create() 中添加 .create(recursive: true)
这是唯一的解决方案。