diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..8da193f --- /dev/null +++ b/.github/workflows/publish.yml @@ -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 }} diff --git a/Dockerfile b/Dockerfile index 32d4f36..9fe95a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b0a4283 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,5 @@ +services: + web: + build: . + ports: + - "5000:5000"