0

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

jps
  • 20,041
  • 15
  • 75
  • 79
R2T8
  • 2,122
  • 2
  • 14
  • 17
  • I suspect you have the wrong current directory for the app. Either open `/app/my_data.json` in your program, or figure out how to cd to the right directory before launching the app. Hint: running an app doesn't change the current directory. – Randal Schwartz Jul 09 '22 at 22:38

2 Answers2

0

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"]
zer0
  • 2,153
  • 10
  • 12
0

Replace

COPY my_data.json /app/my_data.json

with

COPY --from=build app/my_data.json app/
desertnaut
  • 57,590
  • 26
  • 140
  • 166
whynot
  • 1
  • 1