Skip to content

Does apt install of python3 affect python in slim? #777

Closed

Description

@tisdall

I was looking at reducing my build size by using the slim build and then only including build dependencies while building and then purging them. Everything seems to work fine. However, I noticed that when I include glib that apt pulls in python3. I see in #253 that this is to be expected, but I was wondering if this has any potential in breaking the python already within the container.

Specifically, does having something like this in Dockerfile have any potential in breaking something?

RUN apt-get update && \
    apt-get install -y --no-install-recommends python3 && \
    apt-get purge --autoremove -y python3 && \
    rm -rf /var/lib/apt/lists/*

This is obviously contrived, but does apt adding it's own python3 and removing it alter things that need to not be altered?

Activity

  1. ad-m-ss commented on Dec 19, 2022

    @ad-m-ss

    This is not a direct answer to your question, but if you want to optimize the image, you can think about multi-stage build for building dependencies , then you don't need care to remove Python because it will be in a different stage:

    FROM python:3.10.7-bullseye AS build-pip
    ENV PIP_CACHE_DIR=/var/cache/buildkit/pip
    RUN mkdir -p $PIP_CACHE_DIR
    RUN --mount=type=cache,target=/var/cache/buildkit/pip \
        pip install -U pip wheel
    
    FROM build-pip AS build-app
    COPY ./requirements.txt .
    RUN --mount=type=cache,target=/var/cache/buildkit/pip \
        pip wheel --wheel-dir /wheels -r requirements.txt
    
    FROM python:3.10.7-slim-bullseye
    
    RUN --mount=type=cache,target=/var/cache/apt apt-get update \
        && apt-get install -y --no-install-recommends unixodbc git libxml2 openssh-client \
        && rm -rf /var/lib/apt/lists/*
    ENV PIP_CACHE_DIR=/var/cache/buildkit/pip
    RUN mkdir -p $PIP_CACHE_DIR
    
    # Install requirements
    WORKDIR /app
    COPY requirements.txt ./
    
    COPY --from=build-app /wheels /wheels
    
    RUN --mount=type=cache,target=/var/cache/buildkit/pip \
        pip install --find-links /wheels --no-index -r requirements.txt
    
    COPY . .
  2. tianon commented on Dec 19, 2022

    @tianon