Skip to content

Add dockerfile#590

Merged
joamatab merged 3 commits into
gdsfactory:mainfrom
owls-on-wires:main
May 28, 2025
Merged

Add dockerfile#590
joamatab merged 3 commits into
gdsfactory:mainfrom
owls-on-wires:main

Conversation

@owls-on-wires

@owls-on-wires owls-on-wires commented May 27, 2025

Copy link
Copy Markdown
Contributor

Dockerfile (x86, no ARM yet) includes Meep. Could be used as the starting point for a REST API wrapper over the gplugins, allowing it to be deployed anywhere.

Summary by Sourcery

Add Dockerfile and .dockerignore to enable containerized deployment of the gplugins REST API server with Meep integration on Linux/AMD64.

New Features:

  • Introduce Dockerfile to install system dependencies, Miniconda, a conda environment with pymeep, and a UV virtual environment, then run the gplugins server via Uvicorn

Build:

  • Add .dockerignore to exclude unnecessary files from Docker builds

@sourcery-ai

sourcery-ai Bot commented May 27, 2025

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduce a Dockerfile that sets up system dependencies, a conda-based pymeep environment, a uv virtualenv for the Python application, installs all required packages, and specifies uvicorn as the container's startup command, along with a placeholder .dockerignore.

Sequence diagram for Container Startup Process

sequenceDiagram
    participant DE as Docker Engine
    participant C as Container
    participant Proc as Startup Process
    participant Uvicorn as Uvicorn Server
    participant App as "gplugins.server:app"

    DE->>+C: Start Container
    C->>+Proc: Execute CMD ["uvicorn", "gplugins.server:app", "--host", "0.0.0.0", "--port", "8000"]
    Proc->>+Uvicorn: Launch Uvicorn
    Uvicorn->>+App: Load application 'gplugins.server:app'
    App-->>-Uvicorn: Application loaded
    Uvicorn->>Uvicorn: Start HTTP server
    Uvicorn->>Uvicorn: Listen on 0.0.0.0:8000
    Proc-->>-C: Process running
    C-->>-DE: Container running
Loading

Flow diagram for Docker Image Build Process

graph TD
    A[Start: FROM astral/uv:python3.11-bookworm-slim] --> B(Install system dependencies via apt-get)
    B --> C(Install Miniconda to /opt/conda)
    C --> D(Add conda to PATH)
    D --> E(Create conda environment 'pymeep' with Python 3.11)
    E --> F(Add 'pymeep' env to PATH for subsequent RUN commands)
    F --> G(Install pymeep & nlopt into 'pymeep' via conda)
    G --> H(Create uv virtual environment '/opt/venv')
    H --> I(Add '/opt/venv/bin' to PATH)
    I --> J(Set WORKDIR to /app)
    J --> K(COPY application code to /app)
    K --> L(Install Python application dependencies via 'uv pip install')
    L --> M(Install uvicorn via 'uv pip install')
    M --> N(Set CMD to run Uvicorn with gplugins.server:app)
    N --> O[End: Docker Image Ready]
Loading

File-Level Changes

Change Details Files
Add Dockerfile configuring system deps, conda env, app setup and startup
  • Select Python 3.11 slim base image with amd64 platform
  • Install system packages (curl, gcc, klayout, etc.) via apt-get
  • Download and install Miniconda and update PATH
  • Create and activate conda 'pymeep' environment, installing pymeep and nlopt
  • Create uv virtual environment, install editable application extras and uvicorn
  • Set WORKDIR, copy source code, and define default CMD as uvicorn server
Dockerfile
Add .dockerignore file to exclude unnecessary files from Docker build context
  • Initialize .dockerignore (currently empty)
.dockerignore

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @owls-on-wires - I've reviewed your changes - here's some feedback:

Blocking issues:

  • By not specifying a USER, a program in the container may run as 'root'. This is a security hazard. If an attacker can control a process running as root, they may have control over the container. Ensure that the last USER in a Dockerfile is a USER other than 'root'. (link)

General comments:

  • Pin the base image and Miniconda installer to specific versions to ensure reproducible Docker builds.
  • Combine related RUN steps or use a multi-stage build to reduce the number of layers and shrink the final image size.
  • Populate .dockerignore with common patterns (e.g. .git, pycache, venv folders) to avoid sending unneeded files in the build context.
Here's what I looked at during the review
  • 🟡 General issues: 6 issues found
  • 🔴 Security: 1 blocking issue
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread Dockerfile
FROM --platform=linux/amd64 ghcr.io/astral-sh/uv:python3.11-bookworm-slim

# Install system dependencies including KLayout
RUN apt-get update && apt-get install -y \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Set non-interactive frontend and use --no-install-recommends

This ensures the build runs without prompts and reduces image size by excluding unnecessary packages.

