zanooda
zanooda

Reputation: 21

Issue during running server written in go under the local Docker container (Windows, 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:1.16.3-buster@sha256: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

Upvotes: 2

Views: 66

Answers (1)

MIkCode
MIkCode

Reputation: 2845

as mentioned in the comments you are building the container completely wrong.

Im assuming that you are using go modules.

The correct way I to use a multi-stage build in order to create small containers.

In the first stage, we build the binary.

In the second stage, we copy the binary and building the container.

#First stage 
FROM golang:1.16-buster as builder
WORKDIR /app
COPY go.* ./
# Download dependencies 
RUN go mod download
# Copy local code to the container image.
COPY . .
# Build the binary.
RUN  go build -v -o BINERYNAME

# Secound stage 
FROM debian:buster-slim
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    ca-certificates && \
    rm -rf /var/lib/apt/lists/*
# Copy the binary  to  production image from the builder stage.
COPY --from=builder /app/BINERYNAME /app/BINERYNAME

# Run app on container startup.
CMD ["/app/BINERYNAME"]




Upvotes: 1

Related Questions