1. 程式人生 > >Docker 從入門到放棄(一)安裝

Docker 從入門到放棄(一)安裝

前言

Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的Linux機器上,也可以實現虛擬化,容器是完全使用沙箱機制,相互之間不會有任何介面。

Docker採用 C/S架構 Docker daemon 作為服務端接受來自客戶的請求,並處理這些請求(建立、執行、分發容器)。 客戶端和服務端既可以執行在一個機器上,也可通過 socket 或者RESTful API 來進行通訊。

Docker daemon 一般在宿主主機後臺執行,等待接收來自客戶端的訊息。 Docker 客戶端則為使用者提供一系列可執行命令,使用者用這些命令實現跟 Docker daemon 互動。


安裝

前提條件 Docker 要求 Ubuntu 系統的核心版本高於 3.10 ,檢視本頁面的前提條件來驗證你的 Ubuntu 版本是否支援 Docker。

通過 uname -r 命令檢視你當前的核心版本

# uname -r
4.4.0-93-generic

使用指令碼安裝 Docker

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

複製程式碼
# wget -qO- https://get.docker.com/ | sh
# Executing docker install script, commit: 11aa13e
+ sh
-c apt-get update -qq >/dev/null + sh -c apt-get install -y -qq apt-transport-https ca-certificates curl software-properties-common >/dev/null + sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null + sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty edge
" > /etc/apt/sources.list.d/docker.list + [ ubuntu = debian ] + sh -c apt-get update -qq >/dev/null + sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null + sh -c docker version Client: Version: 17.11.0-ce API version: 1.34 Go version: go1.8.3 Git commit: 1caf76c Built: Mon Nov 20 18:36:37 2017 OS/Arch: linux/amd64 Server: Version: 17.11.0-ce API version: 1.34 (minimum version 1.12) Go version: go1.8.3 Git commit: 1caf76c Built: Mon Nov 20 18:35:10 2017 OS/Arch: linux/amd64 Experimental: false 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 your-user Remember that you will have to log out and back in for this to take effect! WARNING: Adding a user to the "docker" group will grant the ability to run containers which can be used to obtain root privileges on the docker host. Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface for more information.
複製程式碼

安裝完成後有個提示:

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

2、啟動docker 後臺服務

sudo service docker start
start: Job is already running: docker

3、用Hello World校驗Docker的安裝

用Docker執行Hello World映象,命令如下:

複製程式碼
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
複製程式碼

可見,Docker註冊伺服器從Docker Hub獲取到最新的Hello World映象,下載到了本地。可以再次執行Hello World映象。

複製程式碼
# 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
複製程式碼

 二、使用

1、Docker Hello World

Docker 允許你在容器內執行應用程式, 使用 docker run 命令來在容器內執行一個應用程式

複製程式碼
[email protected]:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete 
759aa75f3cee: Pull complete 
3fa871dc8a2b: Pull complete 
224c42ae46e7: Pull complete 
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
Hello world
複製程式碼

Docker首先從本地主機上查詢映象是否存在,如果不存在,Docker 就會從映象倉庫 Docker Hub 下載公共映象。這裡發現映象ubuntu:15.10 不存在,正在從映象倉庫下載

引數解析:

  • docker: Docker 的二進位制執行檔案。

  • run:與前面的 docker 組合來執行一個容器。

  • ubuntu:15.10:指定要執行的映象,Docker首先從本地主機上查詢映象是否存在,如果不存在,Docker 就會從映象倉庫 Docker Hub 下載公共映象。

  • /bin/echo "Hello world": 在啟動的容器裡執行的命令

[email protected]:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

以上命令完整的意思可以解釋為:Docker 以 ubuntu15.10 映象建立一個新容器,然後在容器裡執行 bin/echo "Hello world",然後輸出結果。

2、執行互動式的容器

通過docker的兩個引數 -i -t,讓docker執行的容器實現"對話"的能力

[email protected]:~# docker run -i -t ubuntu:15.10 /bin/bash
[email protected]:/#

引數解析:

  • -t:在新容器內指定一個偽終端或終端。

  • -i:允許你對容器內的標準輸入 (STDIN) 進行互動。

