Skip to content
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

Use multi-stage docker builds #247

Open
wants to merge 3 commits into
base: new-features
Choose a base branch
from

Conversation

smitpsanghavi
Copy link

@smitpsanghavi smitpsanghavi commented Feb 1, 2025

  • Update Docker build to multi-stage build.
  • Use python's venv feature to isolate the dependencies to one location.
  • Add a .dockerignore file to ignore files not required within the final image.
  • Cache python dependencies locally on file-system for faster local development.
  • Linted README.md

@pranjal-joshi
Copy link
Owner

pranjal-joshi commented Feb 1, 2025

@smitpsanghavi

Thanks for your contribution. 🎉

Let's discuss/resolve few comments that I've added before merging this.

Also, could you provide the image size after multistage build?

@smitpsanghavi
Copy link
Author

This is what I see in my Docker Desktop locally.
Your image vs the one built using this Dockerfile.

image

The size on Docker hub for v2.24 is 2.04 GB as it is compressed. I don't know much the updated one's size would be after compression.

@smitpsanghavi
Copy link
Author

I don't see any comments of yours anywhere. Where did you add them?

@pranjal-joshi
Copy link
Owner

@smitpsanghavi Let me try creating a docker image with the modified dockerfile and compare the size on Dockerhub as well

The comments are added as a code review which might be visible in the 'Files' section of this PR

@smitpsanghavi
Copy link
Author

Nothing in the conversations section of the PR.

image

Did you submit the comments after reviewing them?

Dockerfile Show resolved Hide resolved
requirements.txt Show resolved Hide resolved
.dockerignore Outdated Show resolved Hide resolved
@pranjal-joshi
Copy link
Owner

@smitpsanghavi I was not able to observe a significant reduction in size either on my end or on dockerhub repo after a push. Seems like dockerhub image size is increased after multistage.

image

image

@smitpsanghavi
Copy link
Author

With regards to the reduction in size, I think something went wrong while building the image in your machine.

I analysed your image and saw that when the command COPY . . is run, the whole git repo (including the complete git history) was added into the image that you pushed. I also see a few other files/folders in the image which I explicitly ignored via .dockerignore. I am not sure why .dockerignore did not have any effect on your system. Hence, you did not see any size reduction.

image

Here's a comparison:

Your image

image

My image

image

I would suggest checking why .dockerignore didn't work for you and fixing it. Once done, you should see massive improvements in the pushed image.

@pranjal-joshi
Copy link
Owner

@smitpsanghavi Got it. I quickly cloned the updated docker file on my local for testing without creating any other files. Let me try building this branch itself.

@pranjal-joshi
Copy link
Owner

There is a substantial reduction in the size after a multistage build! Merging this change after running a functional test @smitpsanghavi
image

@smitpsanghavi
Copy link
Author

I didn't expect a 50% reduction on Docker Hub! 😲
The magic was in the .dockerignore actually.
Multistage also helps and is generally good practice to help your docker image cleaner by removing build time dependencies.

You can reduce this further if you remove any other unnecessary apt-get installations (e.g. git). I'll leave that to you.

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.

2 participants