You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docker/pipenv-and-docker.md
+31-24Lines changed: 31 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
I had [a Django project](https://github.com/natbat/cbwg) that used `pipenv` (in particular a `Pipfile.lock`) to manage dependencies and I wanted to build a Docker container for it.
4
4
5
-
This worked:
5
+
With the help of [this article](https://sourcery.ai/blog/python-docker/) (for the `PIPENV_VENV_IN_PROJECT` tip) I came up with the following:
The key trick here is using `pipenv sync --system` to install into the system Python, rather than trying to create a virtual environment.
26
-
27
-
(`--system` trick courtesy of [feedback on Mastodon](https://social.lol/@ryan/109424753653794299))
28
-
29
-
## Previous method before learning about --system
27
+
Ignore the base image - the project was an emergency port from Heroku and I didn't have time to upgrade it from the ancient version of Python it was using (if you're using a `Pipfile.lock` file you need to keep your Python version stable unless you want to lock new versions).
30
28
31
-
With the help of [this article](https://sourcery.ai/blog/python-docker/) (for the `PIPENV_VENV_IN_PROJECT` tip) I came up with the following:
29
+
The key lines here are these ones:
32
30
33
31
```dockerfile
34
-
FROM python:3.6.15-slim-buster
35
-
36
32
RUN mkdir -p /app
37
33
WORKDIR /app
38
34
@@ -42,33 +38,44 @@ ENV PIPENV_VENV_IN_PROJECT=1
42
38
43
39
RUN pip install pipenv
44
40
RUN pipenv sync
41
+
```
42
+
First we create a `/app` directory and set that as the working directory.
45
43
46
-
RUN pipenv run ./manage.py collectstatic --noinput
44
+
`COPY . .` copies ALL files and directories from the `Dockerfile` directory into the new image, inside `/app`.
47
45
48
-
EXPOSE 8000
46
+
`ENV PIPENV_VENV_IN_PROJECT=1` is important: it causes the resuling virtual environment to be created as `/app/.venv`. Without this the environment gets created somewhere surprising, such as `/root/.local/share/virtualenvs/app-4PlAip0Q` - which makes it much harder to write automation scripts later on.
Then we install `pipenv` and use `RUN pipenv sync` to install all of the package versions from our `Pipfile.lock` file (which was added by `COPY . .`).
52
49
53
-
Ignore the base image - the project was an emergency port from Heroku and I didn't have time to upgrade it from the ancient version of Python it was using (if you're using a `Pipfile.lock` file you need to keep your Python version stable unless you want to lock new versions).
50
+
## Alternative using pipenv sync --system
54
51
55
-
The key lines here are these ones:
52
+
You can avoid creating a virtual environment in your Docker image entirely using `pipenv sync --system`, which instead installs the packages in the Docker container's system Python:
First we create a `/app` directory and set that as the working directory.
69
71
70
-
`COPY . .` copies ALL files and directories from the `Dockerfile` directory into the new image, inside `/app`.
72
+
But this carries its own risks, as [described here by Hynek Schlawack](https://hynek.me/articles/virtualenv-lives/).
73
+
74
+
The key problem is that the operating system itself may have already installed specific package versions in `site-packages`, and there's a risk that these might conflict with the versions of those packages used by your own application.
75
+
76
+
So Hynek advises:
77
+
78
+
> Do isolate your application server’s OS from its host using Docker [...]
79
+
> But inside of them also do isolate your Python environment using virtualenv from unexpected surprises in the system site-packages.
71
80
72
-
`ENV PIPENV_VENV_IN_PROJECT=1` is important: it causes the resuling virtual environment to be created as `/app/.venv`. Without this the environment gets created somewhere surprising, such as `/root/.local/share/virtualenvs/app-4PlAip0Q` - which makes it much harder to write automation scripts later on.
73
81
74
-
Then we install `pipenv` and use `RUN pipenv sync` to install all of the package versions from our `Pipfile.lock` file (which was added by `COPY . .`).
0 commit comments