Hello Guys,
I wrote a Dockerfile which takes the mongo image as a base image and copys 2 scripts. When I run them seperately everything works fine, but it doesn’t with the dockerfile.
Dockerfile:
FROM mongo
# Install Python 3, Pip, Pandas, and PyMongo
RUN apt-get update && apt-get install -y python3 python3-pip \
&& python3 -m pip install --no-cache-dir pandas pymongo
# Kopieren Sie das Init-Script, das Python-Skript und das CSV-Datei in den Container
COPY init-db.sh /docker-entrypoint-initdb.d/
COPY main.py /
COPY iot_telemetry_data.csv /
# Definieren Sie die Standard-Command für den Container
CMD ["bash", "-c", "mongod --fork --logpath /var/log/mongodb.log && python3 main.py"]
bash script:
#!/bin/bash
# Führen Sie die Initialisierungsaufgaben aus, z. B. das Erstellen der Datenbank und Sammlung
mongo portfolio_database --eval 'db.createCollection("co2_data")'
python script :
# importing the needed libraries
import pandas as pd
from pymongo import MongoClient
# Connecting to the MongoDB Database
client = MongoClient("localhost", 27017)
db = client["portfolio_database"]
collection = db["co2_data"]
#Reading CSV File and storing it to the collection
chunk_size = 500
for chunk in pd.read_csv("iot_telemetry_data.csv", chunksize=chunk_size):
# Daten in MongoDB einfügen
records = chunk.to_dict(orient="records")
collection.insert_many(records)
print("Import completed.")
Everything is saved in the same directory. I tried to force the container to keep running, but when I do that connecting to the database is not possible… Even tried to debug with the help of ChatGPT, but it can’t find a solution…