简体   繁体   English

MERN 堆栈无法将数据库连接到 Docker 容器

[英]MERN stack cannot connect to database to Docker Container

Good day... regarding for integration the MERN stack web application on docker using dockerfiles and docker-compose.yml.. the localhost:8000 is where my backend, also localhost:3000 is where my frontend ports..and i'm running it on locally.. the credentials on mongo is correct..then i put networks in docker-compose and driver: "bridge", there are on the same network.. after i build container and running the application on docker.. the error stated below:美好的一天...关于使用 dockerfiles 和 docker-compose.yml 将 MERN 堆栈 Web 应用程序集成到 docker 上.. localhost:8000 是我的后端,localhost:3000 也是我的前端端口......我正在运行它在本地.. mongo 上的凭据是正确的.. 然后我将网络放入 docker-compose 和驱动程序:“bridge”,在同一网络上.. 在我构建容器并在 docker 上运行应用程序后.. 错误如下所述:

[0] [http-server] listening: http://localhost:8000/
[1] ℹ 「wds」: Project is running at http://172.20.0.3/
[1] ℹ 「wds」: webpack output is served from
[1] ℹ 「wds」: Content not from webpack is served from /maint/client/public
[1] ℹ 「wds」: 404s will fallback to /
[1] Starting the development server...
[1]
[0] connect ECONNREFUSED 127.0.0.1:27017
[0] ERROR: Cannot connect to the database
[0] Exiting now...
[0] [nodemon] app crashed - waiting for file changes before starting...

my dockerfiles :我的 dockerfiles :

# get the base node image
FROM node:10.15.2

# set the working dir for container
WORKDIR /maint

# copy the json file first
COPY ./package.json /maint

RUN "npm ci"

# copy other project files
COPY . .

# build the folder
CMD [ "npm", "run", "start" ]

and inside of my docker-compose-local.yml在我的 docker-compose-local.yml 里面

version: '3'
services:
    maint:
      build:
        context: .
        dockerfile: Dockerfile.dev
      command: npm run dev
      container_name: mat-docker
      volumes:
        - ./:/maint
        - /maint/node_modules
      ports:
        - "3000:3000"
      depends_on:
        - mongo
      networks:
        - app-network
    mongo:
      image: mongo
      container_name: mat-dockerdb
      ports:
        - "27017:27017"
      env_file: .env
      environment:
        - MONGO_HOST:$MONGO_HOST
        - MONGO_DB:$MONGO_DB
        - MONGO_USERNAME:"admin"
        - MONGO_PASSWORD:"password123"
      networks:
        - app-network

networks:
  app-network:
    driver: bridge

You can do this much easier by just using the host network.只需使用主机网络,您就可以更轻松地做到这一点。 This way your main service and mongo instance use the same host network as your backend.这样,您的主服务和 mongo 实例使用与后端相同的主机网络。 Here is an example of a docket compose app to do this这是执行此操作的 docket compose 应用程序的示例

 version: "2"
 services:
 app:
    container_name: app
    network_mode: host
    restart: always
    build: .
    ports:
      - "3000:3000”
    depends_on:
      - mongo
 mongo:
    container_name: mongo
    image: mongo
    network_mode: host
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka js 容器无法连接到同一 docker 堆栈上的 kafka 容器 - Kafka js container cannot connect to kafka container on the same docker stack docker 容器中的服务器无法连接到另一个 docker 容器中的 postgres 数据库 - Server in docker container unable to connect to postgres database in another docker container 无法使用 docker 容器连接到 MySQL 数据库 - Cannot connect to MySQL db using docker container 系统能够连接到 mysql docker,但其他 docker 容器不能 - System able to connect to mysql docker, but other docker container cannot MERN堆栈,REST API邮递员,无法获取/ - MERN stack, REST api Postman, Cannot GET / MERN Stack:使用Express / React将数据发布到数据库 - MERN Stack: POSTing data to database with Express/React 第一次尝试时无法将节点连接到 docker 容器中的 mongoDB - Cannot connect Node to mongoDB in docker container on first try 如何在 K8S 中正确定义 MERN 堆栈容器的变量? - How to correctly define variables for a container of MERN stack in K8S? 将nodejs(本地)连接到docker容器上运行的cassandra数据库 - Connect nodejs (local) to cassandra database running on docker container 后端容器无法连接到数据库 - Sequelize、Node、Postgres、Docker - Backend container can't connect to database - Sequelize, Node, Postgres, Docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM