Skip to content

Commit

Permalink
updates the dockerfile to run kopf as nonroot
Browse files Browse the repository at this point in the history
  • Loading branch information
vjayaramrh committed Feb 18, 2025
1 parent c034605 commit a0a8aec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
35 changes: 25 additions & 10 deletions operators/o2ims-operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@
##########################################################################

FROM python:3.12.9-alpine3.21 AS builder
COPY controllers/ /src/
COPY requirements.txt /
RUN pip install --user -r /requirements.txt --no-cache-dir
############### Target
FROM python:3.12.9-alpine3.21 AS target
COPY --from=builder /root/.local \
/src/ \
/root/.local
ENV PATH=/root/.local/bin:$PATH
CMD ["kopf", "run", "/root/.local/manager.py", "--all-namespaces"]

# Create a non-root user
RUN addgroup --gid 65535 o2ims && \
adduser --uid 65535 --disabled-password --home /home/o2ims -G o2ims o2ims

# Set working directory
WORKDIR /usr/o2ims

# Copy application files
COPY --chown=o2ims:o2ims controllers/ src/
COPY --chown=o2ims:o2ims requirements.txt requirements.txt

# Switch to non-root user
USER o2ims

# Create a virtual environment and install dependencies
RUN python -m venv /home/o2ims/venv && \
/home/o2ims/venv/bin/pip install --no-cache-dir -r requirements.txt

# Set environment variables
ENV PATH="/home/o2ims/venv/bin:${PATH}" \
VIRTUAL_ENV="/home/o2ims/venv"

# Run the application
CMD ["kopf", "run", "/usr/o2ims/src/manager.py", "--all-namespaces"]
4 changes: 2 additions & 2 deletions operators/o2ims-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ nohup kubectl proxy --port 8080 &>/dev/null &
Build a Docker image:

```bash
docker build -t o2ims:latest -f Dockerfile .
docker build -t o2ims-operator:latest -f Dockerfile .
```

Push this image in your cluster, here we are using a `kind` cluster so we will push using the below command:

```bash
kind load docker-image o2ims:latest -n o2ims-mgmt
kind load docker-image o2ims-operator:latest -n o2ims-mgmt
```

`NOTE`: `o2ims-mgmt` is the name of the kind cluster. It is good to mention cluster name if you have multiple clusters.
Expand Down

0 comments on commit a0a8aec

Please sign in to comment.