|
| 1 | +# Copyright 2025 The TensorFlow Quantum Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================= |
| 15 | + |
| 16 | +# Summary: build file for making isolated Docker environments for testing. |
| 17 | +# This accepts 2 optional build arguments to set the Ubuntu & Python versions. |
| 18 | +# Usage example: |
| 19 | +# |
| 20 | +# docker build --no-cache --build-arg PYTHON_VERSION=3.10 \ |
| 21 | +# --build-arg UBUNTU_VERSION=24.04 -t my-image:latest . |
| 22 | +# |
| 23 | +# Note that the name and tag ("my-image" and "latest" in the example above) are |
| 24 | +# yours to choose. They're not set using the arguments or linked to their values. |
| 25 | + |
| 26 | +# Default values for build arguments: |
| 27 | +ARG PYTHON_VERSION=3.10 |
| 28 | +ARG UBUNTU_VERSION=22.04 |
| 29 | + |
| 30 | +FROM ubuntu:${UBUNTU_VERSION} |
| 31 | + |
| 32 | +# Make the Python version argument visible to the rest of this file after FROM. |
| 33 | +ARG PYTHON_VERSION |
| 34 | +ENV PYTHON_VERSION=${PYTHON_VERSION} |
| 35 | + |
| 36 | +ENV LANG=C.UTF-8 |
| 37 | +ENV LC_ALL=C.UTF-8 |
| 38 | +ENV DEBIAN_FRONTEND=noninteractive |
| 39 | + |
| 40 | +# Ensure the shell is Bash. |
| 41 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 42 | + |
| 43 | +# Tell the Dockerfile linter not to warn about how we use apt. |
| 44 | +# hadolint global ignore=DL3008,DL3009 |
| 45 | + |
| 46 | +RUN apt-get -q update -q && \ |
| 47 | + apt-get install -y --no-install-recommends ca-certificates \ |
| 48 | + pkg-config gnupg curl lsb-release git zip unzip |
| 49 | + |
| 50 | +# We avoid the use of "add-apt-repository" because it is only available from the |
| 51 | +# "software-properties-common" package, and installing that package brings in a |
| 52 | +# lot of other unrelated unnecessary packages. Instead, we fetch the GPG key for |
| 53 | +# the deadsnakes package archive and install it where apt can find it. |
| 54 | +ENV ID=0xF23C5A6CF475977595C89F51BA6932366A755776 |
| 55 | +ENV GPG_FILE="/etc/apt/trusted.gpg.d/deadsnakes.gpg" |
| 56 | +RUN curl -sSL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=${ID}" | \ |
| 57 | + gpg --dearmor -o ${GPG_FILE} |
| 58 | + |
| 59 | +ENV PPA_URL="https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu" |
| 60 | +ENV APT_FILE="/etc/apt/sources.list.d/deadsnakes.list" |
| 61 | +RUN echo "deb [signed-by=${GPG_FILE}] ${PPA_URL} $(lsb_release -cs) main" > "${APT_FILE}" |
| 62 | + |
| 63 | +RUN apt-get -q update -q && \ |
| 64 | + apt-get install -yq --no-install-recommends make clang g++ zlib1g-dev \ |
| 65 | + python${PYTHON_VERSION} python${PYTHON_VERSION}-dev \ |
| 66 | + python${PYTHON_VERSION}-venv python-is-python3 |
| 67 | + |
| 68 | +# Use update-alternatives to make the desired version be the default. |
| 69 | +# hadolint ignore=SC3010 |
| 70 | +RUN python_path="" bin="/usr/bin" && \ |
| 71 | + case "$(lsb_release -rs)" in \ |
| 72 | + "24.04") python_path="${bin}/python3.12" ;; \ |
| 73 | + "22.04") python_path="${bin}/python3.10" ;; \ |
| 74 | + "20.04") python_path="${bin}/python3.8" ;; \ |
| 75 | + *) python_path=$(readlink -f ${bin}/python3) ;; \ |
| 76 | + esac && \ |
| 77 | + update-alternatives --install ${bin}/python3 python3 "${python_path}" 1 && \ |
| 78 | + update-alternatives --install ${bin}/python3 python3 "${bin}/python${PYTHON_VERSION}" 2 |
| 79 | + |
| 80 | +# Install pip, trying ensurepip first and falling back to get-pip.py. |
| 81 | +RUN export PIP_BREAK_SYSTEM_PACKAGES=1 PIP_ROOT_USER_ACTION=ignore && \ |
| 82 | + (python3 -m ensurepip --upgrade --default-pip || \ |
| 83 | + (curl -sS https://bootstrap.pypa.io/get-pip.py | python3)) && \ |
| 84 | + python3 -m pip install --no-cache-dir --upgrade 'pip>19' |
| 85 | + |
| 86 | +# Clean up before finishing. |
| 87 | +RUN apt-get clean |
| 88 | + |
| 89 | +CMD ["/bin/bash"] |
0 commit comments