在Rocky Linux 9上安裝和使用Docker

一臺(tái)Rocky Linux 9服務(wù)器,并有一個(gè)具有sudo 權(quán)限的非root用戶

如果命令需要root權(quán)限,前面會(huì)有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,它由三個(gè)軟件包組成。

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

安裝完成后,啟動(dòng)Docker守護(hù)程序。

sudo systemctl start docker

驗(yàn)證它是否正在運(yùn)行

sudo systemctl status docker

輸出結(jié)果應(yīng)該與下面類似,顯示該服務(wù)已被激活并運(yùn)行

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)

最后,確保它在每次服務(wù)器重啟時(shí)啟動(dòng)。

sudo systemctl enable docker

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

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

默認(rèn)情況下,運(yùn)行docker 命令需要root權(quán)限--也就是說,你必須在命令前加上sudo 。它也可以由docker組中的用戶運(yùn)行,該組在安裝Docker時(shí)自動(dòng)創(chuàng)建。如果你試圖運(yùn)行docker ,但沒有在前綴中加上sudo ,或者沒有在docker組中,你會(huì)得到這樣的輸出。

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

如果你想避免每次運(yùn)行docker 命令時(shí)都輸入sudo ,請(qǐng)將你的用戶名添加到docker組。

sudo usermod -aG docker $(whoami)

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

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

sudo usermod -aG docker username

重啟docker

sudo service docker restart

本文的其余部分假設(shè)你是以docker用戶組的用戶身份運(yùn)行docker 命令。如果你選擇不這樣做,請(qǐng)?jiān)诿畹那懊婕由蟬udo 。

驗(yàn)證 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

添加當(dāng)前用戶到 docker 組

sudo gpasswd -a ${USER} docker

重啟

sudo service docker restart

如果提示socket文件權(quán)限不足, 則給 .sock 文件增加如下權(quán)限

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

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

重啟

sudo service docker restart
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 容器現(xiàn)在風(fēng)靡于 IT 界 —— 這很好理解。容器是輕量級(jí)的,包含應(yīng)用運(yùn)行所需所有東西(代碼、庫、運(yùn)行時(shí)環(huán)境、系統(tǒng)設(shè)...
    3A網(wǎng)絡(luò)軒軒閱讀 367評(píng)論 0 0
  • Docker簡(jiǎn)介就不說了,也說不太清楚,摘一段wiki的話吧: Docker is a computer prog...
    井底蛙蛙呱呱呱閱讀 1,392評(píng)論 0 1
  • Docker安裝與使用 一、docker安裝。 1、安裝要求: 1)docker要求服務(wù)CentOS6以上,ker...
    卬之別錄閱讀 2,135評(píng)論 0 1
  • 初學(xué)記錄,如有問題,請(qǐng)留言 docker 安裝與使用 個(gè)人理解: 我們把所有的環(huán)境、依賴、所需要的數(shù)據(jù)文件等都集成...
    默寫年華Antifragile閱讀 1,622評(píng)論 0 0
  • 帶你進(jìn)入Docker的世界 Docker優(yōu)勢(shì) 操作系統(tǒng): debain/unbuntu /FreeBSD/Cent...
    海若Hero閱讀 409評(píng)論 0 0

友情鏈接更多精彩內(nèi)容