[英]Error: connect ECONNREFUSED 0.0.0.0:8000 when hitting Docker containerised Node.js app endpoint
I'm just starting with Docker and dough I succeed in creating an image and a container from it I'm not succeeding in connecting to the container's port with postman and I get Error: connect ECONNREFUSED 0.0.0.0:8000
.我刚从Error: connect ECONNREFUSED 0.0.0.0:8000
和面团开始,我成功地从中创建了一个图像和一个容器
In my server.js file I have:在我的 server.js 文件中,我有:
const app = require('./api/src/app');
const port = process.env.PORT || 3000; // PORT is set to 5000
app.listen(port, () => {
console.log('App executing to port ', port);
});
in my index.js I have:在我的 index.js 中,我有:
const express = require('express');
const router = express.Router();
router.get('/api', (req, res) => {
res.status(200).send({
success: 'true',
message: 'Welcome to fixit',
version: '1.0.0',
});
});
module.exports = router;
so if I run my app with either npm start
or nodemon server.js
the localhost:3000/api
endpoint works as expected.因此,如果我使用npm start
或nodemon server.js
运行我的应用程序,则localhost:3000/api
端点将按预期工作。
I then build a docker image for my app with the command docker build. -t fixit-server
然后,我使用命令docker build. -t fixit-server
docker build. -t fixit-server
with this Dockerfile
: docker build. -t fixit-server
与此Dockerfile
:
FROM node:15.14.0
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 5000
# CMD ["npm", "start"]
CMD npm start
# CMD ["nodemon", "server.js"]
and run the container with the command docker run -d -p 8000:5000 --name fixit-container fixit-server tail -f /dev/null
并使用命令docker run -d -p 8000:5000 --name fixit-container fixit-server tail -f /dev/null
and listing the containers with docker ps -a
shows it running:并使用docker ps -a
列出容器显示它正在运行:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da0e4ef12402 fixit-server "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 0.0.0.0:8000->5000/tcp fixit-container
but when I hit the endpoint 0.0.0.0:8000/api
I get the ECONNREFUSED error.但是当我到达端点0.0.0.0:8000/api
时,我得到了 ECONNREFUSED 错误。 I tried both CMD ["npm", "start"]
and CMD npm start
but I get the error both ways.我尝试了CMD ["npm", "start"]
和CMD npm start
但两种方式都出现错误。 Can you what I'm doing wrong?你能知道我做错了什么吗?
@Vincenzo was using docker-machine
and to be able to check whether the app was working properly, we needed to execute the following command in the terminal: @Vincenzo 正在使用 docker docker-machine
并且能够检查应用程序是否正常运行,我们需要在终端中执行以下命令:
docker-machine env
The result was:结果是:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.102:2376"
export DOCKER_CERT_PATH="/Users/vinnytwice/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
Then based on the DOCKER_HOST
value, we hit 192.168.99.102:8000/api
and it was working.然后根据DOCKER_HOST
值,我们点击192.168.99.102:8000/api
并且它正在工作。
I believe the problem is you're never setting the PORT
environment variable to 5000
.我相信问题是您永远不会将PORT
环境变量设置为5000
。
EXPOSE
docker command is a no op. EXPOSE
docker 命令是无操作。 Meaning that it will do nothing but is only for the developer to know that you're exposing the port 5000. You can read it in Docker documentation .这意味着它什么也不做,只是让开发人员知道您正在暴露端口 5000。您可以在Docker 文档中阅读它。
You need to either set an environment variable or pass an environment variable at runtime to the container to specifically tell it that PORT
is 5000.您需要设置环境变量或在运行时将环境变量传递给容器,以明确告诉它PORT
是 5000。
You can change your Dockerfile like below:您可以更改您的 Dockerfile,如下所示:
FROM node:15.14.0
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
ENV PORT=5000
EXPOSE $PORT
# CMD ["npm", "start"]
CMD npm start
# CMD ["nodemon", "server.js"]
Simply use the following command to run your container:只需使用以下命令来运行您的容器:
docker run -d -p 8000:5000 --name fixit-container --env PORT=5000 fixit-server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.