Skip to content

Commit

Permalink
feat: adding slim dockerfile (ApeWorX#2219)
Browse files Browse the repository at this point in the history
Co-authored-by: El De-dog-lo <[email protected]>
  • Loading branch information
johnson2427 and fubuloubu authored Aug 19, 2024
1 parent 6b56755 commit 7dd9d4c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 28 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 14 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 47 additions & 0 deletions Dockerfile.slim
Original file line number Diff line number Diff line change
@@ -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"]
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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.
Expand Down

0 comments on commit 7dd9d4c

Please sign in to comment.