我構建前端(next.js)和后端(express),并嘗試呼叫后端api但失敗。
以下是我的代碼:
來自后端的 dockerFile(監聽埠 3001)
FROM node:16.13-alpine
WORKDIR /backend/
COPY . .
ENV NODE_ENV=production
EXPOSE 3001
RUN npm install -g npm@8.3.0\
&& npm install\
&& npm install typescript -g\
&& npx tsc
CMD [ "node", "./build/index.js" ]
前端的dockerFile(監聽3000埠),api baseUrl是http://node:80/
FROM node:16.13-alpine
WORKDIR /frontend/
ENV PORT 3000
COPY package.json ./
COPY yarn.lock ./
RUN npm install
COPY . ./
EXPOSE ${PORT}
ENTRYPOINT ["npm", "run","dev"]
docker-compose.yml
version: "3.9"
services:
node:
env_file: "./back/.env"
volumes:
- ./test:/backend/test
- ./back/node_modules:/backend/src/node_modules
build:
context: ./back
ports:
- "80:3001"
expose:
- 80
networks:
- testNetwork
next:
depends_on:
- node
build:
context: ./front
ports:
- "3000:3000"
networks:
- testNetwork
volumes:
- ./front/node_modules:/frontend/src/node_modules
- ./test:/frontend/test
networks:
testNetwork:
我可以訪問 localhost:80 ,它正在作業。但是當我訪問 localhost:3000 時,next.js 出現如下錯誤
next_1 | Error: connect ECONNREFUSED 172.21.0.2:80
next_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
next_1 | type: 'Error',
next_1 | config: {
next_1 | transitional: {
next_1 | silentJSONParsing: true,
next_1 | forcedJSONParsing: true,
next_1 | clarifyTimeoutError: false
next_1 | },
next_1 | transformRequest: [ null ],
next_1 | transformResponse: [ null ],
next_1 | timeout: 0,
next_1 | xsrfCookieName: 'XSRF-TOKEN',
next_1 | xsrfHeaderName: 'X-XSRF-TOKEN',
next_1 | maxContentLength: -1,
next_1 | maxBodyLength: -1,
next_1 | headers: {
next_1 | Accept: 'application/json, text/plain, */*',
next_1 | 'User-Agent': 'axios/0.23.0'
next_1 | },
next_1 | baseURL: 'http://node:80/',
next_1 | method: 'get',
next_1 | url: 'getStaticPath/onlyCategoryRoute'
next_1 | },
next_1 | code: 'ECONNREFUSED',
next_1 | status: null
next_1 | }
謝謝你看
uj5u.com熱心網友回復:
您的應用程式正在嘗試連接到 docker 容器內的埠 80,但您的埠 80 僅在主機上可用,在 docker 容器內,它是埠 3001。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/390040.html
上一篇:Docker和RabbitMQ,洗掉后如何重新啟動服務
下一篇:當我在docker中運行時嘗試使用postgres在laravel中遷移時,我收到了一個錯誤,例如找不到驅動程式
