How do I secure and connect to mongodb in a container? - mongodb

I am trying to secure a mongoDB docker container and am stumped by the authentication.
So far, in my docker compose file, I am setting root environment variables, but I am unable to connect to the mongoDB from outside of the container. Without setting auth, I have no such issues.
services:
mongodb:
image: mongo:latest
restart: always
ports:
- "27017:27017"
command: ["--auth"]
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
I've tried to connect to mongodb://root:root#localhost:27017 using MongoDB Compass tool, but I keep getting authentication errors.
I am assuming that the root user is already created since I am getting authentication errors. I have tried using a script to create a user, but still I could not connect.

Try without command: ["--auth"]
Environment variables that you have provided should already turn this option on. Let me know, if that works.

Related

Trying to set up Mongodb as a Docker image to connect to a Sveltekit app through localhost REST API

I'm looking to build a Sveltekit app with Mongodb on the backend. I want to run mongodb as a docker image. I don't want to use Atlas or any any other db as a service type things. The issue is that almost all the tutorials I found online only show you how to use Mongodb integrated into an app with Atlas using a db connection string. I can't find that many tutorials on how to use Mongodb locally (or even just not associated with Atlas). I have this docker-compose file to build the Mongodb instance:
version: '3.7'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: mongoadmin
MONGO_INITDB_ROOT_PASSWORD: <PASSWORD HERE>
MONGO_INITDB_DATABASE: mongo1_0
ports:
- 27017:27017
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
And when I connect to it via Insomnia (which is similar to Postman) using an empty GET request to localhost:27017/, I get this: It looks like you are trying to access MongoDB over HTTP on the native driver port.
The good news is that I can connect to it. So how do I create a schema via an API call? Or Add, Delete, Update entries in this db via API requests? If I can figure out how to add, query, and edit entries, I can incorporate those requests into my Sveltekit app.

Error Running MongoDB via Docker Compose on ECS Fargate

I've been trying to launch a containerized mongoDB instance using docker-compose onto ECS w/ Fargate, here is my docker compose configuration:
mongo:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_USERNAME: root
MONGO_INITDB_DATABASE: db-name
volumes:
- ./migration/init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
ports:
- "27017:27017"
Using docker compose version : 3
I'm getting the following error: FATA[0000] ClientException: Fargate compatible task definitions do not support sourcePath
Any ideas on what I'm doing wrong?
You can't mount a local file/folder (.migration/init.js) to a task in the cloud. I am not familiar with Mongo so I am not 100% sure what that file does but the easiest way to solve this would be to create a new docker image FROM mongo where the only line would be a ADD to add the init.js file where you need it inside the container (/docker-entrypoint-initdb.d/mongo-init.js ?). The "even easier" way to solve this would be to eliminate the volume directive in the compose (but I don't know what the ramifications of doing so would be for the mongo container you need to run).

MongoDB connection problem with Docker container

Recently I'm using Docker and host it to our Dedicated Server everything is working fine,But Issue is we have made and absolute URL for mongodb here is our link below. I put mongodb URL in .env file
mongodb://<username>:<password>#<serverip>:27017/<dbname>?authSource=admin
This URL is accessible without docker, we have existing project in our server with this given URL and it is working. I did not use mongodb in our docker, it is installed globally in our server. Is there any suggestion ??
If you are trying to access the MongoDB from inside a container, replace the with the name of the service in you docker-compose file.
So if your docker-compose.yml looks like this
version: "2"
services:
mymongodb:
image: mongo
volumes:
- ./mongodb:/data/db:z
ports:
- 127.0.0.1:27017:27017
restart: always
your connection string looks like this: mongodb://<username>:<password>#mymongodb:27017/<dbname>?authSource=admin

Create database and user in mongoDB official docker image

