[GH-ISSUE #39] Take advantage of multistage build #30

Open
opened 2026-02-26 21:34:05 +03:00 by kerem · 2 comments
Owner

Originally created by @Jogai on GitHub (Sep 9, 2024).
Original GitHub issue: https://github.com/eduardolat/pgbackweb/issues/39

See https://blog.kubesimplify.com/multi-stage-docker-build#heading-the-lab-rat or https://github.com/alextanhongpin/go-docker-multi-stage-build?tab=readme-ov-file#method-3-using-multi-stage-build

The release image could be a lot smaller that way.

I'm not familiar with building go or the setup you used for your frontend app, so I got stuck at this point:

# Frontend build
FROM node:lts-alpine AS node
WORKDIR /internal/view/static
# Copy and install nodejs dependencies
COPY ["package.json", "package-lock.json", "tailwind.config.js", "/internal/view/static/"]
CMD npm i
CMD npm run esbuild js/app.js --bundle --minify --outfile=app.min.js
CMD npm run tailwindcss --config tailwind.config.js --input /css/style.css --output style.min.css --minify
CMD echo $(ls -hal)

# Go build
FROM golang:1-alpine AS golang
WORKDIR /cmd/app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -o ./dist/app ./cmd/app/.

# Deployment image
FROM alpine:latest AS production
COPY --from=golang /app/main .
COPY --from=node /internal/view/static /internal/view/static/

WORKDIR /app
RUN rm -rf /app/temp && \
mkdir /backups && \
chmod 777 /backups

# Run the app
EXPOSE 8085
CMD ["task", "migrate-serve"]
Originally created by @Jogai on GitHub (Sep 9, 2024). Original GitHub issue: https://github.com/eduardolat/pgbackweb/issues/39 See https://blog.kubesimplify.com/multi-stage-docker-build#heading-the-lab-rat or https://github.com/alextanhongpin/go-docker-multi-stage-build?tab=readme-ov-file#method-3-using-multi-stage-build The release image could be a lot smaller that way. I'm not familiar with building go or the setup you used for your frontend app, so I got stuck at this point: ``` # Frontend build FROM node:lts-alpine AS node WORKDIR /internal/view/static # Copy and install nodejs dependencies COPY ["package.json", "package-lock.json", "tailwind.config.js", "/internal/view/static/"] CMD npm i CMD npm run esbuild js/app.js --bundle --minify --outfile=app.min.js CMD npm run tailwindcss --config tailwind.config.js --input /css/style.css --output style.min.css --minify CMD echo $(ls -hal) # Go build FROM golang:1-alpine AS golang WORKDIR /cmd/app COPY go.mod go.sum ./ RUN go mod download COPY . ./ RUN CGO_ENABLED=0 GOOS=linux go build -a -o ./dist/app ./cmd/app/. # Deployment image FROM alpine:latest AS production COPY --from=golang /app/main . COPY --from=node /internal/view/static /internal/view/static/ WORKDIR /app RUN rm -rf /app/temp && \ mkdir /backups && \ chmod 777 /backups # Run the app EXPOSE 8085 CMD ["task", "migrate-serve"]
Author
Owner

@eduardolat commented on GitHub (Sep 9, 2024):

Hello @Jogai, thanks for your suggestion

If you look at the Dockerfile we use in production (https://github.com/eduardolat/pgbackweb/blob/main/docker/Dockerfile) you will notice that it includes the minimum dependencies needed to run the software on both amd64 and arm64 hardware.

The fact that the golang binary depends on system tools such as pg_dump or psql, among others, makes it more complicated to simply copy the binary into a clean docker image.

If you could modify the aforementioned Dockerfile and make a proposal I would appreciate it, take into account the README.md next to the Dockerfile.

Always keep in mind that it is better to sacrifice a little space if what we gain is easier maintenance of the project.

<!-- gh-comment-id:2338298064 --> @eduardolat commented on GitHub (Sep 9, 2024): Hello @Jogai, thanks for your suggestion If you look at the Dockerfile we use in production (https://github.com/eduardolat/pgbackweb/blob/main/docker/Dockerfile) you will notice that it includes the minimum dependencies needed to run the software on both amd64 and arm64 hardware. The fact that the golang binary depends on system tools such as pg_dump or psql, among others, makes it more complicated to simply copy the binary into a clean docker image. If you could modify the aforementioned Dockerfile and make a proposal I would appreciate it, take into account the README.md next to the Dockerfile. Always keep in mind that it is better to sacrifice a little space if what we gain is easier maintenance of the project.
Author
Owner

@Jogai commented on GitHub (Sep 9, 2024):

Thank you for answering. Like I said, I'm not familiar with go, so I cant bring it further. Its just that I think this is something this project could benefit from. I'm a happy user, so I'll keep using it, but maybe it keeps someone with less bandwidth from trying.

Always keep in mind that it is better to sacrifice a little space if what we gain is easier maintenance of the project.

Believe me, I know about tradeoffs, but I'm not advocating to make it less maintainable. If anything I think you can simplify it. But I also think you are wasting at least a GB, if not more, so we're not talking about 'a little space'.

<!-- gh-comment-id:2338805329 --> @Jogai commented on GitHub (Sep 9, 2024): Thank you for answering. Like I said, I'm not familiar with go, so I cant bring it further. Its just that I think this is something this project could benefit from. I'm a happy user, so I'll keep using it, but maybe it keeps someone with less bandwidth from trying. > Always keep in mind that it is better to sacrifice a little space if what we gain is easier maintenance of the project. Believe me, I know about tradeoffs, but I'm not advocating to make it less maintainable. If anything I think you can simplify it. But I also think you are wasting at least a GB, if not more, so we're not talking about 'a little space'.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/pgbackweb#30
No description provided.