From 7dd9d4c443445331b22544ce3f6c1f10f631e1ed Mon Sep 17 00:00:00 2001 From: johnson2427 <37009091+johnson2427@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:40:44 -0500 Subject: [PATCH] feat: adding slim dockerfile (#2219) Co-authored-by: El De-dog-lo <3859395+fubuloubu@users.noreply.github.com> --- .github/workflows/build.yaml | 80 ++++++++++++++++++++++++++++++++++++ Dockerfile | 41 +++++++----------- Dockerfile.slim | 47 +++++++++++++++++++++ README.md | 30 +++++++++++++- 4 files changed, 170 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/build.yaml create mode 100644 Dockerfile.slim diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000000..669624aaa9 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,80 @@ +name: Container Images +on: + push: + branches: + - main + pull_request: + release: + types: [published] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=pr + type=ref,event=branch,branch=main,latest=true + type=ref,event=branch,branch=main + type=ref,event=branch,branch!=main + type=semver,pattern={{version}},branch=main,latest=false + type=ref,event=tag + + - name: Show tags + run: | + git tag + echo ${{ steps.meta.outputs.tags }} + + - name: Extract version from tag + if: startsWith(github.ref, 'refs/tags/') + run: echo "VERSION=${GITHUB_REF##*/}" >> $GITHUB_ENV + + - name: Log into GitHub Container Registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push slim + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile.slim + tags: ${{ steps.meta.outputs.tags }}-slim + labels: ${{ steps.meta.outputs.labels }} + build-args: | + BUILD_DATE=${{ github.event.repository.updated_at }} + VCS_REF=${{ github.sha }} + VERSION=${{ env.VERSION }} + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + SLIM_IMAGE=${{ steps.meta.outputs.tags }}-slim diff --git a/Dockerfile b/Dockerfile index 83006b5f48..45433f2b50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,43 +2,30 @@ # See LICENSE in the project root for license information. #--------------------------------------------------------------------------------------------- +ARG SLIM_IMAGE ARG PYTHON_VERSION="3.11" -ARG PLUGINS_FILE="./recommended-plugins.txt" - -FROM python:${PYTHON_VERSION} as builder +FROM python:${PYTHON_VERSION} AS builder WORKDIR /wheels -COPY ./recommended-plugins.txt ./recommended-plugins.txt +RUN pip install --upgrade pip \ + && pip install wheel + COPY . . -RUN pip install --upgrade pip \ - && pip install wheel \ - && pip wheel .[recommended-plugins] --wheel-dir=/wheels +COPY ./recommended-plugins.txt ./recommended-plugins.txt -FROM python:${PYTHON_VERSION}-slim +RUN pip wheel .[recommended-plugins] --wheel-dir=/wheels -# See http://label-schema.org for metadata schema -# TODO: Add `build-date` and `version` -LABEL maintainer="ApeWorX" \ - org.label-schema.schema-version="2.0" \ - org.label-schema.name="ape" \ - org.label-schema.description="Ape Ethereum Framework." \ - org.label-schema.url="https://docs.apeworx.io/ape/stable/" \ - org.label-schema.usage="https://docs.apeworx.io/ape/stable/userguides/quickstart.html#via-docker" \ - org.label-schema.vcs-url="https://github.com/ApeWorX/ape" \ - org.label-schema.docker.cmd="docker run --volume $HOME/.ape:/home/harambe/.ape --volume $HOME/.vvm:/home/harambe/.vvm --volume $HOME/.solcx:/home/harambe/.solcx --volume $PWD:/home/harambe/project --workdir /home/harambe/project apeworx/ape compile" +FROM ${SLIM_IMAGE} AS ape_slim -RUN useradd --create-home --shell /bin/bash harambe +USER root -COPY --from=builder /wheels /wheels -COPY ./recommended-plugins.txt ./recommended-plugins.txt +COPY --from=builder /wheels/*.whl /wheels/ -RUN pip install --upgrade pip \ - pip install --no-cache-dir --find-links=/wheels -r ./recommended-plugins.txt \ - && ape --version +RUN pip install --upgrade pip +RUN pip install /wheels/*.whl -WORKDIR /home/harambe/project -RUN chown --recursive harambe:harambe /home/harambe USER harambe -ENTRYPOINT ["ape"] + +RUN ape --version diff --git a/Dockerfile.slim b/Dockerfile.slim new file mode 100644 index 0000000000..e5de65e1b4 --- /dev/null +++ b/Dockerfile.slim @@ -0,0 +1,47 @@ +#--------------------------------------------------------------------------------------------- +# See LICENSE in the project root for license information. +#--------------------------------------------------------------------------------------------- + +ARG PYTHON_VERSION="3.11" + +FROM python:${PYTHON_VERSION} AS builder + +WORKDIR /wheels + +RUN pip install --upgrade pip \ + && pip install wheel + +COPY . . + +RUN pip wheel . + +FROM python:${PYTHON_VERSION}-slim + +# See http://label-schema.org for metadata schema +# TODO: Add `build-date` and `version` +LABEL org.opencontainers.image.title="ape" \ + org.opencontainers.image.description="Ape Framework" \ + org.opencontainers.image.url="https://apeworx.io/framework" \ + org.opencontainers.image.documentation="https://docs.apeworx.io/ape/stable/userguides/quickstart.html\#installation" \ + org.opencontainers.image.source="https://github.com/ApeWorX/ape" \ + org.opencontainers.image.vendor="ApeWorX" \ + org.opencontainers.image.licenses="Apache-2.0" \ + org.opencontainers.image.version="${VERSION:-latest}" \ + org.opencontainers.image.created="${BUILD_DATE}" \ + org.opencontainers.image.revision="${VCS_REF}" \ + org.opencontainers.image.authors="ApeWorX" \ + org.opencontainers.image.base.name="python:${PYTHON_VERSION}-slim" + + +RUN useradd --create-home --shell /bin/bash harambe +COPY --from=builder /wheels/*.whl /wheels/ + +RUN pip install --upgrade pip +RUN pip install /wheels/*.whl + +RUN ape --version + +WORKDIR /home/harambe/project +RUN chown --recursive harambe:harambe /home/harambe +USER harambe +ENTRYPOINT ["ape"] diff --git a/README.md b/README.md index 6505f3763c..341080d989 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,32 @@ There are three ways to install ape: `pipx`, `pip`, or `Docker`. Ape can also run in a docker container. -Please visit our [Dockerhub](https://hub.docker.com/repository/docker/apeworx/ape) for more details on using Ape with Docker. +You can pull our images from [ghcr](https://ghcr.io/apeworx/ape). +This image is built using our `recommended-plugins` extra, so it is a great starting point for running ape in a containerized environment. + +We also have a `slim` docker image that is built without any installed plugins. +This image is meant for production support and must be further configured if any plugins are in use. + +You can pull the image: + +```bash +$ docker pull ghcr.io/apeworx/ape:latest # installs with recommended-plugins +``` + +or pull the slim if you have specific needs that you'd like to build from: + +```bash +$ docker pull ghcr.io/apeworx/ape:latest-slim # installs ape with required packages +``` + +or build the image locally from source: + +```bash +$ docker build -t ape:latest-slim -f Dockerfile.slim . +$ docker build -t ape:latest . +``` + +An example of running a command from the container would be: ```bash docker run \ @@ -70,6 +95,9 @@ docker run \ apeworx/ape compile ``` +> **Note:** +> The above command requires the full install which includes `recommended-plugins` installation extra. + ## Quickstart After you have installed Ape, run `ape --version` to verify the installation was successful.