skip to Main Content

I want to change an alpine-based container user’s UID and GID.

But there’s no usermod and groupmod. Are there equivalents?

(This is for a running container, not an image.)

2

Answers


  1. Chosen as BEST ANSWER

    This is my workaround (must be put in an entrypoint script):

    $ id bob
    uid=1000(bob) gid=1000(bob) groups=1000(bob),1000(bob)
    
    $ apk --no-cache add shadow                              # <-----
    $ groupmod --gid 2000 bob
    $ usermod --uid 2000 --gid 2000 bob
    $ apk del shadow
    
    $ id bob
    uid=2000(bob) gid=2000(bob) groups=2000(bob),2000(bob)
    

    Please let me know if there's a way to do this without installing anything.


  2. Assuming the group and user exist before the container is created you can simply run the container with the proper user:group uid:gid.

    The docker run command can be passed a user and group (or uid / gid).

    docker run --user 2000:2000 acme

    Or, via compose, the user: attribute can be used.

    compose.yml

    services:
      acme:
        image: my-alpine:latest
        user: 2000:2000
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search