skip to Main Content

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.

2

Answers


  1. You can enter the MongoDB container and use the MongoDB shell to check if the data is in the correct database and collection:

    docker exec -it mongodb mongo
    use testing
    db.testcollection.find().pretty()
    

    This will show you the documents in the testcollection.

    Login or Signup to reply.
  2. 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
        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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search