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/github.com/Latias94/taskmanager
# Setting up working directory
WORKDIR /go/src/github.com/Latias94/taskmanager
# Get godeps for managing and restoring dependencies
RUN go get github.com/tools/godep
# Restore godep dependencies
RUN godep restore
# Build the taskmanager command inside the container.
RUN go install github.com/Latias94/taskmanager
# Run the taskmanager command when the container starts.
ENTRYPOINT /go/bin/taskmanager
# Service listens on port 8080.
EXPOSE 8080
Here is one of the function which deals with database.
func createDbSession() {
var err error
session, err = mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{"127.0.0.1"},
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