-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
improved Dockerfile using multistage to reduce image size #5187
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
Codecov Report
@@ Coverage Diff @@
## master #5187 +/- ##
==========================================
- Coverage 93.91% 93.87% -0.04%
==========================================
Files 123 123
Lines 8939 8950 +11
==========================================
+ Hits 8395 8402 +7
- Misses 544 548 +4
Continue to review full report at Codecov.
|
The build is automated in dockerhub so there is no need to use travis to push. Thanks for the PR. While this seems as always over complicated, this doesn't really solve either the onboarding situation with docker as well as installing 3rd party packages, passing a custom config etc.. |
This is a great improvement to reduce the image size. Multistage Docker builds are standard for some time now (and not over complicated)
There are even Helm Charts ready to install: The purpose of this repo should be to optimize the docker image which everyone is pulling. |
@flovilmart - what else is needed for this PR to merge? |
I am, I need to have a look. |
Dockerfile
Outdated
# RUN ls -al | ||
|
||
# capture git_commit in label | ||
ARG GIT_COMMIT |
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.
is this argument passed by anyone?
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.
https://medium.com/microscaling-systems/labelling-automated-builds-on-docker-hub-f3d073fb8e1
I just learned about dockerhub build hooks - https://docs.docker.com/docker-hub/builds/advanced/
I will add a hook to set the arg during build
Dockerfile
Outdated
# list all dir/files - for debugging purposes | ||
# RUN ls -al | ||
|
||
# ---- UNIT TESTS AND FLOW LINT in stage 3------ |
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.
why keep that if it is not run? We should probably either remove or run the tests
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.
It seems that the tests depend on a db, so we cannot run them inside Dockerfile.
Also, the tests are already setup to run in Travis CI.
I am just leaving the comments here for future reference, in case someone wants to change this.
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.
So please remove it!
RUN npm install | ||
# copy all context into WORKDIR (/parse-server) excluding items in .dockerignore | ||
COPY . . | ||
# Need to run build explicitly as npm will not auto run scripts as ROOT |
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.
why run as ROOT then? If there's no need, please run unpriviledged
# base image for release stage with only prod dependencies | ||
FROM base AS dependencies | ||
# set npm configs | ||
RUN npm set progress=false && npm config set depth 0 |
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.
what is the purpose of set depth 0
?
There is no affected command: https://docs.npmjs.com/misc/config#depth
# ------- Stage 1 - Base --------- | ||
FROM node:carbon-alpine as base | ||
# apk - https://www.cyberciti.biz/faq/10-alpine-linux-apk-command-examples/ | ||
# RUN apk update && apk upgrade && \ |
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.
why keep those??
@@ -1,21 +1,86 @@ | |||
FROM node:carbon | |||
# https://blog.hasura.io/an-exhaustive-guide-to-writing-dockerfiles-for-node-js-web-apps-bbee6bd2f3c4 |
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.
no need to copy paste the sources of the code you're writing. please remove
|
||
RUN mkdir -p /parse-server/config | ||
VOLUME /parse-server/config | ||
# ------- Stage 1 - Base --------- |
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.
please remove useless comments
|
||
|
||
# ------- Stage 4 - Release --------- | ||
FROM dependencies AS release |
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.
all in all each layer uses the previous one completely, what's the point then?
update repo
update repo
@barakbd what size are you at currently? I have a very simple Dockerfile that ends up with a 160Mb size:
|
I get almost same size (176mb). This looks fine. The only improvement my file has is shorter build time, since it only installs each npm package once, but I doubt that's a big concern. I am still testing building with labels that contain the git commit. Dockerhub build environment has a few variables, one of which is SOURCE_COMMIT. Feel free to use your Dockerfile, and I will make another pull request regarding the build hook. |
Not sure there’s a real need for the source commit as it is recommended not to use ‘latest’ in any case. Yes the image ‘works’. Try it out. I may have missed the VOLUMES directive. The npm install is quite fast, and it’s always built by docker hub itself, so speed is not that important |
It is not recommended to use latest for tags, not for labels. |
in any case, closing in favor of #5248 |
What do you mean?
Yes and the respository will equally point to different source code. BUT you need to force push to git as well as the docker repository, so ¯_(ツ)_/¯
Which is not provided by default by docker hub as an environment variable, so I guess this is not 'that' important. |
This is an improved Dockerfile which reduces the final image size.
I suggest to leave the comments and links in for future reference and clarity.
I do not see in the travis.yaml that it is being built and pushed to Dockerhub.