-
Notifications
You must be signed in to change notification settings - Fork 7
fix(Dockerfile): readability and golang image update #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I've added some review comment.
Dockerfile
Outdated
@@ -1,13 +1,27 @@ | |||
FROM golang:1.18 as build | |||
WORKDIR /go/src/app | |||
FROM golang:1.18.3-alpine3.16 as builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should set this to golang:1.18-alpine
so that it always gets the latest changes? This way it would be one less thing for teams to update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed and updated this.
@@ -37,4 +39,4 @@ fmt: | |||
## build: Build binary into bin/ directory | |||
.PHONY: build | |||
build: | |||
go build -ldflags="-w -s" -o bin/app ./... | |||
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/app cmd/main.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't cross compile since the GOOS
value is linux
so for instance on my mac it would fail. This was one of the reasons the Dockerfile and Makefile commands were different. The original Makefile command was designed for local testing.
CGO_ENABLED=0
was missing so we definitely need that one 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the GOOS variable
Dockerfile
Outdated
|
||
# optionally expose a port | ||
ENV PORT=8000 | ||
EXPOSE $PORT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence about this one for the reasons covered earlier. To reiterate not all projects will expose a service e.g: queue workers, CLIs etc. I'm of the opinion we should enable teams to add things and not have them worry about removing things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed port
@@ -37,4 +39,4 @@ fmt: | |||
## build: Build binary into bin/ directory | |||
.PHONY: build | |||
build: | |||
go build -ldflags="-w -s" -o bin/app ./... | |||
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/app cmd/main.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to also think about injecting a global variable Version
during linking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffy-mathew yup, it is useful to have but perhaps as a separate issue. Feel free to create one.
@@ -15,3 +15,7 @@ | |||
# vendor/ | |||
|
|||
bin/ | |||
|
|||
# code editor personal settings | |||
.vscode/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be beneficial to still keep task, launch, extensions etc? Personally have used this as a reference in the past: https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not impose anyone's preferences of IDE config on others I think.
Dockerfile
Outdated
COPY --from=build /go/bin/app / | ||
|
||
# copy executable from builder image to final images | ||
COPY --from=build /src/bin/app / |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mridulganga this needs to be updated to the image built from the previous stage. You renamed it to builder
but it is referenced as build
here. Was this tested locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done this change now.
Dockerfile
Outdated
FROM gcr.io/distroless/static:nonroot | ||
COPY --from=build /go/bin/app / | ||
|
||
# copy executable from builder image to final images |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mridulganga Can we follow the comment convention of using uppercase first letter, thanks.
Also I think it should say "Copy executable from builder image"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated comments to have first letter caps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mridulganga I don't know what you mean by "final images". The comment should be updated as I pointed out earlier.
@@ -1,5 +1,7 @@ | |||
SHELL=/bin/bash -e -o pipefail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mridulganga, Not sure if you tested this but this line would cause an error since /bin/bash
doesn't exist in alpine. To make this work and for the sake of portability we should use the default shell /bin/sh
so I would suggest removing this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added bash now as well
@@ -15,3 +15,7 @@ | |||
# vendor/ | |||
|
|||
bin/ | |||
|
|||
# code editor personal settings | |||
.vscode/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgive my persistence here, but why is this needed @mridulganga?
I think its quite a given that one should not include the .vscode
folder here since that is used for local development.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since .vscode is used for local dev, we should not checkin that folder, so I included the same in .gitignore.
@@ -10,3 +10,6 @@ sonar.organization=rog-golang-buddies | |||
|
|||
# Encoding of the source code. Default is default system encoding | |||
#sonar.sourceEncoding=UTF-8 | |||
|
|||
# ensure that test files are identified and not used to check coverage | |||
sonar.test.inclusions=**/*_test.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to remove this file, we could add an argument in gh sonarcloud workflow:
-Dsonar.test.inclusions=**/*_test.go
Merging changes to |
Summary of changes
make build
to build the golang binary replacing the embedded build command