-
Notifications
You must be signed in to change notification settings - Fork 82
feat(docker): Create non-root clp-user
and move env setup after flattening (fixes #1378, fixes #1379).
#1413
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
base: main
Are you sure you want to change the base?
Conversation
…lp-package Dockerfile (fixes y-scope#1379); Reduce layers using multi-line ENV (fixes y-scope#1378).
WalkthroughMoved CLP packaging runtime metadata and user creation from the build/base stage into the final scratch stage of tools/docker-images/clp-package/Dockerfile, copying only built artifacts into the final stage and defining CLP_HOME, PATH, PYTHONPATH, and USER there; consolidated ENV declarations into a multi-line block. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Builder as Docker Builder
participant Base as Base Stage
participant Final as Final Stage (scratch)
participant Image as Resulting Image
Builder->>Base: Build files & artifacts
Note over Base: No final ENV/USER set here
Builder->>Final: FROM scratch\nCOPY --from=base / /
Final->>Final: create clp-user\ncopy package into CLP_HOME\nset WORKDIR
Final->>Image: ENV CLP_HOME, PATH (bin,sbin), PYTHONPATH\nUSER clp-user
Note over Image: ENV and USER persist in final image metadata
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Possibly linkable linked issues
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fix #1410, we need to "Move USER and ENV directives after image flattening in clp-package Dockerfile"
ENV CLP_HOME="/opt/clp" | ||
ENV PATH="${CLP_HOME}/bin:${PATH}" \ | ||
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" | ||
ENV PATH="${CLP_HOME}/sbin:${PATH}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- We still have to keep three
ENV
directives because the laterENV
settings depend on the former ones. - Technically, we could combine the
PATH
settings into a single line likePATH="${CLP_HOME}/sbin:${CLP_HOME}/bin:${PATH}"
. For readability, we previously agreed in feat(docker): Add container image containing the CLP package, for Docker Compose integration (resolves #1164). #1166 that we want to split them into separate lines. Although the aim is to cut down on number of layers, we still should keep the lines separated given the benefits of readability should outweigh the layer count reduction
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" | ||
ENV PATH="${CLP_HOME}/sbin:${PATH}" | ||
|
||
USER 1000:1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The messages of groups: cannot find name for group ID 1000
and I have no name!
are a little scary. Could we find a way to make them disappear?
clp-user
and move env setup after flattening (fixes #1378, fixes #1379).
clp-user
and move env setup after flattening (fixes #1378, fixes #1379).clp-user
and move env setup after flattening (fixes #1378, fixes #1379).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
tools/docker-images/clp-package/Dockerfile (1)
18-21
: Restore one-variable-per-ENV instructionWe previously agreed (PR #1166) to keep each
ENV
directive focused on a single variable for readability and to avoid order-dependent surprises. Please split the combinedENV
back into separate statements forPATH
,PYTHONPATH
, andUSER
, even if it costs an extra layer. Example:-ENV PATH="${CLP_HOME}/bin:${PATH}" -ENV PATH="${CLP_HOME}/sbin:${PATH}" \ - PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \ - USER="clp-user" +ENV PATH="${CLP_HOME}/bin:${PATH}" +ENV PATH="${CLP_HOME}/sbin:${PATH}" +ENV PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" +ENV USER="clp-user"Based on learnings.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
tools/docker-images/clp-package/Dockerfile
(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).
📚 Learning: 2025-10-13T03:32:19.293Z
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).
Applied to files:
tools/docker-images/clp-package/Dockerfile
🪛 Checkov (3.2.334)
tools/docker-images/clp-package/Dockerfile
[low] 1-25: Ensure that HEALTHCHECK instructions have been added to container images
(CKV_DOCKER_2)
COPY ./build/clp-package ${CLP_HOME} | ||
|
||
ENV PATH="${CLP_HOME}/bin:${PATH}" | ||
ENV PATH="${CLP_HOME}/sbin:${PATH}" \ | ||
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \ | ||
USER="clp-user" | ||
|
||
RUN useradd --uid 1000 --shell /bin/bash --home-dir ${CLP_HOME} ${USER} | ||
USER ${USER} | ||
WORKDIR ${CLP_HOME} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Home directory ownership remains root-owned
COPY ./build/clp-package ${CLP_HOME}
leaves /opt/clp
owned by root, and the subsequent useradd … --home-dir ${CLP_HOME}
does not adjust ownership. When the container starts as clp-user
, the user cannot write to their home (e.g., creating dotfiles or logs). Please ensure the home directory is owned by UID/GID 1000 before switching users—for example, by adding chown -R ${USER}:${USER} ${CLP_HOME}
(or using COPY --chown=1000:1000 …
) prior to the USER
instruction.
🧰 Tools
🪛 Checkov (3.2.334)
[low] 1-25: Ensure that HEALTHCHECK instructions have been added to container images
(CKV_DOCKER_2)
🤖 Prompt for AI Agents
In tools/docker-images/clp-package/Dockerfile around lines 16 to 25, the COPY
leaves ${CLP_HOME} owned by root so clp-user (UID 1000) cannot write to its
home; before the USER instruction change ownership of the directory to UID/GID
1000 (either by using COPY --chown=1000:1000 ./build/clp-package ${CLP_HOME} or
running chown -R ${USER}:${USER} ${CLP_HOME} in a RUN step) so the home
directory and its contents are writable by clp-user.
Description
This PR:
USER
andENV
directives to after the image flattening step in theclp-package
Dockerfile. This ensures that the environment variables and user settings are correctly applied to the final image, rather than being overridden or lost during the flattening process. (fixes Docker image flattening loses USER and ENV directives in clp-package Dockerfile #1379)clp-user
user with a home directory and shell in the final image, ensuring that the user environment is properly set up./opt/clp
for theclp-user
, ensuring that the user starts in the correct location when running a container from this image.PATH
includes both/opt/clp/bin
and/opt/clp/sbin
, allowing access to all installed binaries.ENV
directives into a single multi-lineENV
directive. This helps to optimize the image size and improve build performance. (fixes Optimize ENV declarations in clp-package Dockerfile to reduce layers #1378)Checklist
breaking change.
Validation performed
before
without the changes in the PR
after
with the changes in the PR
Observed that:
[+] Building 1.1s (12/12)
->Building 5.3s (14/14)
.uid=0(root) gid=0(root) groups=0(root)
->uid=1000(clp-user) gid=1000(clp-user) groups=1000(clp-user)
.PATH
was set as expected:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
->PATH=/opt/clp/sbin:/opt/clp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.LS_COLORS
was lost, which is even better.CLP_HOME
is set as expected: CLP_HOME missing ->CLP_HOME=/opt/clp
.HOME
is set as expected:HOME=/root
->HOME=/opt/clp
.PYTHONPATH
is set as expected:PYTHONPATH
missing ->PYTHONPATH=/opt/clp/lib/python3/site-packages
.Summary by CodeRabbit