I have a Dockerfile with ubuntu:22.04 as a base image and a manual Python installation(specific version i.e., 3.11.1) layer which takes a long time.
Approach 1:
- Build a custom_image with ubuntu:22.04 as a base image and a manual Python installation layer.
- Create another Dockerfile with custom_image as the base image and add other layers of the image
- Cache the custom_image using
docker/build-push-action@v5action
Approach 2:
Use
ubuntu-latestrunner withactions/setup-pythonGitHub action with a specific version.Once Python setup is successful, copy Python binaries to Docker context like
cp -r $RUNNER_TOOL_CACHE/Python/3.11.1/* Python/3.11.1Copy these files into docker and update the
PATHvariable like# Copy Python binaries from the GitHub Actions workflow COPY Python /opt/Python # Update PATH to include the copied Python binaries ENV PATH="/opt/Python/3.11.1/x64/bin:/opt/Python/3.11.1/x64:${PATH}" # Set the LD_LIBRARY_PATH to include the copied shared libraries ENV LD_LIBRARY_PATH="/opt/Python/3.11.1/x64/lib:${LD_LIBRARY_PATH}"
Dockerfile:
FROM --platform=linux/amd64 ubuntu:22.04 as base
USER root
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
COPY . /app
WORKDIR /app
COPY ZscalerRootCertificate-2048-SHA256.crt /usr/local/share/ca-certificates/ZscalerRootCertificate-2048-SHA256.crt
# Copy Python binaries from the GitHub Actions workflow
COPY Python /opt/Python
# Update PATH to include the copied Python binaries
ENV PATH="/opt/Python/3.11.1/x64/bin:/opt/Python/3.11.1/x64:${PATH}"
# Set the LD_LIBRARY_PATH to include the copied shared libraries
ENV LD_LIBRARY_PATH="/opt/python/lib:${LD_LIBRARY_PATH}"
# Fail soon than later, if python wasn't installed
RUN python --version
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y software-properties-common ca-certificates && \
update-ca-certificates
RUN python -m pip install -U pip
RUN python3.11 -m pip install --no-cache-dir --trusted-host pypi.org --trusted-host files.pythonhosted.org -r requirements/core-requirements.txt
GitHub workflow:
name: Docker Image CI
env:
CONTAINER_NAME: my-use-case
on:
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
permissions:
id-token: write
contents: write
jobs:
integration-tests:
name: Integration Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: View directory files
run: |
pwd
echo "$PATH"
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11.1"
- name: Copy Python binaries to Docker context
run: |
mkdir -p Python/3.11.1
cp -r $RUNNER_TOOL_CACHE/Python/3.11.1/* Python/3.11.1
- name: View directory files
run: |
ls -lhR
echo "$PATH"
- name: Install dependencies
run: |
python --version
python -m pip install --upgrade pip
- name: View directory files
run: |
ls -lhR
- name: Build & push docker image
uses: docker/build-push-action@v2
with:
context: .
push: false
tags: ${{ env.CONTAINER_NAME }}:${{ github.run_number }}
file: ./Dockerfile