docker容器裡面的root權限獲取方法

首先你的container得正在運行

可通過sudo docker container ls或者sudo docker ps查看容器的CONTAINER ID

最後執行命令(其中7509371edd48 為上面查到的CONTAINER ID)

sudo docker exec -ti -u root 7509371edd48 bash

補充:解決非root用戶沒有權限運行docker命令的問題

問題描述:

”Got permission denied while trying to connect to the Docker daemon
socket at unix:///var/run/docker.sock: Get
http://%2Fvar%2Frun%2Fdocker.sock/v1.26/images/json: dial unix
/var/run/docker.sock: connect: permission denied“

原因(摘自docker手冊):

Manage Docker as a non-root user

The docker daemon binds to a Unix socket instead of a TCP port. By
default that Unix socket is owned by the user root and other users can
only access it using sudo. The docker daemon always runs as the root
user.

If you don’t want to use sudo when you use the docker command, create
a Unix group called docker and add users to it. When the docker daemon
starts, it makes the ownership of the Unix socket read/writable by the
docker group.

答案顯而易見,要不用root用戶,要不創建一個名為docker的用戶組,並把你需要使用docker的非root用戶添加到該組中,如果還不會搞,繼續往下看。

方法1:

使用sudo獲取管理員權限,運行docker命令,這個方法在通過腳本執行docker命令的時候會有很多局限性

方法2:

docker守護進程啟動的時候,會默認賦予名為docker的用戶組讀寫Unix socket的權限,因此隻要創建docker用戶組,並將當前用戶加入到docker用戶組中,那麼當前用戶就有權限訪問Unix socket瞭,進而也就可以執行docker相關命令

sudo groupadd docker   #添加docker用戶組
sudo gpasswd -a $USER docker   #將登陸用戶加入到docker用戶組中
newgrp docker   #更新用戶組

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found