1、卸載舊版本,Docker 的舊版本被稱為 docker,docker.io 或 docker-engine 。如果已安裝,請(qǐng)卸載它們:
? ? $ sudo apt-get remove docker docker-engine docker.io containerd runc
2、更新 apt 包索引
? ? $ sudo apt-get update
3、安裝 apt 依賴包,用于通過HTTPS來獲取倉庫:
? ? $ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
4、添加 Docker 的官方 GPG 密鑰:
? ? $ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
5、9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 通過搜索指紋的后8個(gè)字符,驗(yàn)證您現(xiàn)在是否擁有帶有指紋的密鑰
? ? $ sudo apt-key fingerprint 0EBFCD88
? ? 會(huì)看到以下輸出:
? ? pub? rsa4096 2017-02-22 [SCEA]
? ? ? 9DC8 5822 9FC7 DD38 854A? E2D8 8D81 803C 0EBF CD88
? ? uid? ? ? ? ? [ unknown] Docker Release (CE deb) <docker@docker.com>
? ? sub? rsa4096 2017-02-22 [S]
6、使用以下指令設(shè)置穩(wěn)定版?zhèn)}庫
? ? $ sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ $(lsb_release -cs) stable"
7、安裝 Docker Engine-Community,更新 apt 包索引
? ? $ sudo apt-get update
8、安裝最新版本的 Docker Engine-Community 和 containerd ,或者轉(zhuǎn)到下一步安裝特定版本:
? ? $ sudo apt-get install docker-ce docker-ce-cli containerd.io
? ? 如果要安裝特定版本的 Docker Engine-Community,請(qǐng)?jiān)趥}庫中列出可用版本,然后選擇一種安裝。
? ? ? ? 1、列出您的倉庫中可用的版本:
? ? ? ? ? ? $ apt-cache madison docker-ce
? ? ? ? ? ? ? ? docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu? xenial/stable amd64 Packages
? ? ? ? ? ? ? ? docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu? xenial/stable amd64 Packages
? ? ? ? ? ? ? ? docker-ce | 18.06.1~ce~3-0~ubuntu? ? ? | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu? xenial/stable amd64 Packages
? ? ? ? ? ? ? ? docker-ce | 18.06.0~ce~3-0~ubuntu? ? ? | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu? xenial/stable amd64 Packages
? ? ? ? ? ? ? ? ...
? ? ? ? 2、使用第二列中的版本字符串安裝特定版本,例如 5:18.09.1~3-0~ubuntu-xenial。
? ? ? ? ? ? $ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
9、測(cè)試 Docker 是否安裝成功,輸入以下指令,打印出以下信息則安裝成功:
? ? $ sudo docker run hello-world
? ? ? ? Unable to find image 'hello-world:latest' locally
? ? ? ? latest: Pulling from library/hello-world
? ? ? ? 1b930d010525: Pull complete? Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
? ? ? ? Status: Downloaded newer image for hello-world:latest
? ? ? ? Hello from Docker!
? ? ? ? This message shows that your installation appears to be working correctly.
? ? ? ? To generate this message, Docker took the following steps:
? ? ? ? 1. The Docker client contacted the Docker daemon.
? ? ? ? 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
? ? ? ? ? ? (amd64)
? ? ? ? 3. The Docker daemon created a new container from that image which runs the
? ? ? ? ? ? executable that produces the output you are currently reading.
? ? ? ? 4. The Docker daemon streamed that output to the Docker client, which sent it
? ? ? ? ? ? to your terminal.
? ? ? ? To try something more ambitious, you can run an Ubuntu container with:
? ? ? ? $ docker run -it ubuntu bash
? ? ? ? Share images, automate workflows, and more with a free Docker ID:
? ? ? ? https://hub.docker.com/
? ? ? ? For more examples and ideas, visit:
? ? ? ? https://docs.docker.com/get-started/
10、問題描述
? ? 安裝完docker后,執(zhí)行docker相關(guān)命令,出現(xiàn)
? ? ”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“
? ? (1)、原因
? ? ? ? 摘自docker mannual上的一段話
? ? ? ? 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.
? ? ? ? 大概的意思就是:docker進(jìn)程使用Unix Socket而不是TCP端口。而默認(rèn)情況下,Unix socket屬于root用戶,需要root權(quán)限才能訪問。
? ? (2)、解決方法1
? ? ? ? 使用sudo獲取管理員權(quán)限,運(yùn)行docker命令
? ? (3)、解決方法2
? ? ? ? docker守護(hù)進(jìn)程啟動(dòng)的時(shí)候,會(huì)默認(rèn)賦予名字為docker的用戶組讀寫Unix socket的權(quán)限,因此只要?jiǎng)?chuàng)建docker用戶組,并將當(dāng)前用戶加入到docker用戶組中,那么當(dāng)前用戶就有權(quán)限訪問Unix socket了,進(jìn)而也就可以執(zhí)行docker相關(guān)命
? ? ? ? sudo groupadd docker? ? ? ? ? ? #添加docker用戶組
? ? ? ? sudo gpasswd -a $USER docker? ? #將登陸用戶加入到docker用戶組中
? ? ? ? newgrp docker? ? ? ? ? ? ? ? ? ? #更新用戶組
? ? ? ? docker ps? ? ? ? ? ? ? ? ? ? ? ? #測(cè)試docker命令是否可以使用sudo正常使用