Home > database >  Golang - problem with creating docker image
Golang - problem with creating docker image

Time:08-14

I try to make docker image, but when I run cmd in terminal.

sudo docker build testapi .

I get an error:

 => ERROR [6/6] RUN go build -o /app/testapi/cmd/test-api                                                                                                              0.3s
------
 > [6/6] RUN go build -o /app/testapi/cmd/test-api:
#14 0.231 no Go files in /app
------
executor failed running [/bin/sh -c go build -o /app/testapi/cmd/test-api]: exit code: 1

File structure

/testapi
 /cmd
  /test-api
   maing.go
 /pkg
  /...
 Dockerfile

Dockerfile:

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod ./

RUN go mod download

COPY . ./

RUN go build -o /app/testapi/cmd/test-api

EXPOSE 8080

CMD [ "/testapi/cmd/test-api" ]

CodePudding user response:

Try this way.

Also do not forget to add go.sum in Dockerfile if you have. COPY go.sum .

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod .

COPY . .

RUN go build -o main ./cmd/<FOLDER_NAME>

WORKDIR /dist

RUN cp /app/main .

EXPOSE 3000

CMD ["/dist/main"]

Works fine

Status: Downloaded newer image for golang:1.16-alpine
 ---> 7642119cd161
Step 2/9 : WORKDIR /app
 ---> Running in f8ad2994262c
Removing intermediate container f8ad2994262c
 ---> 6e079707cc3e
Step 3/9 : COPY go.mod .
 ---> c79a0cc04ff5
Step 4/9 : COPY . .
 ---> 897027112e73
Step 5/9 : RUN go build -o main ./cmd/api
 ---> Running in eb4579f1c339
Removing intermediate container eb4579f1c339
 ---> e41f6e5542a4
Step 6/9 : WORKDIR /dist
 ---> Running in 92115865d1ec
Removing intermediate container 92115865d1ec
 ---> 8152a0ebe4bc
Step 7/9 : RUN cp /app/main .
 ---> Running in 5c68cb195826
Removing intermediate container 5c68cb195826
 ---> 6f2e1fb8a611
Step 8/9 : EXPOSE 3000
 ---> Running in fba77820c1ec
Removing intermediate container fba77820c1ec
 ---> 182a624d807a
Step 9/9 : CMD ["/dist/main"]
 ---> Running in 164ba25694bd
Removing intermediate container 164ba25694bd
 ---> 7cf22cffc472
Successfully built 7cf22cffc472
Successfully tagged bla:latest

CodePudding user response:

go build [-o output] [build flags] [packages]

  • Related