-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
web: | ||
build: . | ||
ports: | ||
- "5000:5000" |