Trying to use the mgo.v2 package to connect to the mongodb server. I have started the server using:
mongod --auth
I am able to connect to the server using the terminal using:
$ mongo -u "username" -p "password" --authenticationDatabase "db"
But when I use:
mgo.Dial("mongodb://usernamer:password#127.0.0.1:27017/dbname")
It gives me an error saying {"error":"no reachable servers"}.
My docker-compose.yml file is as below
version: "2"
services:
todo:
build:
context: .
dockerfile: todo/Dockerfile
restart: always
volumes:
- .:/go/src/prac
container_name: todo
ports:
- 8800:8081
mongodb:
command: mongod --auth
container_name: mongodb
image: mongo:latest
ports:
- 27017:27017
The problem appears to be that you are trying to connect to 127.0.0.1. MongoDB is not in the same container, so this won't work.
mgo.Dial("mongodb://usernamer:password#127.0.0.1:27017/dbname")
You should instead be connecting to the MongoDB container you defined by using the name you chose.
mgo.Dial("mongodb://usernamer:password#mongodb:27017/dbname")
Docker Compose creates a network for your containers in which they can access each other using the names you have defined as hostnames. Note that you don't need to define ports for containers to reach each other; these are only needed to reach containers from outside Docker.
Related
Similar to Can't connect to MongoDB container from other Docker container - but answers from this post don't work for me.
I am new to Docker. Trying to learn it on a typescript/express/mongo/mongoose api example.
What I am trying to do (and having problems with), is to use mongo cmd line on a running mongo container after it has been spun up using docker compose up. Even though I have my data nicely persisted on a Docker volume, I don't seem to be able to log into the database using cmd line.
This is my docker-compose.yml file:
version: '3.9'
services:
api:
container_name: api_ts
build: .
restart: unless-stopped
environment:
- DB_URL=mongodb://myself:pass123#mongo:27017/
ports:
- '3131:3131'
depends_on:
- mongo
links: # (seems to be needed)
- mongo
mongo:
container_name: mongo_container
image: mongo:latest
restart: always
volumes:
- mongo_dbv:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=myself
- MONGO_INITDB_ROOT_PASSWORD=pass123
ports:
- '27017:27017'
volumes:
mongo_dbv: {}
This is my Dockerfile:
FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json .
RUN npm ci
COPY . .
ENV PORT=3131
EXPOSE 3131
COPY .env ./dist
CMD ["npm", "start"]
I am running
docker compose up -d --build
After both services are ready, I do:
docker exec -it mongo_container mongo
show dbs
...and the output of the last cmd is empty
(same occurs when trying to follow the answers in the post mentioned above)
I am sure the database contains data, because I am able to verify it using REST client.
Also, I am a bit puzzled - and maybe this is somehow connected - why there is no indication, either in docker-compose.yml or in Dockerfile, of the database name which I am using. I would expect it to be part of show dbs output. Despite that, my api runs just fine.
Listing databases requires authentication
docker exec -it mongo_container mongo -u myself -p pass123
Now you can list databases
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
Note: mongo should show you warning that "mongo" shell has been superseded by "mongosh". When you use mongosh, a proper authentication error would be shown on the database listing attempt.
I'm trying to containerize my application. I use mongodb and 2 more micro services.
As you can see in the docker compose file below, I have some problems.
My requirements:
main_image needs to connect to MongoDB.
gui_image needs to connect to MongoDB.
gui_image needs to show its GUI on port 8080 (Can use another port as well)
gui_image has to read and write to a file inside my computer.
MongoDB has to access a volume inside my computer.
main_image needs access to the internet.
Here is my questions:
1- Does exposing ports in docker file and docker-compose the same thing?
2- How do I mount a volume to mongodb as best practice?
3- How to accomplish the requirements above with the diagram below in docker-compose?
Here is my docker-compose file:
version: "3"
services:
mongo:
image: mongo:latest
ports:
- 27017:27017
main_image:
build:
context: .
dockerfile: .\my_project\dockerfile
depends_on:
- mongo
gui_image:
build:
context: .
dockerfile: .\my_gui\dockerfile
ports:
- 8080:8080
- 27017:27017
depends_on:
- mongo
Here is my dockerfile under my_gui directory:
FROM continuumio/miniconda3
WORKDIR /app
COPY . .
RUN pip install dash
EXPOSE 8080
EXPOSE 27017
ENTRYPOINT [ "python","gui_script.py"]
And lastly, here is my dockerfile under my_project directory:
FROM continuumio/miniconda3
WORKDIR /app
COPY . .
EXPOSE 27017
ENTRYPOINT [ "python","main_script.py"]
1.The EXPOSE instruction in Dockerfile informs Docker that the container listens on the specified network ports at runtime(like when using docker run -p command).
However using ports in compose is a dynamic way of specifying these ports. So images like nginx or apache which are always supposed to run on port 80 inside the container will use EXPOSE in Dockerfile itself.
While an image which has dynamic port which may be controlled using an environment variable will then use expose in docker run or compose file.
some_webapi:
environment:
- ASPNETCORE_URLS=http://*:80
build:
context: .
dockerfile: ./Dockerfile
2.As documented on the docker hub page for mongo image (https://hub.docker.com/_/mongo/) you can use
volumes:
- '/path/to/your/pc/folder:/path/inside/docker'
3.And for the last question you might wanna use Networking in Compose.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Services can join networks like this
gui_image:
build:
context: .
dockerfile: .\my_gui\dockerfile
ports:
- 8080:8080
- 27017:27017
depends_on:
- mongo
networks:
- gui
And also you have to define all the networks used by services in global scope of compose file
version: '3'
services:
networks:
gui:
After that containers will be able to see each other even by their container_name which you can define in services
mongo:
image: mongo:latest
ports:
- 27017:27017
container_name: gui_mogno
then you will be able to connect to mongo with a connection string like this mongodb://gui_mogno:27017/
You can get more information about networking here
The following code snippet is a part of my "docker compose" file and as you see, the internal port of 27017 was mapped to 37017 to prevent colliding with the MongoDB instance running on the host development machine:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 27017:37017
I use Compass when trying to connect to this mongo instance in docker and the following connection string, but Compass fails to connect to the database:
mongodb://host.docker.internal:37017
What is missing in this configuration keeping me from connecting to the mongodb in docker?
The credit to this answer goes to David Maze. Switching the port numbers addressed the problem. The correct yaml looks like below:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 37017:27017
I have the following docker compose file that I use to spin up a mongo docker container which works as intended.
However, I cannot seem to connect to the container from compass on my local machine.
I executed docker inspect on my mongo container and got its IP and tried to connect using compass, but it didn't work - connection timed out.
Is it the IP of my docker network or the IP of the mongo container I need?
docker compose file:
version: "3"
services:
pokerStats:
image: pokerStats
container_name: pokerStats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
I was able to get this working using the top answer on this question.
I had to change my docker-compose file to expose port 27018 on my local:
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
And my connection details on Compass to be:
You are mapping port 27017 from your host machine to port 27017 on the container. So the following connection string should work mongodb://localhost:27017
I have a simple program written in golang which connect to a mongodb instance thus:
func main() {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
}
I use docker compose to spin up two containers one to spin up my GOLANG API and the other to spin up mongo:
version: "3.3"
services:
api:
image: golang:latest
volumes:
- .:/go
working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
networks:
- api-net
db:
build: .
expose:
- '27017'
container_name: 'mongo'
ports:
- "27017:27017"
networks:
- api-net
networks:
api-net:
driver: bridge
I use a Dockerfile in order to spin up mongo with a conf file that sets the instance to bind to all IPV4 and IPV6 networks:
FROM mongo:latest
COPY mongod.conf /etc/mongod.conf
CMD mongod --config /etc/mongod.conf##
and this is the mongod.conf file:
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
When I run docker-compose.up this is what I get:
Building db
Step 1/3 : FROM mongo:latest
---> a0f922b3f0a1
Step 2/3 : COPY mongod.conf /etc/mongod.conf
---> Using cache
---> f270e718c11e
Step 3/3 : CMD mongod --config /etc/mongod.conf
---> Running in 89ffc2495a2a
Removing intermediate container 89ffc2495a2a
---> fe2677d53122
Successfully built fe2677d53122
Successfully tagged carsupermarket_db:latest
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating mongo ... done
Creating carsupermarket_api_1 ... done
Attaching to mongo, carsupermarket_api_1
api_1 | panic: no reachable servers
api_1 |
api_1 | goroutine 1 [running]:
api_1 | main.main()
api_1 | /go/src/main.go:38 +0x291
api_1 | exit status 2
I've trawled Google and stackoverflow and the only thing I could manage to find which is vaguely related to my issue is:
mongod --bind_ip using docker-compose version 2
however, my docker-compose.yml file as it stands 'Should' on paper work.
Can someone please point me in the correct direction as to why my GOLANG code cannot find my mongodb instance.
I think that when you call mgo.Dial("localhost:27017") the localhost part refers to the localhost within the golang container, and not the host the the containers are running on.
Docker will resolved container names as host names, change the dial to mgo.Dial("mongo:27017") and it should work.
From the docker container networking bridge docs
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by
IP addresses, unless you use the --link option, which is considered
legacy. On a user-defined bridge network, containers can resolve each
other by name or alias.
https://docs.docker.com/network/bridge/
version: "2"
services:
api:
image: golang:latest
volumes:
- .:/go
# Work dir should be defined in dockerfile
#working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
db:
build: .
container_name: 'mongo'
# You should define some volumes here if you want the data to persist.
You should be able to connect from the golang container to the mongo container using inter container communication using the hostname: 'mongo'