Comment thread Dockerfile
Comment on lines +29 to +35
# Create conda environment with Python 3.11 (matching the base image)
RUN conda create -n pymeep python=3.11 -y
RUN echo "source activate pymeep" >> ~/.bashrc
ENV PATH="/opt/conda/envs/pymeep/bin:${PATH}"

# Install conda packages in the pymeep environment
RUN conda install -n pymeep -c conda-forge pymeep=*=mpi_mpich_* nlopt -y

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Merge conda create and install steps

This will streamline the Docker image and optimize build performance.

Suggested change
# Create conda environment with Python 3.11 (matching the base image)
RUN conda create -n pymeep python=3.11 -y
RUN echo "source activate pymeep" >> ~/.bashrc
ENV PATH="/opt/conda/envs/pymeep/bin:${PATH}"
# Install conda packages in the pymeep environment
RUN conda install -n pymeep -c conda-forge pymeep=*=mpi_mpich_* nlopt -y
# Create conda environment with Python 3.11 (matching the base image) and install packages
RUN conda create -n pymeep python=3.11 -c conda-forge pymeep=*=mpi_mpich_* nlopt -y
RUN echo "source activate pymeep" >> ~/.bashrc
ENV PATH="/opt/conda/envs/pymeep/bin:${PATH}"

Comment thread Dockerfile
Comment on lines +34 to +37
# Install conda packages in the pymeep environment
RUN conda install -n pymeep -c conda-forge pymeep=*=mpi_mpich_* nlopt -y

# Create and activate uv virtual environment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Clean up conda caches to shrink the image

Run 'conda clean --all -y' after installation to remove caches and reduce image size.

Suggested change
# Install conda packages in the pymeep environment
RUN conda install -n pymeep -c conda-forge pymeep=*=mpi_mpich_* nlopt -y
# Create and activate uv virtual environment
# Install conda packages in the pymeep environment
RUN conda install -n pymeep -c conda-forge pymeep=*=mpi_mpich_* nlopt -y && \
conda clean --all -y
# Create and activate uv virtual environment

Comment thread Dockerfile

# Create conda environment with Python 3.11 (matching the base image)
RUN conda create -n pymeep python=3.11 -y
RUN echo "source activate pymeep" >> ~/.bashrc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: Sourcing ~/.bashrc won't affect non-interactive RUNs

Docker RUN steps don't source ~/.bashrc. Use 'conda run -n pymeep ' or set SHELL to a login shell if environment activation is needed during build.

Comment thread Dockerfile
COPY . .

# Install the rest of your requirements
RUN uv pip install -e ".[dev,docs,femwell,gmsh,meow,sax,tidy3d,klayout,vlsir]"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Combine pip install commands into one layer

Merging both installs into one line will reduce image layers and improve build speed.

Comment thread Dockerfile Outdated

# Install the rest of your requirements
RUN uv pip install -e ".[dev,docs,femwell,gmsh,meow,sax,tidy3d,klayout,vlsir]"
RUN uv pip install uvicorn

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Pin pip dependencies to fixed versions

Using fixed versions or a requirements file helps ensure consistent builds and avoids issues from upstream changes.

Suggested implementation:

# Copy requirements with pinned versions
COPY requirements.txt .

# Install the rest of your requirements with pinned versions
RUN uv pip install -r requirements.txt

# Set the default command

  1. You need to generate a requirements.txt file with all dependencies and their pinned versions (including extras and uvicorn). You can do this locally with:
    pip install -e ".[dev,docs,femwell,gmsh,meow,sax,tidy3d,klayout,vlsir]"
    pip freeze > requirements.txt
    pip show uvicorn | grep Version  # Note the version and ensure it's in requirements.txt
    
  2. Add requirements.txt to your repository so it can be copied in the Docker build.
  3. Remove the now-unnecessary uv pip install uvicorn line, as uvicorn should be included and pinned in requirements.txt.

Comment thread Dockerfile Outdated
RUN uv pip install uvicorn

# Set the default command
CMD ["uvicorn", "gplugins.server:app", "--host", "0.0.0.0", "--port", "8000"] No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security (missing-user): By not specifying a USER, a program in the container may run as 'root'. This is a security hazard. If an attacker can control a process running as root, they may have control over the container. Ensure that the last USER in a Dockerfile is a USER other than 'root'.

Suggested change
CMD ["uvicorn", "gplugins.server:app", "--host", "0.0.0.0", "--port", "8000"]
USER non-root
CMD ["uvicorn", "gplugins.server:app", "--host", "0.0.0.0", "--port", "8000"]

Source: opengrep

@joamatab joamatab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome! thank you Chandler!

@joamatab joamatab added the maintenance small patch label May 28, 2025
@joamatab joamatab merged commit 51c308a into gdsfactory:main May 28, 2025
12 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance small patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants