Root password inside a Docker container

前端 未结 16 695
没有蜡笔的小新
没有蜡笔的小新 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 03:50

    Get a shell of your running container and change the root pass.

    docker exec -it <MyContainer> bash
    
    root@MyContainer:/# passwd
    Enter new UNIX password: 
    Retype new UNIX password: 
    
    0 讨论(0)
  • 2020-12-02 03:51

    Eventually, I decided to rebuild my Docker images, so that I change the root password by something I will know.

    RUN echo 'root:Docker!' | chpasswd
    

    or

    RUN echo 'Docker!' | passwd --stdin root 
    
    0 讨论(0)
  • 2020-12-02 03:51

    To create/change a root password in a running container

    docker exec -itu root {containerName} passwd
    
    0 讨论(0)
  • 2020-12-02 03:52

    The password is 'ubuntu' for the 'ubuntu' user (at least in docker for ubuntu :14.04.03).

    NB: 'ubuntu' is created after the startup of the container so, if you just do this:

     docker run -i -t --entrypoint /bin/bash  ubuntu     
    

    You'll get the root prompt directly. From there you can force the password change of root, commit the container and optionally tag it (with -f) to ubuntu:latest like this:

    root@ec384466fbbb:~# passwd
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    root@ec384466fbbb:~# exit
    
    % docker commit ec3844
    5d3c03e7d6d861ce519fe33b184cd477b8ad03247ffe19b2a57d3f0992d71bca
    
    docker tag -f 5d3c ubuntu:latest
    

    You must rebuild your eventual dependencies on ubuntu:latest.

    0 讨论(0)
  • 2020-12-02 03:54

    There are a couple of ways to do it.

    1. To run the Docker overriding the USER setting

       docker exec -u 0 -it containerName bash
      

    or

    docker exec -u root -it --workdir / <containerName> bash
    
    1. Make necessary file permissions, etc., during the image build in the Docker file

    2. If all the packages are available in your Linux image, chpasswdin the dockerfile before the USER utility.

    For complete reference: http://muralitechblog.com/root-password-of-a-docker-container/

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

    By default docker containers run as the root user.

    If you are still using the container you can use exit command to get back to root (default user) user instead of running the container again.

    Example -

    [dev@6c4c86bccf93 ~]$ ls
    [dev@6c4c86bccf93 ~]$ other-commands..
    [dev@6c4c86bccf93 ~]$ exit
    [root@6c4c86bccf93 /]# ls
    
    0 讨论(0)
提交回复
热议问题