From 859d730290ddf53d58d5813c9c0868b80c134530 Mon Sep 17 00:00:00 2001 From: Pierre Gerardi Date: Wed, 16 Aug 2023 10:55:46 +0200 Subject: [PATCH] feat: Docker files to build gpu images --- .github/dependabot.yml | 22 ++++++++ .github/workflows/publish.yml | 45 ++++++++++++++++ .gitignore | 64 +++++++++++++++++++++++ Dockerfile | 98 +++++++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/publish.yml create mode 100644 .gitignore create mode 100644 Dockerfile diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..e98ee6e --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,22 @@ +version: 2 + +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: monthly + commit-message: + prefix: "ci" + prefix-development: "ci" + include: "scope" + - package-ecosystem: pip + directory: / + schedule: + interval: monthly + commit-message: + prefix: "build" + prefix-development: "build" + include: "scope" + versioning-strategy: lockfile-only + allow: + - dependency-type: "all" diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..bb86147 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,45 @@ +name: Build and deploy + +on: + push: + branches: + - main + - master + +jobs: + build_and_push_to_dockerhub: + runs-on: ubuntu-latest + + strategy: + matrix: + version: + - python: "3.8" + tag: "cuda11.8-python3.8" + - python: "3.9" + tag: "cuda11.8-python3.9" + - python: "3.10" + tag: "cuda11.8-python3.10" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + registry: my.registry.com + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Setup Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + build-args: PYTHON_VERSION=${{ matrix.version.python }} + tags: my_registry.com/${{ matrix.version.tag }}:$ + cache-from: type=registry,ref=my_registry.com/${{ matrix.version.tag }}:latest \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b903be --- /dev/null +++ b/.gitignore @@ -0,0 +1,64 @@ +# Coverage.py +htmlcov/ +reports/ + +# cruft +*.rej + +# Data +*.csv* +*.dat* +*.pickle* +*.xls* +*.zip* +data/ + +# direnv +.envrc + +# dotenv +.env + +# Hypothesis +.hypothesis/ + +# Jupyter +*.ipynb +.ipynb_checkpoints/ +notebooks/ + +# macOS +.DS_Store + +# mypy +.dmypy.json +.mypy_cache/ + +# Node.js +node_modules/ + +# Poetry +.venv/ +dist/ + +# PyCharm +.idea/ + +# pyenv +.python-version + +# pytest +.pytest_cache/ + +# Python +__pycache__/ +*.py[cdo] + +# Ruff +.ruff_cache/ + +# Terraform +.terraform/ + +# VS Code +.vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..437bfbb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,98 @@ +# syntax=docker/dockerfile:1 +ARG PYTHON_VERSION=3.8 +FROM python:$PYTHON_VERSION-slim AS base + +# Install Poetry. +ENV POETRY_VERSION 1.4.2 +RUN --mount=type=cache,target=/root/.cache/pip/ \ + pip install poetry~=$POETRY_VERSION + +# Install compilers that may be required for certain packages or platforms. +RUN rm /etc/apt/apt.conf.d/docker-clean +RUN --mount=type=cache,target=/var/cache/apt/ \ + --mount=type=cache,target=/var/lib/apt/ \ + apt-get update && \ + apt-get install --no-install-recommends --yes build-essential + +# Create a non-root user and switch to it [1]. +# [1] https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user +ARG UID=1000 +ARG GID=$UID +RUN groupadd --gid $GID user && \ + useradd --create-home --gid $GID --uid $UID user --no-log-init && \ + chown user /opt/ +USER user + +# Create and activate a virtual environment. +RUN python -m venv /opt/pyhton-gpu-env +ENV PATH /opt/pyhton-gpu-env/bin:$PATH +ENV VIRTUAL_ENV /opt/pyhton-gpu-env + +# Set the working directory. +WORKDIR /workspaces/pyhton-gpu/ + +# Install the run time Python dependencies in the virtual environment. +COPY --chown=user:user poetry.lock* pyproject.toml /workspaces/pyhton-gpu/ +RUN mkdir -p /home/user/.cache/pypoetry/ && mkdir -p /home/user/.config/pypoetry/ && \ + mkdir -p src/pyhton_gpu/ && touch src/pyhton_gpu/__init__.py && touch README.md +RUN --mount=type=cache,uid=$UID,gid=$GID,target=/home/user/.cache/pypoetry/ \ + poetry install --only main --no-interaction + + + +FROM base as ci + +# Allow CI to run as root. +USER root + +# Install git so we can run pre-commit. +RUN --mount=type=cache,target=/var/cache/apt/ \ + --mount=type=cache,target=/var/lib/apt/ \ + apt-get update && \ + apt-get install --no-install-recommends --yes git + +# Install the CI/CD Python dependencies in the virtual environment. +RUN --mount=type=cache,target=/root/.cache/pypoetry/ \ + poetry install --only main,test --no-interaction + + + +FROM base as dev + +# Install development tools: curl, git, gpg, ssh, starship, sudo, vim, and zsh. +USER root +RUN --mount=type=cache,target=/var/cache/apt/ \ + --mount=type=cache,target=/var/lib/apt/ \ + apt-get update && \ + apt-get install --no-install-recommends --yes curl git gnupg ssh sudo vim zsh && \ + sh -c "$(curl -fsSL https://starship.rs/install.sh)" -- "--yes" && \ + usermod --shell /usr/bin/zsh user && \ + echo 'user ALL=(root) NOPASSWD:ALL' > /etc/sudoers.d/user && chmod 0440 /etc/sudoers.d/user +USER user + +# Install the development Python dependencies in the virtual environment. +RUN --mount=type=cache,uid=$UID,gid=$GID,target=/home/user/.cache/pypoetry/ \ + poetry install --no-interaction + +# Persist output generated during docker build so that we can restore it in the dev container. +COPY --chown=user:user .pre-commit-config.yaml /workspaces/pyhton-gpu/ +RUN mkdir -p /opt/build/poetry/ && cp poetry.lock /opt/build/poetry/ && \ + git init && pre-commit install --install-hooks && \ + mkdir -p /opt/build/git/ && cp .git/hooks/commit-msg .git/hooks/pre-commit /opt/build/git/ + +# Configure the non-root user's shell. +ENV ANTIDOTE_VERSION 1.8.6 +RUN git clone --branch v$ANTIDOTE_VERSION --depth=1 https://github.com/mattmc3/antidote.git ~/.antidote/ && \ + echo 'zsh-users/zsh-syntax-highlighting' >> ~/.zsh_plugins.txt && \ + echo 'zsh-users/zsh-autosuggestions' >> ~/.zsh_plugins.txt && \ + echo 'source ~/.antidote/antidote.zsh' >> ~/.zshrc && \ + echo 'antidote load' >> ~/.zshrc && \ + echo 'eval "$(starship init zsh)"' >> ~/.zshrc && \ + echo 'HISTFILE=~/.history/.zsh_history' >> ~/.zshrc && \ + echo 'HISTSIZE=1000' >> ~/.zshrc && \ + echo 'SAVEHIST=1000' >> ~/.zshrc && \ + echo 'setopt share_history' >> ~/.zshrc && \ + echo 'bindkey "^[[A" history-beginning-search-backward' >> ~/.zshrc && \ + echo 'bindkey "^[[B" history-beginning-search-forward' >> ~/.zshrc && \ + mkdir ~/.history/ && \ + zsh -c 'source ~/.zshrc'