github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/Dockerfile (about)

     1  #
     2  # Minify templates and static files
     3  #
     4  
     5  FROM ubuntu:18.04 as frontend-builder
     6  
     7  WORKDIR /build/frontend
     8  
     9  # Install minify
    10  ADD https://github.com/tdewolff/minify/releases/download/v2.7.2/minify_2.7.2_linux_amd64.tar.gz minify.tar.gz
    11  RUN tar -xvzf minify.tar.gz -C /usr/local/bin && rm minify.tar.gz
    12  
    13  # Minify files
    14  COPY templates ./templates
    15  COPY static ./static
    16  
    17  RUN minify --html-keep-default-attrvals -o templates/ templates && \
    18  	minify -o static/css/ static/css && \
    19  	minify -o static/js/ static/js
    20  
    21  # Minify has poor support of Go template syntax. For example, it converts all attributes to lower case.
    22  # It causes template execution errors. For example, <html lang="{{ .Lang }}"> is converted to <html lang="{{ .lang }}">
    23  #
    24  # There's an issue (https://github.com/tdewolff/minify/issues/35) for this problem opened in 2015, but it is still opened.
    25  # So, we have to find a workaround. For example, we can manually fix Go templates in attributes (they can be found with
    26  # this regexp: \{\{ .*? \}\}>
    27  #
    28  RUN sed -i "s/tohtmlattr/toHTMLAttr/g" templates/month.html templates/months.html
    29  
    30  
    31  #
    32  # Build a binary file
    33  #
    34  
    35  FROM golang:1.17-alpine as backend-builder
    36  
    37  ENV CGO_ENABLED=1
    38  
    39  WORKDIR /build/backend
    40  
    41  # Copy dependencies (for better caching)
    42  COPY go.mod go.sum ./
    43  COPY vendor ./vendor
    44  
    45  # Install go-sqlite3
    46  RUN apk add --update gcc musl-dev && \
    47  	GOOS=linux CC=gcc go install -v github.com/mattn/go-sqlite3
    48  
    49  # Copy code
    50  COPY cmd ./cmd
    51  COPY internal ./internal
    52  
    53  # Copy minified templates and static files
    54  COPY --from=frontend-builder build/frontend/static ./static
    55  COPY --from=frontend-builder build/frontend/templates ./templates
    56  
    57  # Build
    58  ARG LDFLAGS
    59  RUN GOOS=linux go build -ldflags "${LDFLAGS}" -o ./bin/budget-manager ./cmd/budget-manager/main.go
    60  
    61  
    62  #
    63  # Build the final image
    64  #
    65  
    66  FROM alpine:3.11
    67  
    68  LABEL \
    69  	org.opencontainers.image.url=https://github.com/users/ShoshinNikita/packages/container/package/budget-manager \
    70  	org.opencontainers.image.source=https://github.com/ShoshinNikita/budget-manager
    71  
    72  WORKDIR /srv
    73  
    74  COPY --from=backend-builder build/backend/bin .
    75  
    76  ENTRYPOINT [ "/srv/budget-manager" ]