在Rocky Linux 9上安裝和使用Docker

一臺Rocky Linux 9服務器,并有一個具有sudo 權限的非root用戶

如果命令需要root權限,前面會有sudo

步驟1 - 安裝Docker

Rocky Linux 9官方軟件庫中提供的Docker安裝包可能不是最新版本,需要先更新

sudo dnf check-update

添加官方Docker倉庫

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

雖然Docker沒有專門的Rocky Linux倉庫,但Rocky Linux是基于CentOS的,可以使用相同的倉庫。添加好倉庫后,安裝Docker,它由三個軟件包組成。

sudo dnf install docker-ce docker-ce-cli containerd.io

安裝完成后,啟動Docker守護程序。

sudo systemctl start docker

驗證它是否正在運行

sudo systemctl status docker

輸出結果應該與下面類似,顯示該服務已被激活并運行

Output
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago
     Docs: https://docs.docker.com
 Main PID: 749 (docker)

最后,確保它在每次服務器重啟時啟動。

sudo systemctl enable docker

現在,安裝Docker不僅給你提供了Docker服務(守護進程),還提供了docker 命令行工具,或Docker客戶端

第2步 - 不使用Sudo執行Docker命令(可選)

默認情況下,運行docker 命令需要root權限--也就是說,你必須在命令前加上sudo 。它也可以由docker組中的用戶運行,該組在安裝Docker時自動創建。如果你試圖運行docker ,但沒有在前綴中加上sudo ,或者沒有在docker組中,你會得到這樣的輸出。

Output
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

如果你想避免每次運行docker 命令時都輸入sudo ,請將你的用戶名添加到docker組。

sudo usermod -aG docker $(whoami)

你將需要退出Droplet,然后以同一用戶身份重新登錄,以啟用這一變化。

如果你需要將一個用戶添加到你沒有登錄的docker 組中,請使用明確聲明該用戶名。

sudo usermod -aG docker username

重啟docker

sudo service docker restart

本文的其余部分假設你是以docker用戶組的用戶身份運行docker 命令。如果你選擇不這樣做,請在命令的前面加上sudo 。

驗證 docker

docker

輸出這些表示成功

Output

    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container or image
    kill      Kill a running container
    load      Load an image from a tar archive or STDIN
    login     Log in to a Docker registry
    logout    Log out from a Docker registry
    logs      Fetch the logs of a container
    network   Manage Docker networks
    pause     Pause all processes within a container
    port      List port mappings or a specific mapping for the CONTAINER
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save one or more images to a tar archive
    search    Search the Docker Hub for images
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within a container
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

一些問題

比如 docker 輸出這些信息

permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denie

查看docker組的用戶信息

sudo cat /etc/group | grep docker

添加當前用戶到 docker 組

sudo gpasswd -a ${USER} docker

重啟

sudo service docker restart

如果提示socket文件權限不足, 則給 .sock 文件增加如下權限

也就是上面提示的那些信息

sudo chmod a+rw /var/run/docker.sock

重啟

sudo service docker restart
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容