我有我的私人自托管 docker 镜像,里面有 nextjs。该图像暴露了 3000 端口。
我尝试通过 docker 和 docker swarm 运行此映像。
docker run -p 3000:3000 path-to-my-image
结果:
docker ps
:
0.0.0.0: 3000->3000/tcp, ::: 3000->3000/tcp
ps
容器内:
PID USER TIME COMMAND
1 nextjs 0:00 next-router-wo
22 nextjs 0:01 next-render-worker-app
23 nextjs 0:01 next-render-worker-pages
51 nextjs 0:00 sh
57 nextjs 0:00 ps
netstat -tulp|grep 3000
容器内:
tcp 0 0 cf748210f6f3:3000 0.0.0.0:* LISTEN 1/next-router-wo
curl http://127.0.0.1:3000
输出正确的响应。所以一切都很好。
stack.yaml
version: "3.7"
services:
app:
image: path-to-my-image
ports:
- 3000:3000
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]
docker service ls
显示服务已上线,1个副本
ps
容器内:
PID USER TIME COMMAND
1 nextjs 0:00 next-router-wo
22 nextjs 0:01 next-render-worker-app
23 nextjs 0:01 next-render-worker-pages
50 nextjs 0:00 sh
56 nextjs 0:00 ps
netstat -tulp|grep 3000
容器内:
tcp 0 0 3e53ca570e77:3000 0.0.0.0:* LISTEN 1/next-router-wo
但是
curl http://127.0.0.1:3000
失败了:
curl: (7) Failed to connect to 127.0.0.1 port 3000 after 0 ms: Connection refused
尝试了另一个端口,另一台机器,尝试了
node
和pm2
。一切都一样。
我不明白原因以及下一步该往哪里挖掘。
提前谢谢您。
使用 Swarm,您需要采取额外的步骤来发布端口以供外部使用。
从命令行,您可以按照此处所述进行操作:
docker service update --publish-add 3000 app
您也可以使用
stack.yaml
在 expose
中定义它:
version: "3.7"
services:
app:
image: path-to-my-image
ports:
- 3000:3000
expose:
- 3000
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]