如何解决使用 Nodejs (Hapi.js) 和 Prisma 的 Docker 中的连接错误
我想将我的 Nodejs 应用程序放入 docker。通过 npm run build 和 start 部署它时,我可以向它发送请求。
但是在创建 docker 镜像时,我遇到了问题:
首先,我的 Dockerfile 中有一个 EXPOSE 8080。然后我正在运行 docker run -p=3000:8080 --env-file .env my-docker-file
。之后我得到了服务器在 http://localhost:3000 上运行的信息。
我知道 localhost:3000 只是在 docker 文件中。但至少 docker 正在运行。
当我使用命令 http localhost:3000
(或浏览器)时,我得到 http: error: ConnectionError: ('Connection aborted.',RemoteDisconnected('Remote end closed connection without response')) while doing a GET request to URL: http://localhost:3000/
。
有人知道出了什么问题吗???我不知道。
接受所有指引我走向正确方向的提示。
我的 Dockerfile:
## this is the stage one,also know as the build step
FROM node:12.17.0-alpine as builder
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/
COPY tsconfig.json .
COPY src ./src/
COPY tests ./tests/
RUN npm install
RUN npx prisma generate
COPY . .
RUN npm run build
## this is stage two,where the app actually runs
FROM node:12.17.0-alpine
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist
EXPOSE 8080
CMD npm start
解决方法
如果您使用 Dockerfile,首先最好构建您的镜像。
FROM node:12.17.0-alpine as builder
WORKDIR /app
COPY . .
RUN npm install
RUN npx prisma generate
RUN npm run build
## this is stage two,where the app actually runs
FROM node:12.17.0-alpine
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist
EXPOSE 8080
CMD ["npm","start"]
从您的 Dockerfile
所在的位置:
docker build -t your-image-name .
docker run -p 3000:8080 --env-file .env your-image-name
您检查 IP 地址了吗?
当我第一次将我的 Node 项目部署到 Docker 时,我也无法访问它,因为我的 Node 项目正在侦听 localhost
请求。但是,如果您没有将网络指定为主机,则您的 Docker 容器将在您的子网中拥有其他一些 IP 地址。
我已将我的 Node 项目侦听 IP 地址更改为 0.0.0.0
,之后我可以连接到在 Docker 容器中运行的 Node 项目。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。