Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C4GT Community]: Containerize using Dockerfile #73

Closed
9 tasks done
codecakes opened this issue Sep 14, 2024 · 2 comments · Fixed by #75
Closed
9 tasks done

[C4GT Community]: Containerize using Dockerfile #73

codecakes opened this issue Sep 14, 2024 · 2 comments · Fixed by #75
Assignees
Labels
C4GT Coding C4GT Community enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@codecakes
Copy link
Contributor

codecakes commented Sep 14, 2024

Ticket Contents

Description

The basic skeletal works great as a first timer setup but now issues such as #26 and #12 involve non-trivial layers and add more IO sophistication that we cannot do without containerization.

A fast dockerfile boostrap should work good for the project such as: https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile

Goals

Goals

  • Understand the nuances and low hanging fruit in Makefile before containerization
  • Add dockerfile and docker compose that can setup the correct python version as per pyproject.toml
  • Docker environment should setup python version with sqlite extensions loadable

Expected Outcome

  • Running docker does not change the cli Make commands for developer.
  • make test-integration now runs on docker
  • make run now runs on docker

Acceptance Criteria

  • Compiled SQLite with loadable extension support
  • Compiled Python to link against the custom SQLite with extension support
  • Local run and IO tests are docker runnable

Implementation Details

Reference dockerfile:

# Use the base image specified
ARG VERSION=3.12.6
ARG BUILDER=docker.io/library/python
FROM ${BUILDER}:${VERSION}-slim

# Set the working directory
WORKDIR /app

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    wget \
    ca-certificates \
    libreadline-dev \
    libncursesw5-dev \
    libssl-dev \
    libffi-dev \
    zlib1g-dev \
    libbz2-dev \
    liblzma-dev \
    libsqlite3-dev \
    tk-dev \
    uuid-dev \
    git \
    pkg-config \
    libgeos-dev \
    libproj-dev \
    libxml2-dev \
    libxmlsec1-dev \
    libgdal-dev \
    libsqlite3-mod-spatialite \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Update CA certificates
RUN update-ca-certificates 2>/dev/null || true

# Create nonroot user and group
RUN addgroup --system nonroot && adduser --system --ingroup nonroot nonroot

# Change ownership of /app and /var/cache
RUN chown -R nonroot:nonroot /app
RUN mkdir -p /var/cache
RUN chown -R nonroot:nonroot /var/cache

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

ENV POETRY_NO_INTERACTION=1
ENV POETRY_VIRTUALENVS_CREATE=false
ENV POETRY_CACHE_DIR='/var/cache/pypoetry'
ENV POETRY_HOME='/usr/local'

# Compile SQLite with loadable extension support
ENV SQLITE_VERSION=3430100  # Corresponds to SQLite 3.43.1
RUN mkdir /build && cd /build && \
    wget https://www.sqlite.org/2023/sqlite-autoconf-${SQLITE_VERSION}.tar.gz && \
    tar xzf sqlite-autoconf-${SQLITE_VERSION}.tar.gz && \
    cd sqlite-autoconf-${SQLITE_VERSION} && \
    ./configure --enable-load-extension --prefix=/usr/local && \
    make -j"$(nproc)" && \
    make install && \
    rm -rf /build

# Remove the pre-installed Python to avoid conflicts
RUN apt-get remove -y python3

# Recompile Python to link against the custom SQLite
ENV PYTHON_VERSION=${VERSION}
RUN mkdir /build && cd /build && \
    wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz && \
    tar xzf Python-${PYTHON_VERSION}.tgz && \
    cd Python-${PYTHON_VERSION} && \
    ./configure \
        --enable-optimizations \
        --with-ensurepip=install \
        --enable-loadable-sqlite-extensions \
        LDFLAGS="-L/usr/local/lib" \
        CPPFLAGS="-I/usr/local/include" \
        PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" && \
    make -j"$(nproc)" && \
    make altinstall && \
    rm -rf /build

# Update alternatives to point to the new Python
RUN ln -sf /usr/local/bin/python${PYTHON_VERSION%.*} /usr/local/bin/python3
RUN ln -sf /usr/local/bin/pip${PYTHON_VERSION%.*} /usr/local/bin/pip3

