Skip to content

Instantly share code, notes, and snippets.

@BillRaymond
Last active December 29, 2024 10:25
Show Gist options
  • Save BillRaymond/b39ae5d862aa81a250b70b812bd5d1cc to your computer and use it in GitHub Desktop.
Save BillRaymond/b39ae5d862aa81a250b70b812bd5d1cc to your computer and use it in GitHub Desktop.
Dockerfile to build a specific version of Python and set it as the default using Deadsnakes
####
# Specify the OS you want to run the Docker container in
# For the latest version of Ubuntu, use:
# FROM ubuntu:latest
# Learn more about this Ubuntu image, and versions (tags):
# https://hub.docker.com/_/ubuntu
####
FROM ubuntu:22.04
#####
# Variables to change before building your image
#
# Define the version of Python to install
# For example "3.11" will get the latest version of 3.11 (ex 3.11.4, 3.11.5, etc)
ARG PYVER="3.11"
# Define the username and email you will use for Git
ARG GITUSERNAME="Your Username"
ARG GITEMAIL="[email protected]"
#####
# These settings prevent a timezone prompt when Python installs
# Use this article to find your time zone (TZ):
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
ENV TZ=US/Pacific \
DEBIAN_FRONTEND=noninteractive
# Update the apt repo
RUN apt-get update
# Install Python (software-properties-common), Git, and Python utilities
# Learn about the deadsnakes Personal Package Archives, hosted by Ubuntu:
# https://www.youtube.com/watch?v=Xe40amojaXE
RUN apt-get install -y software-properties-common && \
add-apt-repository -y ppa:deadsnakes/ppa && \
apt-get install -y python$PYVER \
python3-pip \
git-all
# Upgrade packages to the latest version
RUN apt-get -y upgrade
# Update PIP (Python's package manager)
RUN python3 -m pip install --upgrade pip
# Configure git
RUN git config --global user.name "$GITUSERNAME" &&\
git config --global user.email $GITEMAIL &&\
git config --global init.defaultBranch main
# Set PYVER as the default Python interpreter
RUN update-alternatives --install /usr/bin/python3 python /usr/bin/python$PYVER 1
RUN update-alternatives --set python /usr/bin/python$PYVER
RUN update-alternatives --set python /usr/bin/python$PYVER
@AvocadoStyle
Copy link

I just came accross this Dockerfile while I was looking for a Dockerfile reference on google, I have few things to say about this dockerfile:
seems like

# Upgrade packages to the latest version
RUN apt-get -y upgrade

it is not a best practice for Dockerfile builds since it can lead to instability and inconsistent behavior across builds. Use only apt-get update rather than upgrade and install specific versions of critical dependencies when possible.

seems like you forgot to clean the cache and the apt repositories that already installed, take care for that too such as:

apt-get clean && apt-get autoclean && rm -rf /var/cache/apt/archives /var/lib/apt/lists/*

maybe the

/var/cache/apt/archives 

is unecessary because the apt-get autoclean already does that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment