After deploying my go project in Docker, it cannot connect with mongoDB

Hey, I’m a newbie of Go developers.

Recently I learn to build an API server from 《Web Development with Go》, and it works on my local computer after I run mongod.

After that, I deploy this server with docker, then I run it. But I don’t know how it can connect with mongoDB again.

Here is my Dockerfile.

# golang image where workspace (GOPATH) configured at /go.
FROM golang

# Copy the local package files to the conainer's workspace
ADD . /go/src/

# Setting up working directory
WORKDIR /go/src/

# Get godeps for managing and restoring dependencies
RUN go get

# Restore godep dependencies
RUN godep restore

# Build the taskmanager command inside the container.
RUN go install

# Run the taskmanager command when the container starts.
ENTRYPOINT /go/bin/taskmanager

# Service listens on port 8080.

Here is one of the function which deals with database.

func createDbSession() {
	var err error
	session, err = mgo.DialWithInfo(&mgo.DialInfo{
		Addrs:    []string{""},
		Username: "",
		Password: "",
		Timeout:  60 * time.Second,
	if err != nil {
		log.Fatalf("[createDbSession]: %s\n", err)

But fail…

$ docker run --publish 8080:8080 taskmanager
2017/04/25 13:37:59 [createDbSession]: no reachable servers

As what the book says, I run docker build -t taskmanager . to build docker, and run it with docker run --public 8080:8080 taskmanager. But I don’t know how to reconnect with the database.

In conclusion, the question is that if I want to separate the project into two container, the one is the TaskManager application and another is the container of MongoDB database, how can they communicate with each other as recent?

Here is my project: taskManager. It seems like the port problem :frowning:

Correct me if I am wrong, but the Dockerfile does not seem to install any MongoDB instance in your container, but your mgo.DialWithInfo seems to expect the db instance within the container (

If the MongoDB instance runs on the host, you might need to make the host IP available to the container and then change Addrs: []string{""}, to the host IP.

(Disclaimer: I never tried things like this, so I am only doing guesswork.)

1 Like

Thanks for your replying. :slight_smile:
It seems I need to use docker-compose to link server container and mongodb container, or use link flag docker run -it --link mongodb:mongo --name taskmanager1 taskmanager --publish 8080:8080.

Anyway, I start to make friend with docker now.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.