Issue during running server written in go under the local Docker container (Windows, wsl-2)

  containers, docker, go, mongodb, wsl-2

I have a server code written in golang that listen to http and connecting to mongodb at localhost:27017 at my development machine and on aws ec-2 instance also the same host. All is working well when I run it via go run main.go.

sudo docker build -t server /mnt/c/code/tttfm/backend/server returns

 => [internal] load build definition from Dockerfile                                                                                           0.1s 
 => => transferring dockerfile: 38B                                                                                                            0.0s 
 => [internal] load .dockerignore                                                                                                              0.1s 
 => => transferring context: 2B                                                                                                                0.0s 
 => [internal] load metadata for docker.io/library/golang:1.16.3-buster                                                                        2.3s 
 => [1/6] FROM docker.io/library/golang:[email protected]:2ff1a0d92edf6c2d4a2dd0d6bfa43e512c8b2f3aac738ffbb6195caf7cb38164                  0.0s 
 => [internal] load build context                                                                                                              0.2s 
 => => transferring context: 684B                                                                                                              0.2s 
 => CACHED [2/6] WORKDIR /code                                                                                                                 0.0s 
 => CACHED [3/6] RUN apt-get update                                                                                                            0.0s 
 => CACHED [4/6] RUN go get go.mongodb.org/mongo-driver/mongo                                                                                  0.0s 
 => CACHED [5/6] RUN go get github.com/google/uuid                                                                                             0.0s 
 => CACHED [6/6] COPY . .                                                                                                                      0.0s 
 => exporting to image                                                                                                                         0.0s 
 => => exporting layers                                                                                                                        0.0s 
 => => writing image sha256:9e215e06bf4ede636b31706af38497653bcaa5f3407de50ba7cc9a951be614bd                                                   0.0s 
 => => naming to docker.io/library/server 

But when I’am trying to run it is Exit with go errors
sudo docker run -it server

api/build/build.go:17:2: cannot find package
api/events/events.go:14:2: cannot find package
api/events/events.go:15:2: cannot find package
mongodb/mongodb.go:8:2: cannot find package
mongodb/mongodb.go:9:2: cannot find package

the lines is

import...
"go.mongodb.org/mongo-driver/bson"
"github.com/google/uuid"

Looks like another components imported well, like:

    "net/http"
    api "./api"
    build "./api/build"
    mongodb "./mongodb"
    storage "./storage"
    webChat "./webChat"

But it down on external components:"github.com/google/uuid" and "go.mongodb.org/mongo-driver/bson" on runtime during importing.

The dockerfile

FROM golang:1.16.3-buster
WORKDIR /code
RUN apt-get update
RUN go get go.mongodb.org/mongo-driver/mongo
RUN go get github.com/google/uuid
COPY . .

CMD ["go", "run", "main.go"]

I am noob in dockers and stucked with problem to run server under the docker container. What can be the issue? Thank a lot for any help

Source: Docker Questions

LEAVE A COMMENT