skip to Main Content

I generated a dart project with dart create -t server-shelf . --force.

On the top folder I created a json file (my_data.json) with some mock data.
In my code I am using the data from the json file like:

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

But if I try to start my server with docker run -it -p 8080:8080 myserver I am getting:

FileSystemException: Cannot open file, path = ‘my_data.json’ (OS
Error: No such file or directory, errno = 2)

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

2

Answers


  1. I think since you didn’t set the WORKDIR for the new image that you started building FROM scratch. You can fix this simply by adding WORKDIR /app again, to the specification of the new image you’re building, which is being used to run your application. It will look like this:

    ...
    
    # Start server.
    WORKDIR /app
    EXPOSE 8080
    CMD ["/app/bin/server"]
    
    Login or Signup to reply.
  2. Replace

    COPY my_data.json /app/my_data.json
    

    with

    COPY --from=build app/my_data.json app/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search