Skip to content

Commit

Permalink
build and publish docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Dec 13, 2023
1 parent a9ef524 commit 0a12f0a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Publish a Docker image

on:
push:
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
docker:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
50 changes: 16 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
# The builder image, used to build the virtual environment
# from https://stackoverflow.com/questions/72465421/how-to-use-poetry-with-docker
FROM python:3.11-bookworm AS builder

# Configure Poetry
ENV POETRY_VERSION=1.7.1
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VENV=/opt/poetry-venv
RUN pip install poetry==1.7.1

# Tell Poetry where to place its cache and virtual environment
ENV POETRY_CACHE_DIR=/opt/.cache
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

# Create stage for Poetry installation
FROM builder AS poetry-base
WORKDIR /api

# Creating a virtual environment just for poetry and install it with pip
RUN python3 -m venv ${POETRY_VENV} \
&& ${POETRY_VENV}/bin/pip install -U pip setuptools \
&& ${POETRY_VENV}/bin/pip install poetry==${POETRY_VERSION}
COPY pyproject.toml poetry.lock ./
RUN touch README.md

# Create a new stage from the base python image
FROM builder AS example-app
RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR

# Copy Poetry to app image
COPY --from=poetry-base ${POETRY_VENV} ${POETRY_VENV}
# The runtime image, used to just run the code provided its virtual environment
FROM python:3.11-slim-bookworm AS runtime

# Add Poetry to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"
ENV VIRTUAL_ENV=/api/.venv \
PATH="/api/.venv/bin:$PATH"

WORKDIR /app
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

# Copy Dependencies
COPY poetry.lock pyproject.toml ./
# CMD ["hypercorn", "-b", "0.0.0.0:5000", "api:app"]

# [OPTIONAL] Validate the project is properly configured
RUN poetry check

# Install Dependencies
RUN poetry install --no-interaction --no-cache --without dev

# Copy Application
COPY . /app

# Run Application
EXPOSE $PORT
CMD [ "poetry", "run", "python", "-m", "flask", "run", "--host=0.0.0.0" ]
CMD ["poetry", "run", "start"]
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
web:
build: .
ports:
- "5000:5000"

0 comments on commit 0a12f0a

Please sign in to comment.