简体   繁体   中英

How to connect the services inside docker-compose

This is my docker-compose file, I cannot connect to the database

version: "3.9"
services:
  mongodb:
    image: mongo:5.0.2
    container_name: armaghan_db
    restart: unless-stopped
    networks:
      - armaghan-network
    ports:
      - 27017:27017
    volumes:
      - db:/data/db
  backend:
    container_name: armaghan_backend
    depends_on:
      - mongodb
    build:
      context: .
      dockerfile: "Dockerfile"
    ports:
      - "5001:5000"
    networks:
      - armaghan-network
    restart: unless-stopped
    environment:
      PORT: 5000
      armaghan_jwtPrivateKey: privatekeyarmaghansoroushmadani
      armaghan_db: mongodb://localhost:27017/armaghan
    stdin_open: true
    tty: true
volumes:
  db:
networks:
  armaghan-network:
    driver: bridge

the mongodb image is running, I can make a connection to it when I run the application without docker. but connection fails inside docker.

You have to connect to port 5000 instead of 5001 , because your database and your backend running in a separated network. 5001 is the forwarded port, and can only connect from outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM