No internal docker IP address when I run mongdb image - mongodb

I'm starting a container from an official mongodb image using command
sudo docker run -d --name mongodb mongo
Other containers I spin get IP address but not mongo 
When I run sudo docker inspect mongodb all the fields are blank
I'm running it on Ubuntu with virtual box and network interface is set to NAT
This is the output from inspect command.
{
"NetworkSettings":{
"Bridge":"",
"SandboxID":"16b808df46537e04ab2bf96e05dc41fd4660a270c927634c2a94a1639d32f693",
"HairpinMode":false,
"LinkLocalIPv6Address":"",
"LinkLocalIPv6PrefixLen":0,
"Ports":{
},
"SandboxKey":"/var/run/docker/netns/16b808df4653",
"SecondaryIPAddresses":null,
"SecondaryIPv6Addresses":null,
"EndpointID":"",
"Gateway":"",
"GlobalIPv6Address":"",
"GlobalIPv6PrefixLen":0,
"IPAddress":"",
"IPPrefixLen":0,
"IPv6Gateway":"",
"MacAddress":"",
"Networks":{
"bridge":{
"IPAMConfig":null,
"Links":null,
"Aliases":null,
"NetworkID":"f66bff6e962312af4d9af54ed9e5ba337d3d9466a5702ae8430660bfda690833",
"EndpointID":"",
"Gateway":"",
"IPAddress":"",
"IPPrefixLen":0,
"IPv6Gateway":"",
"GlobalIPv6Address":"",
"GlobalIPv6PrefixLen":0,
"MacAddress":"",
"DriverOpts":null
}
}
}

I solved my problem, however don't know is it a bug or my lack of knowledge
So far I was trying with mongo image (:latest)
I started trying older versions.
Went with mongo:focal - same result, no IP.
However, when I tried with mongo: 4.4.6-bionic
Everything went fine and I have IP assigned to the mongodb container :)

Try running it directly on Ubuntu with Docker, but without virtualbox.
You could try run it like this:
docker run -d -p 27017:27017 --name mongodb dockerfile/mongodb
Alternatively you can try run it with a docker-compose.yml file.
Put this in docker-compose.yml:
version: '3'
services:
mongodb-service:
image: mongo
ports:
- 27017:27017
restart: always
Then run:
docker-compose up
You might want to think of defining some volumes for persistent storage of your data too, but later on once you've solved this issue.

Related

Connecting to Local MongoDB from Containerized Go Application

I've followed the instructions in https://tsmx.net/docker-local-mongodb/ but I still get the following error:
**panic: unable to connect to MongoDB (local): no reachable servers
**
I even tried the following but still get the same error:
_ = pflag.String("mongodb-addr", "127.0.0.1:27017", "MongoDB connection address")
My connection code is as follows:
dbAddr := d.cfg.GetString("mongodb-addr")
session, err := mgo.Dial(dbAddr)
And my docker run command is as follows:
docker run image_name
I'm using macOS Monterey. Any help would be greatly appreciated. Thanks.
If the application and the MongoDB are on the same docker network, then use the docker name to connect to the MongoDB container.
If the MongoDB is running in the server where the application is running in docker container, then use the IP of the server to communicate to the MongoDB. 127.0.0.1 from within the container will try to find the MongoDB within the same Docker as the application.
if you run mongo like this :
mongo:
image: mongo
restart: always
volumes:
- ./mongo-data:/data/db
env_file: .env
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
then you can connect from Go like this :
var cred options.Credential
cred.Username = MongoUsername
cred.Password = MongoPassword
clientOption := options.Client().ApplyURI(mongodb://mongodb:27017).SetAuth(cred)
I was facing the same issue and this command did the trick for me, was mentioned here.
Docker provides a host network which lets containers share your host’s networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself.
docker run -d --network=host my-container:latest
hope it help someone.

Creating mongo docker container with local storage on hos

I want to run mongo db in docker container. I've pulled image and run it. So it seems work ok.
But every time I start it the DB is overwritten so I loose any changes. So I want to want to map somehow internal container storage on my local host folder.
Should I write Dockerfile or/and docker-compose.yaml? I suppose this is simple question but being new in docker I can't understand what to read to get full understanding.
You do not need to write Dockerfile and make thing complex, just use offical image as mentioned in command or compose file.
You can use both options either docker run or docker-compose but the path should be correct in mapping to keep data persistent.
Here is way
Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir.
Start your mongo container like this:
$ docker run --name some-mongo -v /my/own/datadir:/data/db -d mongo
The -v /my/own/datadir:/data/db part of the command mounts the
/my/own/datadir directory from the underlying host system as /data/db
inside the container, where MongoDB by default will write its data
files.
mongo docker volume
with docker-compose
version: "2"
services:
mongo:
image: mongo:latest
restart: always
ports:
- "27017:27017"
environment:
- MONGO_INITDB_DATABASE=pastime
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root_password
volumes:
- /my/own/datadir:/data/db

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.

Expose mongo port in other container

I have this (custom) container which runs a java program which requires mongo locally. Now, with docker I would like to setup mongo in its own container. So I guess, in order to expose this 27017 port locally in this java-container I need to setup an SSH-tunnel, right ? If there is a easier way please let me know.
So, there is this official mongo image image, but I get the impression ssh is not install or running. What would be the best approach to do this?
UPDATE: I've rephrased the question more focussed on port-forwarding here
You have to make your container run on the same network. No need to ssh into your mongo or app container.
https://docs.docker.com/engine/userguide/networking/
First define a network
docker network create --driver bridge isolated_nw
Start you containers using that newly network
docker run -p 27017:27017 --network=isolated_nw -itd --name=mongo-cont mongo
docker run --network=isolated_nw -itd --name=app your_image
The image of mongo includes EXPOSE 27017 so from your app container, you should be able to access to the mongo container using its name mongo-cont
You can build your custom image on top of mongodb official image, which gives you the flexibility to install additional required packages.
FROM mongo:latest
RUN apt-get install ssh
Also try to use docker-compose to build and link your containers together, it will ease the process greatly.
version: '2'
services:
mongo:
image: mongo:latest
ports:
- "27017"
custom_project:
build:
context: . # Parent directory address of Dockerfile
dockerfile: Dockerfile-Custom # Name of Dockerfile
command: /root/docker-entrypoint.sh
This is the image used for mongodb official image.
You are trying to SSH into your container to gain access to it, but that isn't how you connect. Docker provides functionality to securely connect via the following methods.
Connect into a running container - Docs:
docker exec -it <container name> bash
$ root#665b4a1e17b6:/#
Start a container from image, and connect to it - Docs:
docker run -it <image name> bash
$ root#665b4a1e17b6:/#
Note: If it is an Alpine based image, it may not have Bash installed. In that case using sh instead of bash in your commands should work. Mongo's Dockerfile looks to use debian:jessie which will have bash support.

repair command in mongodb in docker container

My client has mongodb running in docker container, one morning the db crashed and logs says "unclean shutdown detected". I tried for repairing mongodb but not able to do as it is running under docker container. The reason is that as i have to shutdown mongodb for running repair command and once i shutdown the mongodb then docker throws me out of the container because only one process was running and as it was stopped container was also stopped, now i am not able to run any mongod commands as i am out of container and container is stopped along with mongod instance.
So, can anyone help me out of this, how can i run repair command from outside of docker container ?
Can i run repair command without stopping mongod ?
Please help !!!
Thanks
Irshad
The example command below fixed my issue with corrupted Wt files.
docker run -it -v <path>:/data/db <image-name> mongod --repair
Just replace path and image-name with yours
I had this same thing when my Mac decided to restart and install an update this morning, grr.
Normally my mongo container is started with a docker-compose up command, which uses a docker-compose.yml file for all the settings, something like...
version: "3"
services:
mongo:
image: mongo:3.6.12
container_name: mongo
ports:
- 27017:27017
volumes:
- ~/mongo/data/db:/data/db
- ~/mongo/db/configdb:/data/configdb
To run a basic docker command with those settings I did...
# Kill any existing mongo containers
docker rm mongo
# Repair the db
docker run -it -v ~/mongo/data/db:/data/db -v ~/mongo/db/configdb:/data/configdb --name mongo -d mongo:3.6.12 mongod --repair
# Has it finished the repair yet?
docker ps -a
After about a minute the container exited, the database was repaired and I was able to run mongo using docker-compose again.
version: '3.9'
services:
mongo:
image: mongo
command: --repair
This is how we pass the --repair in docker-compose.