I followed instructions I found here: https://hub.docker.com/_/golang
% cat Dockerfile
FROM golang:1.21
WORKDIR /usr/src/app
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /usr/local/bin/app ./...
CMD ["app"]
You can use it like this ...
% cat hello.go
package main
import ("fmt")
func main() {
fmt.Println("Hello World!")
}
% docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.21 go build /usr/src/myapp/hello.go
% docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.21 /usr/src/myapp/hello
Hello World!
No comments:
Post a Comment