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

Building docker image and then switching to non root user #7758

Open
adiberk opened this issue Sep 28, 2024 · 0 comments
Open

Building docker image and then switching to non root user #7758

adiberk opened this issue Sep 28, 2024 · 0 comments

Comments

@adiberk
Copy link

adiberk commented Sep 28, 2024

My company does a two part build process in the docker image
We first install dependencies and then we build the image using a non root user

When using poetry we just copy the .venv folder to the new workdir in the image creation step and everything works fine. However when I do this with UV and run the app, I was getting issues bash: line 1: /app/.venv/bin/uvicorn: cannot execute: required file not found even though they are 100% there. After reviewing further I see it might be a symlink issue pointing to a path in first build that doesn't exist in second build. I tried setting link-mode to copy, but I assume the core issue here is I lack an understanding of what is really going on here.
Any advice would be greatly appreciated!
(WE do 2 part built for slight speed improvements, but isn't 100% necessary - seeing if we can just do it all in one go)

Here is a slimmed down version of what we are doing (using UV)

FROM python:3.11.4-bookworm AS build
ARG ENV

COPY --from=ghcr.io/astral-sh/uv:0.4.17 /uv /bin/uv

# SET ENV
ENV LIBRARY_PATH=/lib:/usr/lib

# Prepare install
WORKDIR /app
COPY pyproject.toml uv.lock ./

RUN uv sync --frozen --no-install-project

# Run second stage of build
FROM python:3.11.4-slim

RUN useradd --create-home appuser

# create /app directory and chown to to node user or else it will be owned by root
RUN mkdir -p /app && chown appuser:appuser /app
WORKDIR /app

ENV LIBRARY_PATH=/lib:/usr/lib

# Copy files from build stage
COPY --chown=appuser:appuser --from=build /app/.venv /app/.venv

COPY --chown=appuser:appuser . /app

ENV VIRTUAL_ENV=/app/.venv
ENV PATH="/app/.venv/bin:$PATH"

ENV PYTHONHASHSEED=0
USER lev
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

No branches or pull requests

1 participant