GO+MongoDB in EC2 instance - build error

Dear friends,
Need your help. I have installed GO in EC2, able to run hello world program. I am trying to access mongodb database from my program. I have installed the driver, checked the folders structure, check the paths. Here is the detail:

Folders structure in EC2 instance

[ec2-user@ip-172-31-35-142 mongo-driver]$ pwd
/home/ec2-user/go/src/go.mongodb.org/mongo-driver
[ec2-user@ip-172-31-35-142 mongo-driver]$ dir
benchmark  bson  cmd  CONTRIBUTING.md  data  etc  event  examples  go.mod  go.sum  internal  LICENSE  Makefile  mongo  README.md  tag  THIRD-PARTY-NOTICES  vendor  version  x
[ec2-user@ip-172-31-35-142 mongo-driver]$ cd mongo
[ec2-user@ip-172-31-35-142 mongo]$ dir
batch_cursor.go              client_examples_test.go                  crypt_retrievers.go  index_options_builder.go  options                          single_result_test.go
bson_helpers_test.go         client.go                                cursor.go            index_view.go             readconcern                      testatlas
bulk_write.go                client_options_test.go                   cursor_test.go       integration               readpref                         testaws
bulk_write_models.go         client_side_encryption_examples_test.go  database.go          main_test.go              read_write_concern_spec_test.go  util.go
change_stream_deployment.go  client_test.go                           database_test.go     mongocryptd.go            results.go                       with_transactions_test.go
change_stream.go             collection.go                            doc.go               mongo.go                  results_test.go                  writeconcern
change_stream_test.go        collection_test.go                       errors.go            mongo_test.go             session.go
client_encryption.go         crud_examples_test.go                    gridfs               ocsp_test.go              single_result.go
[ec2-user@ip-172-31-35-142 mongo-driver]$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ec2-user/.cache/go-build"
GOENV="/home/ec2-user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ec2-user/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ec2-user/go/src/go.mongodb.org/mongo-driver/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build744267489=/tmp/go-build -gno-record-gcc-switches"

Building my go program

[ec2-user@ip-172-31-35-142 ~]$ sudo docker build -t my-go-app .
Sending build context to Docker daemon  136.8MB
Step 1/8 : From golang
 ---> 75605a415539
Step 2/8 : MAINTAINER Jyoti Sarkar <jyoti.sarkar@gmail.com>
 ---> Using cache
 ---> d61475aade96
Step 3/8 : RUN mkdir /app
 ---> Using cache
 ---> 91952fea89b3
Step 4/8 : ADD . /app
 ---> Using cache
 ---> eecad49ab17c
Step 5/8 : WORKDIR /app
 ---> Using cache
 ---> f0ab8a35fcb6
Step 6/8 : RUN go build -o main
 ---> Running in 808220c76627
main.go:9:2: cannot find package "go.mongodb.org/mongo-driver/bson" in any of:
        /usr/local/go/src/go.mongodb.org/mongo-driver/bson (from $GOROOT)
        /go/src/go.mongodb.org/mongo-driver/bson (from $GOPATH)
main.go:10:2: cannot find package "go.mongodb.org/mongo-driver/mongo" in any of:
        /usr/local/go/src/go.mongodb.org/mongo-driver/mongo (from $GOROOT)
        /go/src/go.mongodb.org/mongo-driver/mongo (from $GOPATH)
main.go:11:2: cannot find package "go.mongodb.org/mongo-driver/mongo/options" in any of:
        /usr/local/go/src/go.mongodb.org/mongo-driver/mongo/options (from $GOROOT)
        /go/src/go.mongodb.org/mongo-driver/mongo/options (from $GOPATH)
The command '/bin/sh -c go build -o main' returned a non-zero code: 1

Why am I getting above error? Pls advice.

Hi @Jyoti_Sarkar,

This looks like an issue with environment variables not being carried over when using Docker. Your go env output shows the GOROOT is /usr/lib/golang and GOPATH is /home/ec2-user/go but the Docker output shows that it’s looking for the driver in /usr/local/go/ for GOROOT and /go/ for GOPATH. I don’t have much experience with Docker, but a good starting point would be to investigate how environment variables should be set, either on your system or in your Docker config file, to make sure they’re propagated correctly.

1 Like