Search code examples
pythonmongodbdockerdocker-composemongo-express

MongoDB-Docker - How to insert item into mongo-express


I'm trying a simple docker-compose with mongo and mongo-express and trying to insert a document using Python script. The item is successfully added to mongodb, but does not show in mongo-express. Please help me.

compose.yaml

version: '3.0'
services: 
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - "27017:27017"

  mongo-express:
    image: mongo-express
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
    container_name: mongo-express
    ports: 
      - "8080:8081"

test.py

from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017')

# Connect to database
db = client['testing']

# Connect to collection
collection = db['testcollection']

# Insert document
try:
    collection.insert_one({"name": "John Doe", "age": 100})
    print("test complete")
except:
    print("there was an error")
else:
    print("Finished")
---

docker ps:

2aefafcbafa7   mongo           "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   0.0.0.0:27017->27017/tcp   mongodb      
a971ac83bade   mongo-express   "/sbin/tini -- /dock…"   16 minutes ago   Up 16 minutes   0.0.0.0:8080->8081/tcp     mongo-express

docker network inspect desktop_default:

[
    {
        "Name": "desktop_default",
        "Id": "5c29ea429da3644b4726a45b7a918eefe086c26bea58d75d761f7fcdb234eea6",
        "Created": "2024-09-01T08:35:42.61717286Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "2aefafcbafa716dadb624d4252d355e054bbaed45a1612e85e1c5cdf48c5ec44": {
                "Name": "mongodb",
                "EndpointID": "9430405d03ed9741bdd6c30afecbd881ee976fd96f7675fccd520d3c38fda899",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "a971ac83badeb40a2f531c8c42740ca8b9ce62047af6a40342887f151f4156d8": {
                "Name": "mongo-express",
                "EndpointID": "c6c8bbd4ecd90b88c94ee4cecd07eb40d9dc3c26c41f4ee3da51ed43b0543f61",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "desktop",
            "com.docker.compose.version": "2.29.1"
        }
    }
]

Im losing my mind over this.

Edit: when running docker logs , i get this. I assume its due to this issue

C:\Users\Amir\Desktop>docker logs  desktop-mongo-express-1
Waiting for mongodb:27017...
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:15 UTC 2024 retrying to connect to mongodb:27017 (2/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:16 UTC 2024 retrying to connect to mongodb:27017 (3/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:17 UTC 2024 retrying to connect to mongodb:27017 (4/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:18 UTC 2024 retrying to connect to mongodb:27017 (5/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:19 UTC 2024 retrying to connect to mongodb:27017 (6/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:20 UTC 2024 retrying to connect to mongodb:27017 (7/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:21 UTC 2024 retrying to connect to mongodb:27017 (8/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:22 UTC 2024 retrying to connect to mongodb:27017 (9/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
Sat Sep  7 04:09:23 UTC 2024 retrying to connect to mongodb:27017 (10/10)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 15: /dev/tcp/mongodb/27017: Connection refused
No custom config.js found, loading config.default.js
Welcome to mongo-express 1.0.2
------------------------
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
GET / 200 66.734 ms - 9260
GET /public/css/bootstrap-theme.min.css 200 10.696 ms - 23411
GET /public/css/bootstrap.min.css 200 7.435 ms - 121457
GET /public/css/style.css 200 13.766 ms - 1883
GET /public/img/mongo-express-logo.png 200 11.624 ms - 17847
GET /public/vendor-93f5fc3ae20e0dfd68cb.min.js 200 11.426 ms - 131153
GET /public/index-56afe067afbbbde795be.min.js 200 5.584 ms - 936
GET /public/img/gears.gif 200 3.367 ms - 50281
GET /public/fonts/glyphicons-halflings-regular.woff2 200 1.443 ms - 18028
GET / 200 43.773 ms - 9261
GET /public/css/bootstrap.min.css 304 2.468 ms - -
GET /public/css/bootstrap-theme.min.css 304 2.488 ms - -
GET /public/css/style.css 304 1.740 ms - -
GET /public/img/mongo-express-logo.png 304 1.456 ms - -
GET /public/vendor-93f5fc3ae20e0dfd68cb.min.js 304 1.839 ms - -
GET /public/index-56afe067afbbbde795be.min.js 304 1.045 ms - -
GET /public/img/gears.gif 304 0.866 ms - -

Solution

  • Just compared it with my setup, the only thing I made differently is to also set the username and password of a mongo user with privileges. That changes would reflect to something like this:

    version: '3.1'
    services:
    
      mongodb:
        image: mongo:latest
        restart: unless-stopped
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: mySecPW
        ports:
          - 27017:27017
        volumes:
          - ./db-data:/data/db
    
      mongo-express:
        image: mongo-express
        restart: unless-stopped
        depends_on:
          mongodb
        ports:
          - 6043:8081
        environment:
          ME_CONFIG_BASICAUTH_USERNAME: root
          ME_CONFIG_BASICAUTH_PASSWORD: mySecPW
          ME_CONFIG_MONGODB_ADMINUSERNAME: root
          ME_CONFIG_MONGODB_ADMINPASSWORD: mySecPW
          ME_CONFIG_MONGODB_URL: mongodb://root:mySecPW@mongodb:27017/
    

    Note the naming scheme of the URL line: mongodb://USERNAME:PASSWORD@HOSTNAME:PORT/

    Edit: Another thing that might lead to problems is the start order of the services. Docker compose starts services one at a time, seeing in the logs that it only tries 10 times, maybe express starts before the network itself. This can be done by adding the ˋdepends_onˋ section to your compose file.

    Something else to consider would be if the internal communication between the services even works. Try to open an interactive shell on the container mongo-express. Switch to the directory of your compose file and try the following:

    $ docker compose exec -it mongo-express /bin/bash
    bash-5.0# ping mongodb
    PING mongo (172.20.0.2): seq=0 ttl=64 time=0.11ms
    

    If the ping does not work there could be a general problem on your host with docker networking. There are two things which come to my mind immediately that can cause issues on docker hosts:

    • Check if there are other networks using the same network mask on your device using ˋip routeˋ. There you see all networks. In your case you are looking for something that overlaps 172.18.0.0/16
    • Check if you have some kind of firewall that is blocking your connection. Assuming that you are on a debian-based system, the most commonly used firewall is ufw. Try disabling your firewall by entering ˋsystemctl ufw stopˋ and then restarting your compose stack by running ˋdocker compose down && docker compose up -dˋ. If this made a difference you can either disable the firewall generally or follow this guide on how to configure it properly ( https://blog.jarrousse.org/2023/03/18/how-to-use-ufw-firewall-with-docker-containers/ ) depending on if this is a production or dev instance.