简体   繁体   中英

docker-compose and connection to Mongo container

I am trying to create 2 containers as per the following docker-compose.yml file. The issue is that if I start up the mongo database container and then run my code locally (hitting 127.0.0.1) then everything is fine but if I try and run my api container and hit that (see yml file) then I get connection refused ie

172.29.0.12:27117: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 60437a460a3e0fa904650e35, topology_type: Single, servers: [<ServerDescription ('172.29.0.12', 27117) server_type: Unknown, rtt: None, error=AutoReconnect('172.29.0.12:27117: [Errno 111] Connection refused')>]>

Please note: I have set mongo to use port 27117 rather than 27017 My app is a Python Flask app and I am using PyMongo in the following manner:

    try:
        myclient = pymongo.MongoClient('mongodb://%s:%s@%s:%s/%s' % (username, password, hostName, port, database))
        mydb = myclient[database]
        cursor = mydb["temperatures"]
        app.logger.info('Database connected to: ' + database)  
    except:
        app.logger.error('Error connecting to database')

What's driving me mad is it runs locally and successfully accesses mongo via the container, but as soon as I try the app in a container it fails.

docker-compose.yml as follows:

version: '3.7'
services:
  hotbin-db:
    image: mongo
    container_name: hotbin-db
    restart: always
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '27117:27017'
    expose:
      # Opens port 3306 on the container
      - '27117'
    command: [--auth]
    environment:
      MONGO_INITDB_ROOT_USERNAME: ***
      MONGO_INITDB_ROOT_PASSWORD: ***
      MONGO_INITDB_DATABASE: ***
      MONGODB_DATA_DIR: /data/db
      MONDODB_LOG_DIR: /dev/null
      # Where our data will be persisted
    volumes:
      - /home/simon/mongodb/database/hotbin-db/:/data/db
        #- my-db:/var/lib/mysql
    # env_file:
    #   - .env
    networks:
      hotbin-net:
        ipv4_address: 172.29.0.12

  hotbin-api:
    image: scsherlock/compost-api:latest
    container_name: hotbin-api
    environment:
      MONGODB_DATABASE: ***
      MONGODB_USERNAME: ***
      MONGODB_PASSWORD: ***
      MONGODB_HOSTNAME: 172.29.0.12
      MONGODB_PORT: '27117'
    depends_on: 
      - hotbin-db
    restart: always
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '5050:5050'
    expose:
      - '5050'
    networks:
      hotbin-net:
        ipv4_address: 172.29.0.13  

# # Names our volume
volumes:
  my-db:  

networks:
  hotbin-net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.29.0.0/16

Using the service name of the mongo container and the standard port of 27017 instead of 27117 (even though that's what is defined in the docker-compose file) works. I'd like to understand why though

Your docker compose file does NOT configure MongoDB to run on port 27117. If you want to get it to run on 27117 you would have to change this line in the docker compose:

command: mongod --auth --port 27117

As you haven't specified a port, MongoDB will run on the default port 27017.

Your expose section exposes the container port 27117 to the host, but Mongo isn't running on that port, so that line is effectively doing nothing.

Your ports section maps a host port 27117 to a container port 27017. This means if you're connecting from the host , you can connect on port 27117, but that is connecting to port 27017 on the container .

Now to your python program. As this is running in the container network, to connect services within a docker-compose network, you reference them by their service name.

Putting this together, your connection string will be: mongodb://hotbin-db:27017/yourdb?<options>

As others have mentioned, you really don't need to create specific IP addresses unless you have a very good need to . You also don't even to define a network, as docker-compose creates it's own internal network.

Reference: https://docs.docker.com/compose/networking/

Are you using Windows to run the container? If yes, localhost is identified as localhost of the container and not the localhost of your host machine.

Hence, instead of providing the IP address of your host, try modifying your mongodB string this way when running inside the docker container:

Try this: mongodb://host.docker.internal:27017/ instead of: mongodb://localhost:27017/

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