I want to have a MongoDB service running in a Docker in order to serve a Flask app. What I've tried is create a container using docker-compose.yml:
my_mongo_service:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=${MY_DATABASE_NAME}
ports:
- "27017:27017"
volumes:
- "/data/db:/data/db"
command: mongod
Imagine we have an .env file like this:
MONGO_ROOT_USER=my_fancy_username
MONGO_ROOT_PASSWORD=my_fancy_password
MY_DATABASE_NAME=my_fancy_database
What I would expect (reading the doc) is that a database matching MY_DATABASE_NAME value is created and an user matching MONGO_ROOT_USER is created too and I could authenticate with the pair (MONGO_ROOT_USER,MONGO_ROOT_PASSWORD).
Ok, I launch my container with docker-compose up and enter on it with docker exec -it <container-id> bash. I put mongo on the console and when I try to authenticate it crashes:
> use my_fancy_database
switched to db my_fancy_database
> db.auth('my_fancy_username','my_fancy_password')
Error: Authentication failed.
0
On the log, the error I find is the following
[...] authentication failed for my_fancy_username on my_fancy_database from client [...] ; UserNotFound: Could not find user my_fancy_username#my_fancy_database
The docker-compose.yml configuration (as it was posted on official documentation) is not working. What I'm doing wrong?
Thanks in advance.
I don't get it. Are you using environmental variables, which are not in the environment? It sure looks so.
If you do echo $MY_DATABASE_NAME in your terminal and see empty output, then here is the answer to your question. You either first have to define the variable with export (or source for a file) or redefine your docker-compose.yml.
For that, it's best to use env_file directive:
my_mongo_service:
image: mongo
env_file:
- .env
ports:
- "27017:27017"
volumes:
- "/data/db:/data/db"
And set your .env as this:
MONGO_INITDB_ROOT_USERNAME=my_fancy_username
MONGO_INITDB_ROOT_PASSWORD=my_fancy_password
MONGO_INITDB_DATABASE=my_fancy_database
Side note: using command: mongod is not necessary, the base image is already using it.

Connect to a mongoDB session from within container

I'm new to learning how to use goLang to build microservices. I had a whole project up and running locally, but when I tried deploying it I ran into a problem. The session I was working with (mgo.Dial("localhost")) was no longer working. When I put this into a docker image, it failed to connect to the local host, which makes sense, since the docker image builds it over a new OS (alpine in my case). I was wondering what I should do to get it to connect.
To be clear, when I was researching this, most people wanted to connect to a mongoDB session that is a docker container, I want to connect to a mongoDB session from within a docker container. Also once I'm ready for deployment I'll be using StatefulSet with kubernetes if that changes anything.
For example, this is what I want my program to be like:
sess, err := mgo.Dial("localhost") //or whatever
if err != nil {
fmt.Println("failed to connect")
else {
fmt.Println("connected")
What I tried doing:
Dockerfile:
FROM alpine:3.6
COPY /build/app /bin/
EXPOSE 8080
ENTRYPOINT ["/bin/app"]
In terminal:
docker build -t hell:4 .
docker run -d -p 8080:8080 hell:4
And as you can expect, it says not connected. Also the port mapping is for the rest of the project, not this part.
Thanks for your help!
I think you should not try to connect to the MongoDB server running on your machine. Think about deploying the whole application lateron you want a MongoDB server running together with your service on some cloud or server.
That problem could be solved by setting up an additional container and link it to your Go Web App. Docker compose can handle this. Just place a docker-compose.yml file in the directory you are executing your docker build in.
version: '3'
services:
myapp:
build: .
image: hell:4
ports:
- 8080:8080
links:
- mongodb
depends_on:
- mongodb
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
- MONGODB_USER="user"
- MONGODB_PASS="pass"
Something like this should do it (not tested). You have two services: One for your app that gets build according to your Dockerfile in the directory in which you currently are. Additionally it links to a service called mongodb defined below. The mongodb service is accessible via the service name mongodb.
If your mongoDB server is running in your host machine, replace localhost by you host IP.