Skip to content

Commit 78edc33

Browse files
committed
Added go dockerfiles examples with and without packages
1 parent 7133a04 commit 78edc33

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed

go/simple/Dockerfile

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM golang:1.21 AS builder
2+
3+
# Set the current working directory inside the container
4+
WORKDIR /app
5+
6+
# Copy the source code into the container
7+
COPY hello.go .
8+
9+
# Compile the binary
10+
RUN go build -o /bin/hello ./hello.go
11+
12+
# Create a non-root user on a temporary file
13+
RUN echo "appuser:x:65534:65534:appuser:/:" > /etc_passwd
14+
15+
FROM scratch
16+
17+
# Copy the passwd file to create a non-root user
18+
COPY --from=builder /etc_passwd /etc/passwd
19+
20+
# Copy compiled binary from builder
21+
COPY --chown=appuser:appuser --from=builder /bin/hello /bin/hello
22+
23+
# Set the user to use when running the image
24+
USER appuser
25+
26+
# Set the entrypoint to the binary
27+
CMD ["/bin/hello"]

go/simple/Dockerfile.old

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.21
2+
3+
WORKDIR /app
4+
5+
COPY hello.go .
6+
7+
RUN go build -o /bin/hello ./hello.go
8+
9+
CMD ["/bin/hello"]

go/simple/hello.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("¡Hello world!")
7+
}

go/simple_with_packages/Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM golang:1.21 AS builder
2+
3+
# Set the current working directory inside the container
4+
WORKDIR /app
5+
6+
# Copy go mod and sum files
7+
COPY go.mod go.sum ./
8+
9+
# Download all dependencies
10+
RUN go mod download
11+
12+
# Copy the source code into the container
13+
COPY *.go .
14+
15+
# Compile the binary
16+
RUN go build -o /app_bin
17+
18+
# Create a non-root user on a temporary file
19+
RUN echo "appuser:x:65534:65534:appuser:/:" > /etc_passwd
20+
21+
FROM scratch
22+
23+
# Copy the passwd file to create a non-root user
24+
COPY --from=builder /etc_passwd /etc/passwd
25+
26+
# Copy compiled binary from builder
27+
COPY --chown=appuser:appuser --from=builder /app_bin /app_bin
28+
29+
# Set the user to use when running the image
30+
USER appuser
31+
32+
# Set the entrypoint to the binary
33+
CMD ["/app_bin"]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.21
2+
3+
WORKDIR /app
4+
5+
# Copy go.mod and go.sum to download dependencies
6+
COPY go.mod go.sum .
7+
8+
# Download dependencies
9+
RUN go mod download
10+
11+
# Copy source code
12+
COPY *.go .
13+
14+
# Build
15+
RUN go build -o /app_bin
16+
17+
ENTRYPOINT ["/app_bin"]

go/simple_with_packages/go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module multi-stage-test
2+
3+
go 1.21
4+
5+
require (
6+
github.com/onsi/ginkgo v1.16.4
7+
github.com/onsi/gomega v1.12.0
8+
github.com/prometheus/client_golang v1.11.0
9+
)

go/simple_with_packages/go.sum

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
3+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
4+
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 h1:OgUuv8lsRpBibGNbSizVwKWlysjaNzmC9gYMhPVfqFM=
5+
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
6+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9+
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10+
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
11+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12+
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
14+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
15+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
16+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
19+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
20+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
22+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23+
ktbs.dev/mubeng v0.4.3 h1:PN+z98Ep/qPeFABSioxQNAki+TBUEjleiS22Xnm+3Ic=
24+
ktbs.dev/mubeng v0.4.3/go.mod h1:PX3wTh1h2QSycnFPTCUqjhMnrSAsL5Rq46OJXFdz92s=
25+
ktbs.dev/mubeng v0.4.4 h1:YUVbCGZfJuPN2JHQxBYKxRInBClx111lCiA3jRWID1E=
26+
ktbs.dev/mubeng v0.4.4/go.mod h1:k/F8G4p9KtN6/UBLONvgSVYRrruwEuwlpPxXAFBu+K4=

go/simple_with_packages/hello.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("¡Hello world!")
7+
}

0 commit comments

Comments
 (0)