feat: multistage build Dockerfile, not tested

This commit is contained in:
П$iх 2022-07-18 10:32:25 +03:00
parent 304b9c7381
commit b9006e4a1b
3 changed files with 31 additions and 33 deletions

View File

@ -6,6 +6,9 @@
# Visual Studio Code workspace files
/workstations.code-workspace
# vendoring
# directories
/vendor/
/build/
/build/
go.mod
go.sum

4
.gitignore vendored
View File

@ -6,9 +6,9 @@
# Visual Studio Code workspace files
/workstations.code-workspace
# vendoring
# directories
/vendor/
/build/pkg/
/build/
.env
.env.docker

View File

@ -1,49 +1,44 @@
FROM golang:alpine
LABEL org.opencontainers.image.authors="psssix <psssix@gmail.com>"
MAINTAINER psssix <psssix@gmail.com>
FROM golang:alpine as builder
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# set build environmet variables needed for multistage building
ENV CGO_ENABLED=0 \
GOOS=linux
# Use workdir
RUN mkdir /source
# source workdir
WORKDIR /source
# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
# Vendoring
COPY go.mod go.sum ./
RUN go mod download
# Copy the code into the container
COPY . .
# Vendoring
RUN go mod tidy && go mod vendor
# Some different way for vendoring
#RUN go mod tidy && go mod vendor
# Build the application
RUN go build -o build/golang-app -mod=vendor .
RUN go build -o build/app .
# Move to /smart-bot directory as the place for resulting binary folder
RUN mkdir /app
WORKDIR /app
FROM scratch
# Move to /app directory as the place for resulting binary folder
WORKDIR /
# Copy binary from build to main folder
#
# some application assets
# RUN cp /source/build/bot . \
# && cp -R /source/docs/dictionaries .
RUN cp /source/build/golang-app
# COPY ./database/data.json /dist/database/data.json
RUN rm -rf /source
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder build/app app
# Copy some application assets
#COPY ./database/data.json /dist/database/data.json
# Export necessary port
# EXPOSE 3000
#EXPOSE 3000
ENTRYPOINT ["/app/golang-app"]
ENTRYPOINT ["/app"]
# Command to run when starting the container
#CMD ["/dist/golang-app"]
#CMD ["/app"]
# if have not application daemon
#CMD tail -f /dev/null