Ubuntu16.04安裝配置Docker
安裝相關包
$ 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
$ sudo apt-get update $ sudo apt-get install docker-ce # 安裝最新版的docker
如果需要安裝指定版本的,使用以下命令:
$ apt-cache policy docker-ce # 檢視可供安裝的所有docker版本 $ sudo apt-get install docker-ce=18.03.0~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 rootroot180 11月 21 16:36 docker -rw-r--r--1 rootroot5 11月 21 16:35 docker.pid srw-rw----1 rootdocker0 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/
到這裡就安裝完成了~
參考資料
ofollow,noindex">https://docs.docker.com/get-started/#test-docker-version
https://www.jianshu.com/p/a12558da034e
https://www.jianshu.com/p/95e397570896About

瞭解更多有趣的操作請關注我的微信公眾號:DealiAxy
每一篇文章都在我的部落格有收錄: blog.deali.cn