此時我們已進入一個 ubuntu15.10系統的容器,嘗試在容器中執行命令 cat /proc/version 和 ls 分別檢視當前系統的版本資訊和當前目錄下的檔案列表

複製程式碼
[email protected]:/# cat /proc/version
Linux version 4.4.0-93-generic ([email protected]28) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) ) #116~14.04.1-Ubuntu SMP Mon Aug 14 16:07:05 UTC 2017
[email protected]:/# cat /etc/issue
Ubuntu 15.10 \n \l
[email protected]:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
複製程式碼

可以通過執行exit命令或者使用CTRL+D來退出容器,檢視當前真實的伺服器系統版本

[email protected]:/# exit
exit
[email protected]:~# cat /etc/issue
Ubuntu 14.04.5 LTS \n \l

3、啟動容器(後臺模式)

使用以下命令建立一個以程序方式執行的容器

[email protected]:~# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
6ddd469230b5402251c4fd3214b63f6a6af7d9e2711f8944a9d74b36346bbd5a

在輸出中,我們沒有看到期望的"hello world",而是一串長字元 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63 這個長字串叫做容器ID,對每個容器來說都是唯一的,我們可以通過容器ID來檢視對應的容器發生了什麼。

首先,我們需要確認容器有在執行,可以通過 docker ps 來檢視

[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
6ddd469230b5        ubuntu:15.10        "/bin/sh -c 'while t…"   About a minute ago   Up About a minute                       romantic_visvesvaraya

CONTAINER ID:容器ID

NAMES:自動分配的容器名稱

在容器內使用docker logs命令,檢視容器內的標準輸出

(1)使用ID

[email protected]:~# docker logs 6ddd469230b5
hello world
hello world
hello world
...

(2)使用NAMES

[email protected]:~# docker logs romantic_visvesvaraya
hello world
hello world
...

發現結果是一樣的,輸出了同樣的結果

4、停止容器

使用 docker stop 命令來停止容器,讓然啦,要指定停止的容器物件啦,不然會這樣子

複製程式碼
[email protected]:~# docker stop 
"docker stop" requires at least 1 argument.
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags]

Stop one or more running containers
複製程式碼

指定一個容器

[email protected]:~# docker stop 6ddd469230b5
6ddd469230b5

通過 docker ps 檢視,容器已經停止工作

