Docker:尝试使用docker

时间:2019-12-04 17:57:07

标签: node.js mongodb docker docker-compose

因此,我尝试按照此tutorial在远程linux服务器(数字海洋小滴)上使用docker部署非常基本的Node.js应用程序。我对与部署相关的东西还很陌生,所以我肯定在做一些基本的错误。请多多包涵我。

我正在使用的项目和docker,docker-compose配置文件的代码是:

.env

MONGO_USERNAME=sammy
MONGO_PASSWORD=password
MONGO_DB=sharkinfo
PORT=8080
MONGO_PORT=27017
MONGO_HOSTNAME=127.0.0.1

Dockerfile

FROM node:10

# 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 ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "src/index.js"]

docker-compose.yml

version: '3'

services:
  nodejs:
    build: .
    env_file: .env
    environment:
      - MONGO_USERNAME=$MONGO_USERNAME
      - MONGO_PASSWORD=$MONGO_PASSWORD
      - MONGO_HOSTNAME=myDB
      - MONGO_PORT=$MONGO_PORT
      - MONGO_DB=$MONGO_DB
    ports:
      - "80:8080"
    networks:
      - app-network
  myDB:
    image: mongo:4.1.8-xenial
    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:

mongoose.js

const mongoose = require('mongoose');

const {
  MONGO_USERNAME,
  MONGO_PASSWORD,
  MONGO_HOSTNAME,
  MONGO_PORT,
  MONGO_DB
} = process.env;

const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;

mongoose.connect(url, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false
});

然后,我使用命令(按顺序)-docker-compose build docker-compose up -d创建并运行了docker镜像。 节点和mongoDB服务器都显示在正在运行的docker进程列表中。但是,当我做docker logs node-application时,出现错误:

Server is up on port 8080
(node:1) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mydb:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.19.0.3:27017]
    at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
    at Pool.emit (events.js:198:13)
    at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:286:20)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我尝试对代码进行各种调整,但均未成功,并且仍然会出现相同的错误。这里可能是什么问题?

0 个答案:

没有答案