[英]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.