[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

也可以用下面的NAMES命令來停止

docker stop romantic_visvesvaraya

 

前言

Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的Linux機器上,也可以實現虛擬化,容器是完全使用沙箱機制,相互之間不會有任何介面。

Docker採用 C/S架構 Docker daemon 作為服務端接受來自客戶的請求,並處理這些請求(建立、執行、分發容器)。 客戶端和服務端既可以執行在一個機器上,也可通過 socket 或者RESTful API 來進行通訊。

Docker daemon 一般在宿主主機後臺執行,等待接收來自客戶端的訊息。 Docker 客戶端則為使用者提供一系列可執行命令,使用者用這些命令實現跟 Docker daemon 互動。


安裝

前提條件 Docker 要求 Ubuntu 系統的核心版本高於 3.10 ,檢視本頁面的前提條件來驗證你的 Ubuntu 版本是否支援 Docker。

通過 uname -r 命令檢視你當前的核心版本

# uname -r
4.4.0-93-generic

使用指令碼安裝 Docker

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

複製程式碼
# wget -qO- https://get.docker.com/ | sh
# Executing docker install script, commit: 11aa13e
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq apt-transport-https ca-certificates curl software-properties-common >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
+ sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty edge" > /etc/apt/sources.list.d/docker.list
+ [ ubuntu = debian ]
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sh -c docker version
Client:
 Version:      17.11.0-ce
 API version:  1.34
 Go version:   go1.8.3
 Git commit:   1caf76c
 Built:        Mon Nov 20 18:36:37 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.11.0-ce
 API version:  1.34 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   1caf76c
 Built:        Mon Nov 20 18:35:10 2017
 OS/Arch:      linux/amd64
 Experimental: false
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 your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.
複製程式碼

安裝完成後有個提示:

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

2、啟動docker 後臺服務

sudo service docker start
start: Job is already running: docker

3、用Hello World校驗Docker的安裝

用Docker執行Hello World映象,命令如下:

複製程式碼
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
複製程式碼

可見,Docker註冊伺服器從Docker Hub獲取到最新的Hello World映象,下載到了本地。可以再次執行Hello World映象。

複製程式碼
# 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
複製程式碼

 二、使用

1、Docker Hello World

Docker 允許你在容器內執行應用程式, 使用 docker run 命令來在容器內執行一個應用程式

複製程式碼
[email protected]:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete 
759aa75f3cee: Pull complete 
3fa871dc8a2b: Pull complete 
224c42ae46e7: Pull complete 
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
Hello world
複製程式碼

Docker首先從本地主機上查詢映象是否存在,如果不存在,Docker 就會從映象倉庫 Docker Hub 下載公共映象。這裡發現映象ubuntu:15.10 不存在,正在從映象倉庫下載

引數解析:

  • docker: Docker 的二進位制執行檔案。

  • run:與前面的 docker 組合來執行一個容器。

  • ubuntu:15.10:指定要執行的映象,Docker首先從本地主機上查詢映象是否存在,如果不存在,Docker 就會從映象倉庫 Docker Hub 下載公共映象。

  • /bin/echo "Hello world": 在啟動的容器裡執行的命令

[email protected]:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

以上命令完整的意思可以解釋為:Docker 以 ubuntu15.10 映象建立一個新容器,然後在容器裡執行 bin/echo "Hello world",然後輸出結果。

2、執行互動式的容器

通過docker的兩個引數 -i -t,讓docker執行的容器實現"對話"的能力

[email protected]:~# docker run -i -t ubuntu:15.10 /bin/bash
[email protected]:/#

引數解析:

  • -t:在新容器內指定一個偽終端或終端。

  • -i:允許你對容器內的標準輸入 (STDIN) 進行互動。

此時我們已進入一個 ubuntu15.10系統的容器,嘗試在容器中執行命令 cat /proc/version 和 ls 分別檢視當前系統的版本資訊和當前目錄下的檔案列表

複製程式碼
[email protected]:/# cat /proc/version
Linux version 4.4.0-93-generic ([email protected]28) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) ) #116~14.04.1-Ubuntu SMP Mon Aug 14 16:07:05 UTC 2017
[email protected]:/# cat /etc/issue
Ubuntu 15.10 \n \l
[email protected]:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
複製程式碼

可以通過執行exit命令或者使用CTRL+D來退出容器,檢視當前真實的伺服器系統版本

[email protected]:/# exit
exit
[email protected]:~# cat /etc/issue
Ubuntu 14.04.5 LTS \n \l

3、啟動容器(後臺模式)

使用以下命令建立一個以程序方式執行的容器

[email protected]:~# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
6ddd469230b5402251c4fd3214b63f6a6af7d9e2711f8944a9d74b36346bbd5a

在輸出中,我們沒有看到期望的"hello world",而是一串長字元 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63 這個長字串叫做容器ID,對每個容器來說都是唯一的,我們可以通過容器ID來檢視對應的容器發生了什麼。

首先,我們需要確認容器有在執行,可以通過 docker ps 來檢視

[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
6ddd469230b5        ubuntu:15.10        "/bin/sh -c 'while t…"   About a minute ago   Up About a minute                       romantic_visvesvaraya

CONTAINER ID:容器ID

NAMES:自動分配的容器名稱

在容器內使用docker logs命令,檢視容器內的標準輸出

(1)使用ID

[email protected]:~# docker logs 6ddd469230b5
hello world
hello world
hello world
...

(2)使用NAMES

[email protected]:~# docker logs romantic_visvesvaraya
hello world
hello world
...

發現結果是一樣的,輸出了同樣的結果

4、停止容器

使用 docker stop 命令來停止容器,讓然啦,要指定停止的容器物件啦,不然會這樣子

複製程式碼
[email protected]:~# docker stop 
"docker stop" requires at least 1 argument.
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags]

Stop one or more running containers
複製程式碼

指定一個容器

[email protected]:~# docker stop 6ddd469230b5
6ddd469230b5

通過 docker ps 檢視,容器已經停止工作

[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

也可以用下面的NAMES命令來停止

docker stop romantic_visvesvaraya