midaef
midaef

Reputation: 47

Docker doesn't see main.go

I have some problems with Docker. My dockerfile doesn't see main.go.
I have that struct project

docker-compose.yml  
go.mod
frontend-microservice  
  -cmd  
    -app
      -main.go
  -internal
    -some folders

When I try start docker-compose It's give me that error.

ERROR: Service 'frontend-microservice' failed to build: The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .' returned a non-zero code: 1

By the way dockerfile give error related to go.mod

That my docker-compose

version: "3"
services:
    frontend-microservice:
        build:    
            context: ./frontend-microservice/
            dockerfile: Dockerfile
        ports:
            - 80:80

That my dockerfile

# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder

ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
 
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs 
COPY ./configs/config.json ./configs
 
EXPOSE 8080
 
ENTRYPOINT ["./frontend-microservice"]

Thank you in advance for any help

Upvotes: 0

Views: 449

Answers (1)

Emanuel Bennici
Emanuel Bennici

Reputation: 446

The file where the main() function is defined is located in cmd/app. Instead of changing the current working directory into cmd/app, append cmd/app/main.go to the go build command.

Your Dockerfile would look like this:

# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder

ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice cmd/app/main.go

FROM alpine:latest
RUN apk --no-cache add ca-certificates
 
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs 
COPY ./configs/config.json ./configs
 
EXPOSE 8080
 
ENTRYPOINT ["./frontend-microservice"]

Upvotes: 2

Related Questions