安裝相關包
# 先更新一下軟件源庫信息
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
添加軟件倉庫
官方倉庫
# 添加 Docker 官方的 GPG 密鑰(為了確認所下載軟件包的合法性,需要添加軟件源的 GPG 密鑰)
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 設置穩定版本的apt倉庫地址
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
阿里雲倉庫
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
安裝docker
安裝最新版的docker
$ sudo apt-get update
$ sudo apt-get install docker-ce # 安裝最新版的docker
如果需要安裝指定版本的,使用以下命令
$ apt-cache policy docker-ce # 查看可供安裝的所有docker版本
$ sudo apt-get install docker-ce=17.03.1~ce-0~ubuntu # 安裝指定版本的docker
``
# 檢查docker是否安裝成功
$ docker --version # 查看安裝的docker版本
添加訪問權限
這個時候運行docker的話,如果不是root用戶會報錯:
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
看一下權限
$ cd /var/run
$ ll | grep docker
# 輸出如下
drwx------ 8 root root 180 11月 21 16:36 docker
-rw-r--r-- 1 root root 5 11月 21 16:35 docker.pid
srw-rw---- 1 root docker 0 11月 21 16:35 docker.sock
可以看到 docker.sock 的所有者是 docker 這個組。所以我們要把當前用戶添加到這個組里。
$ sudo gpasswd -a ${USER} docker
重啟docker
sudo service docker restart
切換當前會話到新 group 或者重啟 X 會話
newgrp - docker
*** 注意:最后一步是必須的,否則因為 groups 命令獲取到的是緩存的組信息,剛添加的組信息未能生效,所以 docker images 執行時同樣有錯。
運行docker測試
這個時候就可以運行helloworld測試啦~
$ docker run hello-world
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/
到這里就安裝完成了~
參考資料
https://docs.docker.com/get-started/#test-docker-version
https://www.jianshu.com/p/a12558da034e
https://www.jianshu.com/p/95e397570896