Docker部署dart-frog服务

参考:

        dart-frog官网:https://dartfrog.vgv.dev/docs/overview

        使用 'Dart Frog' 体验 Dart 服务端开发 - 简书

打包项目

按照demo新增项目后,执行:

dart_frog build

等待build后生成 build 文件夹,这个文件夹就是需要部署的内容

需要注意,由于环境问题,需要编辑一下 build/Dockerfile 文件:

# Official Dart image: https://hub.docker.com/_/dart
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.17)

FROM dart:stable AS build

# 在此处添加PUB_HOSTED_URL 和 FLUTTER_STORAGE_BASE_URL

ENV PUB_HOSTED_URL="https://pub.flutter-io.cn"
ENV FLUTTER_STORAGE_BASE_URL="https://storage.flutter-io.cn"


WORKDIR /app

# Copy Dependencies


# Install Dependencies


# Resolve app dependencies.
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date if anything has changed
RUN dart pub get --offline
RUN dart compile exe bin/server.dart -o bin/server

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/

# Start server.
CMD ["/app/bin/server"]
 

部署服务器

然后把 build/ 放到服务器

需要先查看Docker 有没有 dart 环境,如果没有就下载一下

docker pull dart:latest

在服务器下进入文件夹执行一下指令,构建项目(Custom Dockerfile

docker build . -t dart-frog-app

如果没有修改Dockerfile文件,编译的时候会报错

[+] Building 49.2s (8/13)                                                                                docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 912B                                                                               0.0s
 => [internal] load metadata for docker.io/library/dart:stable                                                    21.9s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 126B                                                                                  0.0s
 => [build 1/7] FROM docker.io/library/dart:stable@sha256:7e0b4e5d3773c61b5d5b42908f48853fab04c33a080c6f73ee3a99b  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 428B                                                                                  0.0s
 => CACHED [build 2/7] WORKDIR /app                                                                                0.0s
 => CACHED [build 3/7] COPY pubspec.* ./                                                                           0.0s
 => ERROR [build 4/7] RUN dart pub get                                                                            27.1s
------
 > [build 4/7] RUN dart pub get:
0.531 Resolving dependencies...
27.06 Got socket error trying to find package test at https://pub.dev.
------
Dockerfile:15
--------------------
  13 |     # Resolve app dependencies.
  14 |     COPY pubspec.* ./
  15 | >>> RUN dart pub get
  16 |
  17 |     # Copy app source code and AOT compile it.
--------------------
ERROR: failed to solve: process "/bin/sh -c dart pub get" did not complete successfully: exit code: 69

成功情况下

[+] Building 101.6s (14/14) FINISHED                                                                     docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 916B                                                                               0.0s
 => [internal] load metadata for docker.io/library/dart:stable                                                    88.6s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 126B                                                                                  0.0s
 => [build 1/7] FROM docker.io/library/dart:stable@sha256:7e0b4e5d3773c61b5d5b42908f48853fab04c33a080c6f73ee3a99b  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 428B                                                                                  0.0s
 => CACHED [build 2/7] WORKDIR /app                                                                                0.0s
 => CACHED [build 3/7] COPY pubspec.* ./                                                                           0.0s
 => [build 4/7] RUN dart pub get                                                                                   6.9s
 => [build 5/7] COPY . .                                                                                           0.1s
 => [build 6/7] RUN dart pub get --offline                                                                         1.3s
 => [build 7/7] RUN dart compile exe bin/server.dart -o bin/server                                                 3.9s
 => [stage-1 1/2] COPY --from=build /runtime/ /                                                                    0.1s
 => [stage-1 2/2] COPY --from=build /app/bin/server /app/bin/                                                      0.1s
 => exporting to image                                                                                             0.1s
 => => exporting layers                                                                                            0.1s
 => => writing image sha256:248f909c7afe366ebd0f045fb76eac2c491d840ffa22ce9609df3c56ac42125a                       0.0s
 => => naming to docker.io/library/dart-frog-app                                                                   0.0s

View build details: docker-desktop://dashboard/build/default/default/vr5kryu8vmt430i7hgx6vl9jw

What's Next?
  1. Sign in to your Docker account → docker login
  2. View a summary of image vulnerabilities and recommendations → docker scout quickview

部署项目

docker run -i -t -p 8080:8080 dart-frog-app

成功情况下

✓ Running on http://:::8088

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值