1. 程式人生 > >docker + ubuntun 安裝show doc

docker + ubuntun 安裝show doc

增加 cert gui pri tom 新版 依賴包 sta class

基本安裝步驟

Ubuntu Docker 安裝

Docker 支持以下的 Ubuntu 版本:

    • Ubuntu Precise 12.04 (LTS)
    • Ubuntu Trusty 14.04 (LTS)
    • Ubuntu Wily 15.10
    • 其他更新的版本……


前提條件

Docker 要求 Ubuntu 系統的內核版本高於 3.10 ,查看本頁面的前提條件來驗證你的 Ubuntu 版本是否支持 Docker。

通過 uname -r 命令查看你當前的內核版本

[email protected]:~$ uname -r

技術分享


使用腳本安裝 Docker

1、獲取最新版本的 Docker 安裝包

[email protected]:~$ wget -qO- https://get.docker.com/ | sh

技術分享

輸入當前用戶的密碼後,就會下載腳本並且安裝Docker及依賴包。

技術分享

技術分享

安裝完成後有個提示:

	If you would like to use Docker as a non-root user, you should now consider
	adding your user to the "docker" group with something like:

	sudo usermod -aG docker runoob
   Remember that you will have to log out and back in for this to take effect!

當要以非root用戶可以直接運行docker時,需要執行 sudo usermod -aG docker runoob 命令,然後重新登陸,否則會有如下報錯

技術分享

2、啟動docker 後臺服務

[email protected]:~$ sudo service docker start

技術分享

3、測試運行hello-world

[email protected]:~$ docker run hello-world

詳情測試步驟

安裝Docker只需3步,下載Docker, 安裝Docker,檢查Docker是否成功。

Docker目前支持主流的3種操作系統的Linux, Mac, Windows的環境,本文使用的Linux系統環境為:Linux Ubuntu 14.04.4 LTS 64bit。在Ubuntu中下載和安裝Docker可以直接用apt-get搞定。

由於Docker在1.7.1以後的版本指定了自己的源,所以我們需要先在APT中配置Docker的源。

更新APT的源,安裝https和ca證書的庫,默認這2個庫都已經裝了。


~ sudo apt-get update
~ sudo apt-get install apt-transport-https ca-certificates

添加秘鑰GPG到APT配置中。


~ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

增加Docker的源到/etc/apt/souces.list文件中,我的版本是14.04對應ubuntu-trusty。


~ sudo vi /etc/apt/sources.list

# 增加到最後一行
deb https://apt.dockerproject.org/repo ubuntu-trusty main

接下來,就可以用可以用apt-get直接安裝Docker了。


~ sudo apt-get update
~ sudo apt-get install docker-engine

安裝完成,默認會啟動Docker。


# 檢查docker服務
~ service docker status
docker start/running, process 10013

# 檢查docker進行
~ ps -aux|grep docker
root     10013  0.0  1.0 424948 40584 ?        Ssl  22:29   0:00 /usr/bin/dockerd --raw-logs
root     10022  0.0  0.2 199680 10280 ?        Ssl  22:29   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shimdocker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc

# 檢查docker版本
~ sudo docker version
Client:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:22:43 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:22:43 2016
 OS/Arch:      linux/amd64

檢查Docker是否成功,運行hello-world。如果出現下面的信息,表示Docker引擎安裝成功。


~ sudo docker run hello-world
Unable to find image ‘hello-world:latest‘ locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete 
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

註意:我們在執行上面的命令的時候,經常會遇到一個錯誤。Cannot connect to the Docker daemon. Is the docker daemon running on this host?

比如,直接輸入 docker run hello-world 命令。


~ docker run hello-world
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See ‘docker run --help‘.

這是由於權限的問題,docker默認和root權限綁定,如果不加sudo時則沒有權限。

問題解決步驟

默認安裝完 docker 後,每次執行 docker 都需要運行 sudo 命令,非常浪費時間影響效率。如果不跟 sudo,直接執行 docker images 命令會有如下問題:

FATA[0000] Get http:///var/run/docker.sock/v1.18/images/json: dial unix /var/run/docker.sock: permission denied. Are you trying to connect to a TLS-enabled daemon without TLS?

於是考慮如何免 sudo 使用 docker,經過查找資料,發現只要把用戶加入 docker 用戶組即可,具體用法如下。

2 免 sudo 使用 docker

  • 如果還沒有 docker group 就添加一個:

    1. sudo groupadd docker
  • 將用戶加入該 group 內。然後退出並重新登錄就生效啦。

    1. sudo gpasswd -a ${USER} docker
  • 重啟 docker 服務

    1. sudo service docker restart
  • 切換當前會話到新 group 或者重啟 X 會話

    1. newgrp - docker
    2. OR
    3. pkill X

註意,最後一步是必須的,否則因為 groups 命令獲取到的是緩存的組信息,剛添加的組信息未能生效,所以 docker images 執行時同樣有錯。

3 原因分析

  • 因為 /var/run/docker.sock 所屬 docker 組具有 setuid 權限

    1. $ sudo ls -l /var/run/docker.sock
    2. srw-rw---- 1 root docker 0 May 1 21:35 /var/run/docker.sock

docker + ubuntun 安裝show doc