Bradley Kirton's Blog

Published on May 31, 2024

Go home

Installing Python packages from private repos within Docker

This snippet will install a Python package from a private github repository without exposing your ssh-keys. Before you get started make sure you have the docker buildx plugin installed.

requirements.prod.txt

# Assuming requests were private
requests@git+ssh://git@github.com:psf/requests@main

Dockerfile

FROM python:3.11.6-slim-bullseye

# Git and ssh are required
RUN apt-get update && apt-get install -y ssh git

COPY requirements.prod.txt requirements.prod.txt

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN pip install -U pip==24.0 wheel==0.43.0
RUN python -m venv .venv
RUN --mount=type=ssh .venv/bin/python -m pip install -r requirements.prod.txt
docker build --ssh default=$HOME/.ssh/id_rsa -t my-tag .