How to add users to Docker container?

后端 未结 8 1243
无人及你
无人及你 2020-11-28 17:07

I have a docker container with some processes (uwsgi and celery) running inside. I want to create a celery user and a uwsgi user for these processes as well as a worker grou

8条回答
  •  天涯浪人
    2020-11-28 17:53

    Ubuntu

    Try the following lines in Dockerfile:

    RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1001 ubuntu
    USER ubuntu
    WORKDIR /home/ubuntu
    

    useradd options (see: man useradd):

    • -r, --system Create a system account. see: Implications creating system accounts
    • -m, --create-home Create the user's home directory.
    • -d, --home-dir HOME_DIR Home directory of the new account.
    • -s, --shell SHELL Login shell of the new account.
    • -g, --gid GROUP Name or ID of the primary group.
    • -G, --groups GROUPS List of supplementary groups.
    • -u, --uid UID Specify user ID. see: Understanding how uid and gid work in Docker containers
    • -p, --password PASSWORD Encrypted password of the new account (e.g. ubuntu).

    Setting default user's password

    To set the user password, add -p "$(openssl passwd -1 ubuntu)" to useradd command.

    Alternatively add the following lines to your Dockerfile:

    SHELL ["/bin/bash", "-o", "pipefail", "-c"]
    RUN echo 'ubuntu:ubuntu' | chpasswd
    

    The first shell instruction is to make sure that -o pipefail option is enabled before RUN with a pipe in it. Read more: Hadolint: Linting your Dockerfile.

提交回复
热议问题