Docker撰写连接被拒绝

时间:2020-09-26 16:04:25

标签: node.js docker docker-compose

当我尝试运行在Docker容器中的NodeJS服务器时,出现“拒绝连接”错误。如果我尝试从主机上卷曲服务器的URL,则会收到错误“卷曲:(56)接收失败:对等重置连接”。

运行sudo docker-compose up -d会启动我的服务,运行sudo docker ps -a会显示以下内容:

CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS                    NAMES
e56b30b1de9c        mongo:4.1.8-xenial   "docker-entrypoint.s…"   About a minute ago   Up About a minute   27017/tcp                db
0d82b0a881e5        nodejs               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3000->3000/tcp   nodejs

这些容器在IP为192.168.0.24的服务器上运行,因此我尝试通过192.168.0.24:3000/items在“ nodejs”应用中命中一个端点,但这会导致连接被拒绝错误。 / p>

docker-compose.yml

version: '3'

services:
  nodejs:
    build:
      context: .
      dockerfile: Dockerfile
    image: nodejs
    container_name: nodejs
    restart: unless-stopped
    env_file: .env
    environment:
      - MONGO_USERNAME=$MONGO_USERNAME
      - MONGO_PASSWORD=$MONGO_PASSWORD
      - MONGO_HOSTNAME=db
      - MONGO_PORT=$MONGO_PORT
      - MONGO_DB=$MONGO_DB
    ports:
      - "3000:3000"
    volumes:
      - .:/home/node/app
      - /home/node/app/node_modules
    networks:
      - app-network
    command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js
  db:
    image: mongo:4.1.8-xenial
    container_name: db
    restart: unless-stopped
    env_file: .env
    environment:
      - MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
      - MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
    volumes:
      - dbdata:/data/db
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  dbdata:
  node_modules:

Dockerfile

FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 3000
CMD [ "npm", "app.js" ]

0 个答案:

没有答案