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 命令來在容器內運行一個應用程序

root@aliyunTinywan:~# 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": 在啟動的容器里執行的命令

root@aliyunTinywan:~# docker run ubuntu:15.10 /bin/echo "Hello world" Hello world

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

2、運行交互式的容器

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

root@aliyunTinywan:~# docker run -i -t ubuntu:15.10 /bin/bash root@aliyunTinywan:/#

參數解析:

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

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

此時我們已進入一個 ubuntu15.10系統的容器,嘗試在容器中運行命令 cat /proc/version 和 ls 分別查看當前系統的版本信息和當前目錄下的文件列表

root@aliyunTinywan:/# cat /proc/version Linux version 4.4.0-93-generic (buildd@lcy01-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 root@aliyunTinywan:/# cat /etc/issue Ubuntu 15.10 \n \l root@aliyunTinywan:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

可以通過運行exit命令或者使用CTRL+D來退出容器,查看當前真實的服務器系統版本

root@18c5f2d5476b:/# exit exit root@iZ235mi4a64Z:~# cat /etc/issue Ubuntu 14.04.5 LTS \n \l

3、啟動容器(后台模式)

使用以下命令創建一個以進程方式運行的容器

root@iZ235mi4a64Z:~# 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 來查看

root@iZ235mi4a64Z:~# 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

root@iZ235mi4a64Z:~# docker logs 6ddd469230b5 hello world hello world hello world ...

(2)使用NAMES

root@iZ235mi4a64Z:~# docker logs romantic_visvesvaraya hello world hello world ...

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

4、停止容器

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

root@iZ235mi4a64Z:~# 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

指定一個容器

root@iZ235mi4a64Z:~# docker stop 6ddd469230b5 6ddd469230b5

通過 docker ps 查看,容器已經停止工作

root@iZ235mi4a64Z:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

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

docker stop romantic_visvesvaraya

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM