Home  >  Q&A  >  body text

golang - MongoDB cannot be connected after deploying the Go project to Docker

I followed a book and made an API Server. Here is the project. After the project is started, you can connect to MongoDB by running mongod locally. But when I package the Server into docker (docker build -t taskmanager .) and start the Server with a container (docker run --publish 8080:8080 taskmanager), I don’t know How to connect to the database as before. I don’t know if I wrote the Dockerfile wrong or if the port is not set properly, and I am not very familiar with Docker.

// 连接 mongodb
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)
    }
}
// 报错
$ docker run --publish 8080:8080 taskmanager
2017/04/25 13:37:59 [createDbSession]: no reachable servers

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

Should I also wrap the database in Docker? Or is there something wrong with the way I start Docker? The port of Mongodb is the default 27017. If the project is successfully started and opened, 404 will be displayed. godep restoreYou can install project dependencies.


Update:

func createDbSession() {
    var err error
    // 改为 mgo.Dial
    session, err = mgo.Dial(AppConfig.MongoDBHost) // "mongo:27017"
    if err != nil {
        log.Fatalf("[GetSession]: %s\n", err)
    }
    if err != nil {
        log.Fatalf("[createDbSession]: %s\n", err)
    }
}

docker build -t taskmanager . Try link after build

$ docker run -it --link mongodb:mongo --name taskmanager2 taskmanager --publish 8080:8080
2017/04/26 13:17:07 Listening...

It can be started now, but localhost:8080 I can’t see the project, and I can’t find the server address.
I plan to read the "First Docker Book" in the next few days Let’s look back at this issue again.
Thanks to the responders.

仅有的幸福仅有的幸福2736 days ago966

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:19:18

    Programs in docker cannot access the host's services. You should also use docker to run mongodb, and then use the link command to link the two containers

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:19:18

    Your problem has nothing to do with link or not.
    Docker cannot access the host service using 127.0.0.1. You must use an internal or external IP such as 192.168.x.x

    reply
    0
  • Cancelreply