docker image
#build image
FROM golang:1.13 AS builder
WORKDIR /go/src
##Download dependent libraries(I want to use the cache, so I'll do this first)
ENV GO111MODULE=on
COPY go.mod go.sum ./
RUN go mod download
ADD . /myapp
WORKDIR /myapp
## main.Compile go and save the executable binary
RUN CGO_ENABLED=0 GOOS=linux go build -o server ../myapp/cmd/main.go
# run-time image
FROM alpine:3.10
COPY --from=builder /myapp/server /app
EXPOSE 50051
ENTRYPOINT ["/app"]
#build image
FROM golang:1.13 AS builder
ADD . /go/src/github.com/myself/myapp
WORKDIR /go/src/github.com/myself/myapp
##Install dep and resolve dependencies
RUN go get github.com/golang/dep/cmd/dep
RUN dep ensure
## main.Compile go and save the executable binary
RUN CGO_ENABLED=0 GOOS=linux go build -o server /myapp/cmd/main.go
# run-time image
FROM alpine:3.10
COPY --from=builder /go/src/github.com/myself/myapp/server /app
EXPOSE 50051
ENTRYPOINT ["/app"]
cannot find package
error in go build--Most likely the path is wrong or the file is missing
packages founded A and B
error in go build--There are multiple packages in the same directory
--The following is a compilation error
--Place multiple main package modules under the target directory
--Put modules with different name package declarations in the same directory
--Files ending in _test
are interpreted as test code, so they can be placed as multiple files as an exception.
--By the way, it is safe to declare the package with a different name from the directory.
--Because it imports on a path basis instead of a package name
XX is not using a known version control system
error/ vendor
and it worked for some reason./ hoge is not within a known GOPATH / src
error――I don't know, but the directory structure seems to be useless.
--A feature of Go's directory structure is that all source code is placed under the environment variable $ GOPATH / src
.
--There are two solutions:
--Place the repository under / go / src
in the container
--Put a symbolic link
root project import: dep does not currently support using GOPATH / src as the project root
error--The workdir was specified as GOPATH / src
, but it was fixed by changing it to GOPATH / src / app
.
fatal: could not read Username for'https://github.com'~
--When you take resources from private repository, you have to use github token etc.
――However, you should be able to use the local path when taking resources from your repository. If you can't get it, it's likely that the path is wrong
--In fact, I was able to change docker's workdir from go / src / app
to /go/src/github.com/myself/my-app
--Recent Go binaries seem to do a Dynamic link, which seems to be caused by the lack of the link target.
--You can add CGO_ENABLED = 0
at build time
- CGO_ENABLED=0 go build
Recommended Posts