Root password inside a Docker container

前端 未结 16 697
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 03:24

I\'m using a Docker image which was built using the USER command to use a non-root user called dev. Inside a container, I\'m \"dev\", but I want to edit the

相关标签:
16条回答
  • 2020-12-02 04:01

    I had exactly this problem of not being able to su to root because I was running in the container as an unprivileged user.

    But I didn't want to rebuild a new image as the previous answers suggest.

    Instead I have found that I could access the container as root using 'nsenter', see: https://github.com/jpetazzo/nsenter

    First determine the PID of your container on the host:

    docker inspect --format {{.State.Pid}} <container_name_or_ID>
    

    Then use nsenter to enter the container as root

    nsenter --target <PID> --mount --uts --ipc --net --pid
    
    0 讨论(0)
  • 2020-12-02 04:07

    I am able to get it working with the below command.

    root@gitnew:# docker exec -it --user $(username) $(containername) /bin/bash
    
    0 讨论(0)
  • 2020-12-02 04:07

    I'd suggest a better solution is to give the --add-host NAME:IP argument to docker run when starting the container. That will update the /etc/hosts/ file without any need to become root.

    Otherwise, you can override the the USER setting by giving the -u USER flag to docker run. I would advise against this however, as you shouldn't really be changing things in a running container. Instead, make your changes in a Dockerfile and build a new image.

    0 讨论(0)
  • 2020-12-02 04:08
    docker exec -u 0 -it containername bash
    
    0 讨论(0)
  • 2020-12-02 04:08

    You can SSH in to docker container as root by using

    docker exec -it --user root <container_id> /bin/bash
    

    Then change root password using this

    passwd root
    

    Make sure sudo is installed check by entering

    sudo
    

    if it is not installed install it

    apt-get install sudo
    

    If you want to give sudo permissions for user dev you can add user dev to sudo group

    usermod -aG sudo dev
    

    Now you'll be able to run sudo level commands from your dev user while inside the container or else you can switch to root inside the container by using the password you set earlier.

    To test it login as user dev and list the contents of root directory which is normally only accessible to the root user.

    sudo ls -la /root
    

    Enter password for dev

    If your user is in the proper group and you entered the password correctly, the command that you issued with sudo should run with root privileges.

    0 讨论(0)
  • 2020-12-02 04:08

    In some cases you need to be able to do things like that under a user with sudo (e.g. the application running in the container provides a shell to users). Simply add this into you Dockerfile:

    RUN apt-get update         # If necessary
    RUN apt-get install sudo   # If your base image does not contain sudo.
    RUN useradd -m -N -s /bin/bash -u 1000 -p '$1$miTOHCYy$K.c4Yw.edukWJ7z9rbpTZ0' user && \
        usermod -aG sudo user  # Grant sudo to the user
    USER user
    

    Now under the default image user user you will be able to sudo with the password set on line 3.

    See how to generate password hash for useradd here or here.

    0 讨论(0)
提交回复
热议问题