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

COPY with --chown command should not require user or group IDs to exist #1456

Closed
robertgates55 opened this issue Oct 14, 2020 · 12 comments · Fixed by #1477 or #2106
Closed

COPY with --chown command should not require user or group IDs to exist #1456

robertgates55 opened this issue Oct 14, 2020 · 12 comments · Fixed by #1477 or #2106
Labels
cmd/run cmd/user help wanted Looking for a volunteer! priority/p2 High impact feature/bug. Will get a lot of users happy work-around-available

Comments

@robertgates55
Copy link

Actual behavior
This looks to be similar to #477, but given that was closed a year ago with unable to reproduce I thought I'd start afresh.

Building:

FROM alpine:latest AS helper
COPY --chown=1000:1000 scripts /scripts

Gives:
error building image: error building stage: failed to execute command: getting user group from chown: user: unknown user 1000

Expected behavior
This Dockerfile works with docker - I'd expect that specifying the --chown would create uid/gid 1000 without me creating them as a build step.

To Reproduce
Steps to reproduce the behavior:
Use gcr.io/kaniko-project/executor:latest (v1.2.0) to build the following image:

FROM alpine:latest AS helper
COPY --chown=1000:1000 scripts /scripts

(you'll obviously need a scripts/ dir in the build context)

@NMFR
Copy link

NMFR commented Oct 19, 2020

I stumbled on the same issue on kaniko-project/executor:v1.2.0:

error building image: error building stage: failed to execute command: getting user group from chown: user: unknown user 2000

Here is an example on how to replicate:

Dockerfile

FROM golang:1.14.2-buster@sha256:09b04534495af5148e4cc67c8ac55408307c2d7b9e6ce70f6e05f7f02e427f68 AS tools
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh > /tmp/t

FROM gcr.io/distroless/base-debian10:debug@sha256:b8ec84402b588696f4c77d04cc3115b4d07ba887aaa1b1d3af0f76b2fed5f82d AS production
USER 2000:2000
WORKDIR /opt/app
COPY --from=tools /tmp/t ./t1
COPY --from=tools --chown=2000:2000 /tmp/t ./t2

Docker version 19.03.13

Run:

docker run -v $(pwd):/workspace gcr.io/kaniko-project/executor:v1.2.0 --dockerfile Dockerfile --target production --context . --no-push

My temporary workaround was removing the COPY --chown flag and modifying file permissions for all users before the copy. On my use case this is safe to do but beware of the security implications.

@franco-martin
Copy link

franco-martin commented Oct 19, 2020

stumbled upon a similar issue when running in openshift. Images cant run as uid 0 so we modify all images to run as 1001. We had to replace
USER 1001
for

RUN useradd -u 1001 generic
USER 1001:1001

@tejal29
Copy link
Member

tejal29 commented Oct 29, 2020

Thanks @robertgates55. we do some processing to find if there are secondary groups for a user and probably that is where kaniko is throwing an error.

A simple patch to fix this will be see if userStr and groupStr are already int before calling out to GetUserFromUsername.
If they are, you could just return.

uidStr, gidStr, err := GetUserFromUsername(userStr, groupStr, fallbackToUID)

@tejal29 tejal29 added cmd/run cmd/user help wanted Looking for a volunteer! priority/p2 High impact feature/bug. Will get a lot of users happy work-around-available labels Oct 29, 2020
@abdennour
Copy link

is this fixed and how ?
indeed, i sill got same issue , and this is my dockerfile

FROM gcr.io/distroless/nodejs:14
COPY --from=build --chown=1001:0 /node /app
WORKDIR /app
USER 1001
EXPOSE 3000
CMD ["app.js"]

@abdennour
Copy link

abdennour commented Jan 21, 2021

@franco-martin , if your base image is distroless, RUN useradd will never work :(

INFO[0062] Running: [/bin/sh -c useradd -u 1001 generic]
error building image: error building stage: failed to execute command: starting command: fork/exec /bin/sh: no such file or directory

Guys, can you just it keep same behavor of docker?

@abdennour
Copy link

abdennour commented Jan 21, 2021

this is my workaround with my distroless base-image which does not have user 1001 :

FROM ... as build
.....
FROM  alpine:3.12 as usergroup
RUN addgroup -S appgroup && adduser -S appuser -u 1001 -G appgroup

#gcr.io/distroless/nodejs
FROM  gcr.io/distroless/nodejs:14
COPY --from=usergroup /etc/passwd /etc/passwd
COPY --from=usergroup /etc/group /etc/group
COPY --from=build --chown=1001:0 /node /app

Unfortunately, i had to do that.
I had to not break the compatibility between our ci env & developer env when he uses docker build in this laptop.

If you have somethingl like us (similar to packer ), i advice to prepare base image as following:

FROM alpine:3.12 as usergroup
RUN addgroup -S appgroup && adduser -S appuser -u 1001 -G appgroup

#gcr.io/distroless/nodejs
FROM  gcr.io/distroless/nodejs:14
COPY --from=usergroup /etc/passwd /etc/passwd
COPY --from=usergroup /etc/group /etc/group

Built it and tag it nexus.example.com/distroless/nodejs:14-nonroot
Now use it as base everywhere

@itscaro
Copy link

itscaro commented Feb 15, 2021

I still encountered this issue with kaniko 1.3.0

Base image: https://github.com/bitnami/bitnami-docker-zookeeper/blob/master/3/debian-10/Dockerfile#L28

Overriding Dockerfile

FROM bitnami/zookeeper

ADD --chown=1001:1001 <source url> /opt/bitnami/zookeeper/lib/

Error

 INFO[0034] ADD --chown=1001:1001 <source url> /opt/bitnami/zookeeper/lib/ 
 error building image: error building stage: failed to execute command: getting user group from chown: user: unknown user 1001

@7AC
Copy link

7AC commented Feb 17, 2021

1.5.0 still has this issue despite #1477

@cortex93
Copy link

cortex93 commented Feb 22, 2021

1.5.0 still has this issue despite #1477

Hopefully the test in #1477 did not work (a string will never be an int). Otherwise, the return statement would have returned root uid/gid instead of given values.

In addition, the expected behavior should be
If uid is parseable to an int, the int value must be kept, otherwise the uid must be lookup by name in /etc/passwd
if gid is parseable to an int, the int value must be kept, otherwise the gid must be lookup by name in /etc/group

@Kewynhe
Copy link

Kewynhe commented Mar 16, 2021

Hello,

I'm using 1.5.1 and I have the same issue.

Dockerfile:

Base image: https://github.com/bitnami/bitnami-docker-nginx/blob/1.19.8-debian-10-r6/1.19/debian-10/Dockerfile

# Stage 2 - the final image
FROM bitnami/nginx:1.19.7

####
SOME INSTRUCTIONS
####

COPY --chown=1001:1001 --from=node-builder /app/dist    /usr/share/nginx/html

Output:

INFO[0621] COPY --chown=1001:1001 --from=node-builder /app/dist    /usr/share/nginx/html 
error building image: error building stage: failed to execute command: getting user group from chown: user: unknown user 1001

@EarthlingDavey
Copy link

@abdennour your workaround was needed and worked when I was using a bitnami base image.

It's is not distroless, it's built on minideb. Dockerfile for reference.

Thank you ✌️

@weltonrodrigo
Copy link

Hi.

This impacts quarkus image building: quarkusio/quarkus#25499

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd/run cmd/user help wanted Looking for a volunteer! priority/p2 High impact feature/bug. Will get a lot of users happy work-around-available
Projects
None yet