-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: 1.0.0
-
None
-
Environment:Tested this on 2 environments and both have the same problems:
Laptop specs:
- OS: Elementry os
- Docker version: 18.09.3
- Mongodb docker image: mongo:latest
- Mongodb installed on system: db version v4.0.6
- Golang driver version: v1.0.0-rc1-1-gfcec76a
- Docker image used on app: golang:alpine and ubuntu
Server:
(because of security rules i can't tell much about the server)
- OS: CoreOS
- Mongodb docker image: mongo:latest
- Docker image used on app: golang:alpineTested this on 2 environments and both have the same problems: Laptop specs: - OS: Elementry os - Docker version: 18.09.3 - Mongodb docker image: mongo:latest - Mongodb installed on system: db version v4.0.6 - Golang driver version: v1.0.0-rc1-1-gfcec76a - Docker image used on app: golang:alpine and ubuntu Server: (because of security rules i can't tell much about the server) - OS: CoreOS - Mongodb docker image: mongo:latest - Docker image used on app: golang:alpine
For now a few days i'm trying to dockerize a new stack we are using at the company.
But i'm now stuck at a point where this driver running in a docker container doesn't want to connect to a mongodb server that is also running in a docker container.
Here is the error i'm getting when i want to do any interaction with the mongodb server:
2019/03/14 14:27:30 server selection error: server selection timeout current topology: Type: Unknown Servers: Addr: localhost:27017, Type: Unknown, State: Connected, Avergage RTT: 0, Last error: dial tcp 127.0.0.1:27017: connect: connection refused
This only happens when the app is inside a docker container.
This is the code I've used to test this:
package mainpackage main import ( "context" "fmt" "log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func C() context.Context { return context.Background() } func main() { connectionURL := "mongodb://mongo?authMechanism=SCRAM-SHA-1" fmt.Println("Connecting to mongodb server:", connectionURL) client, err := mongo.Connect(C(), options.Client().ApplyURI(connectionURL)) if err != nil { log.Panicln(err) } DB := client.Database("idk") _, err = DB.Collection("test").Find(C(), bson.M{}) if err != nil { log.Panicln(err) } }
How i run the database to test this:
docker run -p 27017:27017 --name mongo -it --rm mongo
Then to run the app i build this docker container:
FROM ubuntu WORKDIR / COPY ./testBinary /testBinary CMD /testBinary
And to run it i do:
docker run --link mongo:mongo -it --rm test-binary
When running it hangs for around 30 seconds and after that it errors with the error specified above.
I've tested this with multiple ways that work.
- When running mongo-express in a docker container works:
docker run --link mongo:mongo -it --rm -p 8081:8081 mongo-express
- When running the mongoDB server in a container and the app directly on the system (not in a docker container)
- when running the mongoDB server the normal way (no docker containers via systemctl) and the app directly on the system (also not in a docker container)
Some notes:
- This has nothing todo with --link because when running everything in a docker network it has the same issue.