admin管理员组

文章数量:1123220

This is the docker-compose:

services:
      backend:
          container_name: backend
          image: python:3.9-slim
          working_dir: /app
          command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
          volumes:
            - ./api:/app
          ports:
            - "8000:8000"
          networks:
            - app-network
          environment:
              - MONGO_URI
              - SECRET_KEY
              - ALGORITHM
              - TOKEN_EXPIRE_MINUTES
              - ENCRYPTION_KEY
              - MONGO_INITDB_DATABASE
          env_file:
              - .env
          depends_on:
            - mongo
          build:
            context: ./api
            dockerfile: Dockerfile
    
    mongo:
        image: mongo:5.0
        container_name: mongo
        ports:
          - "28000:27017"
        volumes:
          - ./mongo_data:/data/db
        environment:
          - MONGO_INITDB_ROOT_USERNAME
          - MONGO_INITDB_ROOT_PASSWORD
        env_file:
          - .env
        restart: always
    
    networks:
      app-network:
        driver: bridge

And this is the .env:

PYTHONUNBUFFERED=1
MONGO_URI=mongodb://adminuser:123asdfAaJKIhj@mongo:27017/
SECRET_KEY=asdfSD980SDadsfhu
ALGORITHM=HS256
TOKEN_EXPIRE_MINUTES=30
ENCRYPTION_KEY=B3hFQk1CUnpKcGxFUVFvR1pDSXU2dGlqYjczZ3R0d0E=
MONGO_INITDB_ROOT_USERNAME=adminuser
MONGO_INITDB_ROOT_PASSWORD=123asdfAaJKIhj
MONGO_INITDB_DATABASE=scrapmind_db

When I try to connect to db container I recived the following error

raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: scrapmindmongo:27017: [Errno -2] Name or service not known (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 67813bd5f71ffae59e979903, topology_type: Unknown, servers: [<ServerDescription ('scrapmindmongo', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('scrapmindmongo:27017: [Errno -2] Name or service not known (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 
20000.0ms)')

Also when I try to connect to mongodb://adminuser:123asdfAaJKIhj@localhost:28000/ using mongoDB Compass this returns Authentication failed. Mongo container it's up without errors. Any advice?

本文标签: mongodbHow to connect Mongo Container to another in DockerStack Overflow