-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Docker layer caching-friendly workflow with pipenv #3285
Comments
I'm pretty sure the answer you want is to use a Docker Ignore file:
And yeah we can definitely use a documentation note about this, I am sure we have some docker info in the docs, feel free to propose the changes you want to see whenever you work out the best way to structure things (I don't use docker directly so I'll just verify/leave it to the other maintainers to verify) |
@slint , More details on do I do it in Dockerfile: https://tech.zarmory.com/2018/09/docker-multi-stage-builds-for-python-app.html#pipenv |
Hi @haizaar, sorry for the bother but I'm really interested in what you achieved, i.e. slim & lean docker images with pipenv through multi-stage builds. I tried to adapt the example you linked but without success. In particular it's not clear to me where is the step that should include the app sources inside the last stage of your build. Am I missing something? 🤔 Btw my setup only includes a |
Would using the |
I don't think so @Fongshway as my goal is to "cook" a lean & custom image with just what I need to run my application :( |
@fusillicode So eventually both your app and its dependencies end up under I prefer to have Hope this helps. |
P.S. @fusillicode, if you really want to go lean with under 40MB final images, you can use python-minimal images. |
FYI @haizaar I didn’t forget your issue it just wound up being super complicated on the resolver side for some reason. I am very interested in any updates to docker documentation as I am using pipenv in docker myself now and am not that great with it yet :) |
Dan,
Do you think it worth contributing my best practices to official pipenv
docs? (Or may be to Hitchhiker's Guide to Python?)
…On Sat., 8 Dec. 2018, 10:46 Dan Ryan ***@***.*** wrote:
FYI @haizaar <https://github.com/haizaar> I didn’t forget your issue it
just wound up being super complicated on the resolver side for some reason.
I am very interested in any updates to docker documentation as I am using
pipenv in docker myself now and am not that great with it yet :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3285 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AADjWafRAp7UAUWINsHvsjKlLvmZQ2gKks5u2v27gaJpZM4Yv5ep>
.
|
@haizaar I'm really sorry for the lateness of my reply 🙇♂️ Thanks a lot for pointing out the possibile problem and solution but even more for sharing your experience in this matter! Actually I've solved my problem by generating, inside the "builder" stage of my multistage Dockerfile, the If it would be helpful I'll share the Dockerfile :) Thanks a lot for your support, I really appreciate it! 🍻 |
Another workflow we have been investigating is building two images: A dependencies-only image, tagged with the # ./Dockerfile.deps
FROM python:3.6
RUN pip install --upgrade pip pipenv setuptools wheel
RUN mkdir /app
WORKDIR /app
COPY Pipfile Pipfile
COPY Pipfile.lock Pipfile.lock
RUN pipenv install --deploy --system To build the image: $ deps_version=$(jq ._meta.hash.sha256 Pipfile.lock)
$ docker build -t myapp-deps:$deps_version Dockerfile.deps This image can be e.g. built on a regular basis by some cronjob/CI workflow, since it's often the case that dependencies don't change that often, compared to actual application code. For the application image now, we can use # ./Dockefile
ARG DEPS_VERSION=latest
FROM myapp-deps:${DEPS_VERSION}
COPY . /app
RUN pip install .
CMD ["python", "-m", "myapp.run"] To build the image we need to pass the dependencies version via $ deps_version=$(jq ._meta.hash.sha256 Pipfile.lock)
$ docker build -t myapp:1.2.0 --build-arg DEPS_VERSION=$deps_version . One caveat of this method is that, if the specific dependencies image might not have been already built, and thus you have to check your registry and trigger a build if needed before building the application image. |
@fusillicode |
@slint |
@haizaar This is an additional optimization in case you're not building your images locally, but on e.g. a CI/CD service, which runs your |
Thanks @slint, I see your point. |
I'd recommend multistage builds with docker and pipenv
|
@lukasz-madon Thanks for sharing! I don't think I understand your solution; you are installing dependencies twice, once with dev and once without. I also do not see how are you using the build stage - I don't see interactions with it. Could you elaborate? Thank you |
Note added here: This is a recommendation not an absolute truth. |
The usual way to create a Docker-based deployment (e.g. for deploying on Kubernetes) for a Python application looked something like this, using a
requirements.txt
, produced bypip freeze
orpip-compile
:Using
pipenv
, I would imagine the equivalent would be something like:If this is something that others have come across and consider a best practice, I believe it would be useful to make it part of the official documentation, since
pipenv
is meant to be a solution for applications.For example, before that, I thought that the logical thing would be doing something like:
This would make it easy for someone to create an application that could be easily installed locally with just a
pipenv install --dev
. The problem is that now that the application package is part of the Pipfile, Docker layer caching is thrown out of the window (i.e. one has to doADD . /app
much earlier in order forpipenv install --system --deploy
to be able to find the application'ssetup.py
).(This issue is in no way meant to be a complaint or a back in the "pip"-days I used to...-kind of rant. I'm really just hoping for a good discussion with practical advice and seeing how others tackle this issue using "pipenv")
The text was updated successfully, but these errors were encountered: