参考:https://www.codingone.top/archives/191">踩坑归来,经验贴!成功完成 Hoppscotch 的 Docker 部署!
想用Hoppscotch作接口测试工具,结果在github上学习了大半天,终于研究出来了怎么部署了。
官方仓库里面的docker-compose.yaml 更多是用来build编译镜像用的(里面包含了多个不同版本的镜像)。 其实如果只是你自己部署,只需要2个镜像,一个Hoppscotch-aio(All in One)作为多合一的应用服务器, 一个postgres做数据库服务器。 而且,如果是自己部署,根本不需要从头build,项目组在docker hub上有编译好的All in One 镜像:hoppscotch/hoppscotch:latest 所以自己想要部署的话,只要拉下镜像,就能用了
但是!!还有一个大坑!!
就是数据库容器hoppscotch-db(来自postgres数据库镜像),需要一次数据迁移(类似数据初始化),但是相关脚本和docker-compose都没有涉及。相关问题在hoppsctoch项目的issue中已经有不少人解答。但是操作起来都不免有些麻烦。 在研究学习的过程中,我找到了一条简单的利用docker-compose的方法。
问题解决:
-
建议先git clone hoppscotch的仓库,需要使用到其中的
.env.example文件(也可以自己下载这个文件),复制一份命名为.env,在docker-compose.yaml中指定了.env作为容器运行的环境变量。 - 编写自己的docker-compose.yaml, 原理就是除了平时运行需要的hoppscotch-aio和hoppscotch-db, 增加1个第一次初始化用的 hoppscotch-aio-init. 可以参考我的docker-compose.yaml修改:
services:
# The service that spins up all 3 services at once in one container
hoppscotch-aio:
container_name: hoppscotch-aio
image: hoppscotch/hoppscotch:latest
restart: unless-stopped
env_file:
- ./.env
depends_on:
hoppscotch-db:
condition: service_healthy
ports:
- "3000:3000"
- "3100:3100"
- "3170:3170"
- "3080:80"
hoppscotch-aio-init:
container_name: hoppscotch-aio-init
image: hoppscotch/hoppscotch:latest
entrypoint: ["sh", "-c", "pnpm config set registry https://registry.npmmirror.com/ && pnpm exec prisma migrate deploy"] # 覆盖默认的ENTRYPOINT
env_file:
- ./.env
depends_on:
hoppscotch-db:
condition: service_healthy
ports:
- "3000:3000"
- "3100:3100"
- "3170:3170"
- "3080:80"
# The preset DB service, you can delete/comment the below lines if
# you are using an external postgres instance
# This will be exposed at port 5432
hoppscotch-db:
container_name: hoppscotch-db
image: postgres:15
ports:
- "5432:5432"
user: postgres
environment:
# The default user defined by the docker image
POSTGRES_USER: postgres
# NOTE: Please UPDATE THIS PASSWORD!
POSTGRES_PASSWORD: testpass
POSTGRES_DB: hoppscotch
healthcheck:
test:
[
"CMD-SHELL",
"sh -c 'pg_isready -U {POSTGRES_USER} -d{POSTGRES_DB}'",
]
interval: 5s
timeout: 5s
retries: 10
- 先注释docker-compose.yaml中的hoppscotch-aio部分【要手工按要求进行注释】,保留hoppscotch-aio-init和hoppscotch-db部分。然后使用命令docker -compose up -d 启动。然后查看日志 docker-compose logs -f ,看到如下内容,说明数据库已经初始化成功:
hoppscotch-aio | └─ 20240725043411_infra_config_encryption/
hoppscotch-aio | └─ migration.sql
hoppscotch-aio | └─ 20240726121956_infra_token/
hoppscotch-aio | └─ migration.sql
hoppscotch-aio |
hoppscotch-aio | All migrations have been successfully applied.
- 这个时候,通过docker-compose ps -a查看状态,能看到hoppscotch-aio-init已经是退出Exited状态了,而hoppscotch-db还是启动Up中。此时可以使用
docker-compose rm命令删除已经完成使命的 hoppscotch-aio-init 容器。
$ docker compose rm
? Going to remove hoppscotch-aio-init Yes
[+] Removing 1/0
✔ Container hoppscotch-aio-init Removed
- 现在可以重新打开docker-compose.yaml进行编辑,把之前第3步注释的hoppscotch-aio部分,取消注释;同时,把hoppscotch-aio-init部分注释掉。
-
修改好docker-compose.yaml后,就可以再次执行
docker-compose up -d命令,启动hoppscotch-aio。等待大约10秒左右,就会启动成功。也可以使用 docker-compose logs -f 命令,查看日志信息,可以看到成功启动的信息:
hoppscotch-aio | Backend Server | [Nest] 37 - 09/10/2024, 6:47:07 AM LOG [RouterExplorer] Mapped {/infra/users/:uid/admin-status, PATCH} (version: 1) route +0ms
hoppscotch-aio | Backend Server | Initialize PubSub
hoppscotch-aio | Backend Server | [Nest] 37 - 09/10/2024, 6:47:08 AM LOG [GraphQLModule] Mapped {/graphql, POST} route +415ms
hoppscotch-aio | Backend Server | [Nest] 37 - 09/10/2024, 6:47:08 AM LOG [NestApplication] Nest application successfully started
- 现在就可以登陆 localhost:3000 进行访问拉。
谢谢!这个破项目让我仔细学习了以下docker-compose和dockfile相关命令,还有关于apt\npm\pnpm等镜像构建过程中,可能需要使用国内加速的命令:
# 国内的 alpine 镜像服务器, wangyuanhong.fj
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 国内的 npm 镜像服务器, wangyuanhong.fj
npm config set registry https://registry.npmmirror.com/
# 国内的 npm 镜像服务器, wangyuanhong.fj
pnpm config set registry https://registry.npmmirror.com/
文章评论