Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Add dockerfile #234

Merged
merged 4 commits into from
Dec 26, 2023
Merged

Add dockerfile #234

merged 4 commits into from
Dec 26, 2023

Conversation

izellevy
Copy link
Collaborator

@izellevy izellevy commented Dec 21, 2023

Problem

Build/deploy of canopy is not streamlined.

Solution

Add a dockerfile to host canopy.

Explanation

This Dockerfile defines a multi-stage build process for a Python application using Poetry for dependency management. Let's break down each stage and understand what it does:

Stage 1: Python Base

This stage sets up the shared environment variables and creates a virtual environment using the official Python 3.11.7-slim image. Key environment variables include settings for Python, pip, and Poetry. The PYTHONPATH is also configured. The virtual environment is created at /venv.

Stage 2: Builder Base

This stage extends the Python Base stage and adds tools necessary for building dependencies. It installs additional packages using apt-get such as build essentials, Git, Vim, and other dependencies. Poetry is installed, and the build cache is utilized to speed up the build process. It sets the working directory to /app and copies pyproject.toml and poetry.lock to install dependencies.

Stage 3: Development

This stage is used during development/testing. It extends the Builder Base stage and copies the project files. It installs development dependencies, and the CMD is set to run a Bash shell.

Stage 4: Production

This is the final stage used for runtime. It extends the Python Base stage and copies the built Poetry and virtual environment from the Builder Base stage. It installs runtime dependencies, copies the project files, and sets up the environment for production. The application is exposed on port 8000, and Gunicorn is configured as the entry point to run the application.

Optimizations and Considerations:

Multi-stage Build:

The use of multi-stage builds helps to keep the final image small by discarding unnecessary build dependencies.
The production image only includes the necessary artifacts for runtime, reducing its size.

Build Cache:

Caching is used effectively during the installation of dependencies to speed up the build process.
Poetry and pip caches are stored in the build cache directory to allow reuse.

Virtual Environment:

A virtual environment is used for dependency isolation.
The virtual environment is created in a separate stage to ensure a clean environment and is then copied to the production image.

Dependency Caching Optimization

As part of the Dockerfile, the optimization of installing dependencies with the --no-root option is employed. This is mentioned in both the Builder Base and Development stages. The --no-root option allows for the caching of dependencies, as they are installed in a location that is not the final root of the system. This can significantly speed up subsequent builds, as the dependencies are cached separately from the application code.

Here's the specific part in the Dockerfile where this optimization is implemented:

Builder Base Stage

RUN --mount=type=cache,target=/root/.cache
poetry install --no-root --all-extras --only main

Development Stage

RUN --mount=type=cache,target=/root/.cache
poetry install --no-root --all-extras --with dev

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

Describe specific steps for validating this change.

@izellevy izellevy marked this pull request as ready for review December 24, 2023 13:54
Copy link
Contributor

@igiloh-pinecone igiloh-pinecone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see a few suggestions

Dockerfile Outdated Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
Copy link
Contributor

@igiloh-pinecone igiloh-pinecone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Makefile Show resolved Hide resolved
@izellevy izellevy added this pull request to the merge queue Dec 26, 2023
Merged via the queue into pinecone-io:main with commit e9cef6e Dec 26, 2023
10 checks passed
@izellevy izellevy deleted the feature/add_dockerfile branch December 26, 2023 13:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants