Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(framework) Add alpine base image #3453

Merged
merged 11 commits into from
May 17, 2024
15 changes: 7 additions & 8 deletions .github/workflows/docker-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ on:
permissions:
contents: read

env:
DEFAULT_UBUNTU: 22.04

jobs:
parameters:
name: Collect build parameters
Expand All @@ -24,7 +21,6 @@ jobs:
outputs:
pip-version: ${{ steps.versions.outputs.pip-version }}
setuptools-version: ${{ steps.versions.outputs.setuptools-version }}
ubuntu-version: ${{ steps.versions.outputs.ubuntu-version }}

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand All @@ -36,7 +32,6 @@ jobs:
run: |
echo "pip-version=${{ steps.bootstrap.outputs.pip-version }}" >> "$GITHUB_OUTPUT"
echo "setuptools-version=${{ steps.bootstrap.outputs.setuptools-version }}" >> "$GITHUB_OUTPUT"
echo "ubuntu-version=${{ env.DEFAULT_UBUNTU }}" >> "$GITHUB_OUTPUT"

build-base-images:
name: Build base images
Expand All @@ -46,15 +41,19 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
distro:
- name: ubuntu
version: 22.04
with:
namespace-repository: flwr/base
file-dir: src/docker/base
file-dir: src/docker/base/${{ matrix.distro.name }}
build-args: |
PYTHON_VERSION=${{ matrix.python-version }}
PIP_VERSION=${{ needs.parameters.outputs.pip-version }}
SETUPTOOLS_VERSION=${{ needs.parameters.outputs.setuptools-version }}
UBUNTU_VERSION=${{ needs.parameters.outputs.ubuntu-version }}
tags: py${{ matrix.python-version }}-ubuntu${{ needs.parameters.outputs.ubuntu-version }}
DISTRO=${{ matrix.distro.name }}
DISTRO_VERSION=${{ matrix.distro.version }}
tags: py${{ matrix.python-version }}-${{ matrix.distro.name }}${{ matrix.distro.version }}
secrets:
dockerhub-user: ${{ secrets.DOCKERHUB_USERNAME }}
dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
2 changes: 1 addition & 1 deletion doc/source/contributor-how-to-build-docker-images.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The following example creates a base image with Python 3.11.0, pip 23.0.1 and se

.. code-block:: bash

$ cd src/docker/base/
$ cd src/docker/base/ubuntu
Moep90 marked this conversation as resolved.
Show resolved Hide resolved
$ docker build \
--build-arg PYTHON_VERSION=3.11.0 \
--build-arg PIP_VERSION=23.0.1 \
Expand Down
85 changes: 85 additions & 0 deletions src/docker/base/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2024 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# Multi-stage base image build
#
# Building the base image for ARM64 requires installing some additional system dependencies to
# compile the grpcio package, as they do not provide a pre-built package. However, we don't want
# the dependencies in the final base image as they are only needed to compile the package.
# That's why we're creating a multi-stage build. When installing the flwr, we are using a
# virtual environment to keep all files in a single isolated directory as described here:
# https://pythonspeed.com/articles/multi-stage-docker-python/

# hadolint global ignore=DL3018
ARG PYTHON_VERSION=3.11
ARG DISTRO=alpine
ARG DISTRO_VERSION=3.19
FROM python:${PYTHON_VERSION}-${DISTRO}${DISTRO_VERSION} as compile

# Install system dependencies
RUN apk add --no-cache \
# require for compiling grpcio on ARM64
g++ \
libffi-dev \
# create virtual env
&& python -m venv /opt/venv

# Make sure we use the virtualenv
ENV PATH="/opt/venv/bin:$PATH"

# Install specific version of pip, setuptools and flwr
ARG PIP_VERSION
ARG SETUPTOOLS_VERSION
ARG FLWR_VERSION
ARG FLWR_PACKAGE=flwr
RUN pip install -U --no-cache-dir \
pip==${PIP_VERSION} \
setuptools==${SETUPTOOLS_VERSION} \
${FLWR_PACKAGE}==${FLWR_VERSION}

FROM python:${PYTHON_VERSION}-${DISTRO}${DISTRO_VERSION} as base

# required by the grpc package
RUN apk add --no-cache \
libstdc++

COPY --from=compile /opt/venv /opt/venv

# Make sure we use the virtualenv
ENV PATH="/opt/venv/bin:$PATH" \
# Send stdout and stderr stream directly to the terminal. Ensures that no
# output is retained in a buffer if the application crashes.
PYTHONUNBUFFERED=1 \
# Typically, bytecode is created on the first invocation to speed up following invocation.
# However, in Docker we only make a single invocation (when we start the container).
# Therefore, we can disable bytecode writing.
PYTHONDONTWRITEBYTECODE=1 \
# Ensure that python encoding is always UTF-8.
PYTHONIOENCODING=UTF-8 \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8

# add non-root user
RUN adduser \
--no-create-home \
--disabled-password \
--gecos "" \
--uid 49999 app \
&& mkdir -p /app \
&& chown -R app:app /app

WORKDIR /app
USER app
ENV HOME=/app
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
# Copyright 2024 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

ARG UBUNTU_VERSION=22.04
FROM ubuntu:$UBUNTU_VERSION as base
ARG DISTRO=ubuntu
ARG DISTRO_VERSION=22.04
FROM $DISTRO:$DISTRO_VERSION as base

ENV DEBIAN_FRONTEND noninteractive
# Send stdout and stderr stream directly to the terminal. Ensures that no
Expand Down