Home > Back-end >  Dart Package - Can't create file inside docker
Dart Package - Can't create file inside docker

Time:02-26

[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>

this is the error i am getting when i am trying to return the url for the stored image which i uploaded using json request.

i am trying to build an API Server using dart and getting this error


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();

This code is working fine directly, but when i run it inside docker it gives me that error.

my docker file looks like

# 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"]

CodePudding user response:

Inside the final container, you're copying to the executable to /app/bin/server, which means that your file is expected to be somewhere in /app/bin/storage/ as well. As you've never created this directory in the container, it simply doesn't exist.

If you want to create the file inside of the container, manually make sure that you create the parent directory before writing the file. If you already expect those files to be available in the container, you need to copy them into the container in your DOCKERFILE.

CodePudding user response:

   File file = await File(path.join(
              path.dirname(Platform.script.toFilePath()),
              "storage",
              "userProfiles",
              "${Manager.getRandomString(20)}.png"))
          .create(recursive: true);

Whenever creating a file in docker environment in .create() add .create(recursive: true)

This is the only solution.

  • Related