Skip to content

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

Merged
merged 10 commits into from
Jul 26, 2022

Conversation

mridulganga
Copy link
Contributor

@mridulganga mridulganga commented Jul 7, 2022

Summary of changes

  1. Made changes to Dockerfile to use golang's alpine image for smaller size which helps build image faster
  2. Added make build to build the golang binary replacing the embedded build command
  3. Added an Optional PORT env var and expose command
  4. Minor changes to Makefile to use specific main.go file instead of folder, and added few build args (-v - listing packages while building and -a - rebuilding prebuilt packages everytime) and envs (to disable cgo and setting GOOS)
  5. Minor change in sonar-project.properties to add test inclusions parameter.
  6. Minor change in .gitignore to ignore vs code and pycharm IDE config folders.

@mridulganga mridulganga linked an issue Jul 7, 2022 that may be closed by this pull request
Copy link
Collaborator

@haani-niyaz haani-niyaz left a 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
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

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 👍

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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

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?

Copy link
Collaborator

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.

@mridulganga mridulganga requested a review from haani-niyaz July 8, 2022 18:48
@@ -15,3 +15,7 @@
# vendor/

bin/

# code editor personal settings
.vscode/
Copy link

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

Copy link
Contributor Author

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 /
Copy link
Collaborator

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?

Copy link
Contributor Author

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
Copy link
Collaborator

@haani-niyaz haani-niyaz Jul 10, 2022

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"

Copy link
Contributor Author

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

Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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

@haani-niyaz haani-niyaz mentioned this pull request Jul 12, 2022
@@ -15,3 +15,7 @@
# vendor/

bin/

# code editor personal settings
.vscode/
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link

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

@haani-niyaz haani-niyaz changed the base branch from main to v0.2.0-review-updates July 26, 2022 08:24
@haani-niyaz haani-niyaz merged commit a28f2ef into v0.2.0-review-updates Jul 26, 2022
@haani-niyaz
Copy link
Collaborator

Merging changes to v0.2.0 branch to control next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improvements to Dockerfile
6 participants