restructure repo, separate sample app from docker configs
This commit is contained in:
9
06-building-container-images/api-golang/Dockerfile.0
Normal file
9
06-building-container-images/api-golang/Dockerfile.0
Normal file
@ -0,0 +1,9 @@
|
||||
FROM golang
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go mod download
|
||||
|
||||
CMD ["go", "run", "./main.go"]
|
||||
11
06-building-container-images/api-golang/Dockerfile.1
Normal file
11
06-building-container-images/api-golang/Dockerfile.1
Normal file
@ -0,0 +1,11 @@
|
||||
# Pin specific version for stability
|
||||
# Use alpine for reduced image size
|
||||
FROM golang:1.19-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go mod download
|
||||
|
||||
CMD ["go", "run", "./main.go"]
|
||||
14
06-building-container-images/api-golang/Dockerfile.2
Normal file
14
06-building-container-images/api-golang/Dockerfile.2
Normal file
@ -0,0 +1,14 @@
|
||||
# Pin specific version for stability
|
||||
# Use alpine for reduced image size
|
||||
FROM golang:1.19-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go mod download
|
||||
|
||||
# Compile application during build rather than at runtime
|
||||
RUN go build -o api-golang
|
||||
|
||||
CMD ["./api-golang"]
|
||||
17
06-building-container-images/api-golang/Dockerfile.3
Normal file
17
06-building-container-images/api-golang/Dockerfile.3
Normal file
@ -0,0 +1,17 @@
|
||||
# Pin specific version for stability
|
||||
# Use alpine for reduced image size
|
||||
FROM golang:1.19-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy only files required to install dependencies (better layer caching)
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Compile application during build rather than at runtime
|
||||
RUN go build -o api-golang
|
||||
|
||||
CMD ["./api-golang"]
|
||||
29
06-building-container-images/api-golang/Dockerfile.4
Normal file
29
06-building-container-images/api-golang/Dockerfile.4
Normal file
@ -0,0 +1,29 @@
|
||||
# Pin specific version for stability
|
||||
# Use separate stage for building image
|
||||
# Use debian for easier build utilities
|
||||
FROM golang:1.19-bullseye AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy only files required to install dependencies (better layer caching)
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Compile application during build rather than at runtime
|
||||
# Add -w and -s flags to
|
||||
RUN go build \
|
||||
-ldflags="-linkmode external -extldflags -static" \
|
||||
-o api-golang
|
||||
|
||||
# Use separate stage for deployable image
|
||||
FROM scratch
|
||||
|
||||
WORKDIR /
|
||||
|
||||
# Copy the binary from the build stage
|
||||
COPY --from=build /app/api-golang api-golang
|
||||
|
||||
CMD ["/api-golang"]
|
||||
35
06-building-container-images/api-golang/Dockerfile.5
Normal file
35
06-building-container-images/api-golang/Dockerfile.5
Normal file
@ -0,0 +1,35 @@
|
||||
# Pin specific version for stability
|
||||
# Use separate stage for building image
|
||||
# Use debian for easier build utilities
|
||||
FROM golang:1.19-bullseye AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy only files required to install dependencies (better layer caching)
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Compile application during build rather than at runtime
|
||||
# Add flags to statically link binary
|
||||
RUN go build \
|
||||
-ldflags="-linkmode external -extldflags -static" \
|
||||
-o api-golang
|
||||
|
||||
# Use separate stage for deployable image
|
||||
FROM scratch
|
||||
|
||||
# Set gin mode
|
||||
ENV GIN_MODE=release
|
||||
|
||||
WORKDIR /
|
||||
|
||||
# Copy the binary from the build stage
|
||||
COPY --from=build /app/api-golang api-golang
|
||||
|
||||
# Indicate expected port
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["/api-golang"]
|
||||
47
06-building-container-images/api-golang/Dockerfile.6
Normal file
47
06-building-container-images/api-golang/Dockerfile.6
Normal file
@ -0,0 +1,47 @@
|
||||
# Pin specific version for stability
|
||||
# Use separate stage for building image
|
||||
# Use debian for easier build utilities
|
||||
FROM golang:1.19-bullseye AS build
|
||||
|
||||
# Add non root user
|
||||
RUN useradd -u 1001 nonroot
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy only files required to install dependencies (better layer caching)
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# Use cache mount to speed up install of existing dependencies
|
||||
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||
--mount=type=cache,target=/root/.cache/go-build \
|
||||
go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
# Compile application during build rather than at runtime
|
||||
# Add flags to statically link binary
|
||||
RUN go build \
|
||||
-ldflags="-linkmode external -extldflags -static" \
|
||||
-o api-golang
|
||||
|
||||
# Use separate stage for deployable image
|
||||
FROM scratch
|
||||
|
||||
# Set gin mode
|
||||
ENV GIN_MODE=release
|
||||
|
||||
WORKDIR /
|
||||
|
||||
# Copy the passwd file
|
||||
COPY --from=build /etc/passwd /etc/passwd
|
||||
|
||||
# Copy the binary from the build stage
|
||||
COPY --from=build /app/api-golang api-golang
|
||||
|
||||
# Use nonroot user
|
||||
USER nonroot
|
||||
|
||||
# Indicate expected port
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["/api-golang"]
|
||||
12
06-building-container-images/api-golang/Makefile
Normal file
12
06-building-container-images/api-golang/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
API_GOLANG_PATH:=../../05-example-web-application/api-golang/
|
||||
|
||||
N?=0
|
||||
.PHONY: build-N
|
||||
build-N:
|
||||
docker build --file ./Dockerfile.$(N) -t api-golang:$(N) ${API_GOLANG_PATH}
|
||||
|
||||
.PHONY: build-all
|
||||
build-all:
|
||||
for number in 0 1 2 3 4 5 6 ; do \
|
||||
N=$$number $(MAKE) build-N; \
|
||||
done
|
||||
Reference in New Issue
Block a user