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

makes install available for all users in docker image #3202

Merged
merged 5 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -47,6 +47,7 @@
<!-- For example, Docker, GitHub Actions, pre-commit, editors -->

- Vim plugin: prefix messages with `Black: ` so it's clear they come from Black (#3194)
- Docker: changes the installation path to be available to non-root users (#3202)

### Output

Expand Down
9 changes: 6 additions & 3 deletions Dockerfile
Expand Up @@ -2,16 +2,19 @@ FROM python:3-slim AS builder

RUN mkdir /src
COPY . /src/
ENV VIRTUAL_ENV=/opt/venv
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if this will work - Ignore if it does not ...

Suggested change
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN source /opt/venv/bin/activate

Lets use the inbuilt activate script venv includes ... This way if any fixes are made in future python's venv module we get them for free.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, i agree. Being that RUN instructions are independent from each other we are kind of forced to do this:
RUN . /opt/venv/bin/activate && pip install ...
It works indeed. I'm adding this, let me know what you think about it!

RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
# Install build tools to compile dependencies that don't have prebuilt wheels
&& apt update && apt install -y git build-essential \
&& cd /src \
&& pip install --user --no-cache-dir .[colorama,d]
&& pip install --no-cache-dir .[colorama,d]

FROM python:3-slim

# copy only Python packages to limit the image size
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
Copy link
Collaborator

@cooperlees cooperlees Aug 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this make the PATH persistent like this? i.e.everyone who download and runs commands in this container will get this PATH prepend by default?

Can you add test output of which black for different users to show this working maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will prepend the PATH for all users. Here's a couple examples:

# test output as host user
❯ docker run --user $(id -u):$(id -g) nborges/black:pr which black
/opt/venv/bin/black

# test output as root
❯ docker run --user 0:0 nborges/black:pr which black
/opt/venv/bin/black


CMD ["black"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CMD ["black"]
CMD ["/opt/venv/bin/black"]

Lets do this so it's more evident to people looking that we're in a venv and this is the black binary you should expect to use ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, adding this as well