I have been having problems using mongodb as a service in docker-compose. I have already managed to insert data into its container (csv, txt and json files, for instance) at data/db/ through a volume folder, but I still can`t use it as a collection. I believe I should use the function mongoimport, but it fail to connect to the host. All seem to be fine but the host. I have tried a lot of other addresses as hosts, but none have worked.
Bellow is my docker-compose.yml
version: '3'
services:
mongodb:
image: mongo:3.6
container_name: mongodb
restart: on-failure
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=example
volumes:
- ./mongodbvol:/data/db
ports:
- "27107:27107"
command:
mongoimport --host http://0.0.0.0:27017 -c col1 --type csv --file /data/db/tt.csv --headerline
mongo-express:
image: mongo-express
container_name: mongo-express
restart: on-failure
environment:
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=example
- ME_CONFIG_BASICAUTH_USERNAME=necode
- ME_CONFIG_BASICAUTH_PASSWORD=passwd
ports:
- "8081:8081"
depends_on:
- mongodb
rshiny:
image: guigo13/app
container_name: rshiny
restart: on-failure
command: R -e "shiny::runApp('R/app.R', host = '0.0.0.0', port = 3838)"
environment:
- DB_URI=mongodb
- DB_USERNAME=root
- DB_PASSWORD=example
- DB_NAME=appdb
- DB_HOST=mongodb
- DB_PORT=27017
ports:
- "3838:3838"
- "3839:3838"
depends_on:
- mongodb
and here is the error from the logs
mongodb | 2021-10-29T02:51:09.170 0000 [########################] test.col1 11B/11B (100.0%)
mongodb | 2021-10-29T02:51:09.685 0000 [########################] test.col1 11B/11B (100.0%)
mongodb | 2021-10-29T02:51:09.685 0000 Failed: error connecting to db server: no reachable servers
mongodb | 2021-10-29T02:51:09.685 0000 imported 0 documents
Could someone, please, get me an alternative? Thanks in advance.
CodePudding user response:
The problem is you are overriding the default CMD for the mongo image (which is running the db service - CMD ["mongod"]). This makes it impossible for the container to connect to itself). Also I think the default port for mongo is 27017, and not 27107.
I've seen people using a separate container to seed the data to the database, where all it does - it runs the mongoimport to populate the data and then dies. You could try thi approach. Check this answer, this may give you an idea.
CodePudding user response:
If you want to import it just once, I would advice to create an own image:
Dockerfile:
FROM mongo:versiontag
ENV MONGO_INITDB_ROOT_USERNAME root
ENV MONGO_INITDB_ROOT_PASSWORD example
ENV MONGO_INITDB_DATABASE mydb
COPY mongodb.js /docker-entrypoint-initdb.d/
RUN mongod --fork --logpath=/tmp/mongodb.log && sleep 20 && \
mongoimport -c=users -d=acrc --mode=upsert --file=/opt/acrc/config/users.json && \
mongoimport -c=tasks -d=acrc --mode=upsert --file=/opt/acrc/config/tasks.json && \
mongoimport -c=configs -d=acrc --mode=upsert --file=/opt/acrc/config/configs.json && \
mongoimport -c=agentGroupConfigs -d=acrc --mode=upsert --file=/opt/acrc/config/agentGroupConfigs.json && \
mongoimport -c=col1 --type=csv --file=/data/db/tt.csv --headerline
It is firing up the mongo while creating the container and fills it then.
in the mongodb.js you can script additional mongosh
commands, like adding users, dbs or insert data and so on.