# Install Poetry using the recompiled Python
RUN curl -sSL https://install.python-poetry.org | python3 - && \
    ln -s /root/.local/bin/poetry /usr/local/bin/poetry

# Copy the application code
COPY --chown=nonroot:nonroot . .

# Install project dependencies
ARG INSTALL_CMD="poetry install --no-root --all-extras --no-ansi"
RUN if [ ! -z "${INSTALL_CMD}" ]; then sh -c "$INSTALL_CMD";  fi

# Switch to nonroot user
USER nonroot:nonroot

# Set the start command
ARG START_CMD="make run"
ENV START_CMD=${START_CMD}
RUN if [ -z "${START_CMD}" ]; then echo "Unable to detect a container start command" && exit 1; fi
CMD ${START_CMD}

Mockups/Wireframes

No response

Product Name

project-healthcare

Organisation Name

XCoV19

Domain

Healthcare

Tech Skills Needed

CI/CD, Database, Docker, Python

Mentor(s)

@codecakes

Complexity

High

Category

Backend, Configuration, CI/CD, Database, Delpoyment, Refactoring

@JiyaGupta-cs
Copy link
Collaborator

@codecakes Please assign this to me

codecakes added a commit that referenced this issue Sep 20, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 20, 2024
… service (#75)

resolves #73

adds:

- Dockerfile.build: This is the base multi-stage image.
- Dockerfile: Main image to run the service using docker compose.
- Dockerfile.test-integration: For running integration tests.

<!-- Generated by sourcery-ai[bot]: start summary -->

## Summary by Sourcery

Add Docker support for integration testing and running the service in a
containerized environment. Update documentation to guide developers on
setting up and using Docker for local testing and service emulation.
Introduce Dockerfiles for building base, main, and integration test
images, and add a Docker Compose configuration for service deployment.

New Features:
- Introduce Docker support for integration testing and running the
service in a containerized environment using Docker Compose.

Enhancements:
- Update CONTRIBUTING.md and README.md to include instructions for
setting up and running the containerized application, enhancing the
documentation for developers.

Build:
- Add Dockerfile.build for creating a base multi-stage image, Dockerfile
for the main service image, and Dockerfile.test-integration for
integration testing.

Deployment:
- Add docker-compose.yml to define the containerized service setup,
including shared configuration for ports, volumes, and logging.

Documentation:
- Enhance user-facing documentation in CONTRIBUTING.md and README.md to
guide developers on using Docker for local integration testing and
service emulation.

<!-- Generated by sourcery-ai[bot]: end summary -->
@codecakes codecakes self-assigned this Sep 20, 2024
@codecakes
Copy link
Contributor Author

@codecakes Please assign this to me

@JiyaGupta-cs this is closed. Checkout more issues that are open.

codecakes added a commit that referenced this issue Sep 22, 2024
… service (#75)

resolves #73

adds:

- Dockerfile.build: This is the base multi-stage image.
- Dockerfile: Main image to run the service using docker compose.
- Dockerfile.test-integration: For running integration tests.

<!-- Generated by sourcery-ai[bot]: start summary -->

## Summary by Sourcery

Add Docker support for integration testing and running the service in a
containerized environment. Update documentation to guide developers on
setting up and using Docker for local testing and service emulation.
Introduce Dockerfiles for building base, main, and integration test
images, and add a Docker Compose configuration for service deployment.

New Features:
- Introduce Docker support for integration testing and running the
service in a containerized environment using Docker Compose.

Enhancements:
- Update CONTRIBUTING.md and README.md to include instructions for
setting up and running the containerized application, enhancing the
documentation for developers.

Build:
- Add Dockerfile.build for creating a base multi-stage image, Dockerfile
for the main service image, and Dockerfile.test-integration for
integration testing.

Deployment:
- Add docker-compose.yml to define the containerized service setup,
including shared configuration for ports, volumes, and logging.

Documentation:
- Enhance user-facing documentation in CONTRIBUTING.md and README.md to
guide developers on using Docker for local integration testing and
service emulation.

<!-- Generated by sourcery-ai[bot]: end summary -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C4GT Coding C4GT Community enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
2 participants