'Mongo import json file in mongo container from different container
Here I have created two different conatiner mongodb and mongo_seed the mondo_seed container executed the scripts and goes to exit state.
version: '3'
services:
mongodb:
image: mongo:4.4
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
# Provide the admin password to be used for authentication
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
volumes:
- ./mdata/mongo:/data/db
mongo_seed:
image: mongo:4.4
links:
- mongodb
volumes:
- ./mongo-seed:/mongo-seed
command:
/mongo-seed/import.sh
Inside mongo-seed folder mongo-seed import.sh location.json
import.sh file contents
#! /bin/bash
mongoimport --host mongodb --db test --collection location --type json --file /mongo-seed/location.json --jsonArray
Every thing works file if I don't add env variables.
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
# Provide the admin password to be used for authentication
- MONGO_INITDB_ROOT_PASSWORD=admin
I have also tried multiple commands in the import.sh file like
mongoimport --host localhost:27017 --db test -u admin -p admin --collection location --type json --file /mongo-seed/location.json --jsonArray
mongoimport --db test --collection location --authenticationDatabase admin --username admin --password admin --drop --file /mongo-seed/location.json --jsonArray
mongoimport --host mongodb --db test --collection location --authenticationDatabase admin --username admin --password admin --type json --file /mongo-seed/location.json --jsonArray
But comes with a error authentication failed.
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
I have tried to load the json data to mongo db container but failed. I am expecting a way to load only the json data in the mondodb container as when the conatier startes. My data is only available in a JSON file and so it won't work to bind-mount a Javascript file into the container.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
