Reputation: 7
I am deploying a MongoDB image, and I want to make a custom image so that it has some data pre-imported. When I build the image, it shows that the files have been correctly imported:
> docker build -t mongoibdn -f mongodocker/Dockerfile .
...
Step 10/11 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db && dockerize -wait tcp://localhost:27017 -timeout 30s && ./resources/import_distances.sh
---> Running in f526a3759508
about to fork child process, waiting until server is ready for connections.
forked process: 9
child process started successfully, parent exiting
2024/05/28 23:48:25 Waiting for: tcp://localhost:27017
2024/05/28 23:48:25 Connected to tcp://localhost:27017
2024-05-28T23:48:25.537+0000 connected to: mongodb://localhost/
2024-05-28T23:48:25.597+0000 4696 document(s) imported successfully. 0 document(s) failed to import.
[ 'Origin_1_Dest_1' ]
{
_id: ObjectId('66566d491fd79c28cafda03f'),
Origin: 'GST',
Dest: 'JNU',
Distance: 41
}
Removing intermediate container f526a3759508
---> 0ee667e0f8f2
Step 11/11 : EXPOSE 27017
---> Running in 9b2daad9cf3c
Removing intermediate container 9b2daad9cf3c
---> aaba8043bc65
Successfully built aaba8043bc65
Successfully tagged mongoibdn:latest
The script import_distances.sh is:
#!/bin/bash
# Import our enriched airline data as the 'airlines' collection
mongoimport -d agile_data_science -c origin_dest_distances --file data/origin_dest_distances.jsonl
mongosh agile_data_science --eval 'db.origin_dest_distances.ensureIndex({Origin: 1, Dest: 1})'
mongosh agile_data_science --eval 'printjson(db.origin_dest_distances.findOne())'
However, when I deploy said image:
...
mongo:
image: mongoibdn
container_name: mongotainer
networks:
- app-tier
restart: always
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: agile_data_science
ports:
- 27017:27017
volumes:
- data-volume:/pcreativa/data
...
And enter mongosh to see whether the data is there:
user@userr:~/path$ docker exec -it mongotainer mongosh
Current Mongosh Log ID: 66566d638aa9b64adf2202d7
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB: 6.0.15
Using Mongosh: 2.2.5
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-28T23:48:44.317+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-28T23:48:44.434+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-28T23:48:44.434+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2024-05-28T23:48:44.434+00:00: vm.max_map_count is too low
------
test> show dbs
admin 8.00 KiB
config 12.00 KiB
local 8.00 KiB
As you see, the db "agile_data_science" isn't there. There has been times where the data was there, but it happens seemingly randomly.
Please help me with this as I really do not know what is causing this isuee.
I have tried to substitue the RUN with a CMD so that the import happens when the service is deployed, and not when the image is being built. This didn't work.
Upvotes: 0
Views: 33