大資料物流專案:Docker 入門使用(二)

語言: CN / TW / HK

持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第6天,點選檢視活動詳情

Logistics_Day02:Docker 入門使用

img

01-[複習]-上次課程內容回顧

主要講解物流大資料專案專案介紹和專案解決方案。

``` 1、專案介紹 行業背景、專案背景:資料量大(海量資料、大規模資料)和業務複雜 本專案基於一家大型物流公司研發的智慧物流大資料平臺 物流實時大屏系統:ClickHouse資料庫 + 服務介面SpringCloud + NodeJS/Vue

2、專案解決方案 物流快讀:業務流程,6個部分組成,中間4個部分 快遞單:tbl_express_bill,運單:tbl_waybill,客戶表、快遞員表、倉庫表、車輛運輸表 邏輯架構 業務資料:資料庫RDBMs(Oracle、MySQL) 採集資料:OGG、Canal 資料緩衝:Kafka 實時ETL:StructuredStreaming 資料儲存:Kudu(離線報表和即席查詢Impala)、Es(物流資訊檢索)、ClickHouse(實時大屏和服務介面) 資源統一排程:YARN、Azkaban 資料流轉圖 業務資料儲存 大資料儲存與分析 技術選項,為什麼選擇這些框架?? Kafka,大資料流式處理 計算Spark,目前來說,能夠使用Spark搞定,就不用Flink 非功能描述 伺服器選項:雲主機還是物理機 叢集規模:按照資料量計算和規劃 伺服器資源規劃 本次學習專案時,伺服器說明 ```

02-[瞭解]-第2天:課程內容提綱

主要講解Docker容器基本使用

``` 1、Docker 容器是什麼 虛擬化是什麼 容器是什麼 虛擬機器與容器比較

2、Docker 框架安裝部署 Docker容器引擎框架架構 C/S 安裝,線上聯網yum安裝 啟動服務及加速配置

3、Docker容器命令使用 映象image 容器container ```

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-NlPjC7CH-1651888604573)(/img/image-20210518151229534.png)]

03–[瞭解]-大資料專案為什麼使用Docker

​ 隨著大資料平臺型產品方向的深入應用實踐和Docker開源社群的逐漸成熟,業界有不少的大資料研發團隊開始使用Docker。

Docker會讓大資料平臺部署更加簡單快捷、讓研發和測試團隊整合交付更加敏捷高效、讓產線環境的運維更加有質量保障。

1615533027804

虛擬機器可以實現一些開發、測試與運維之間的問題,但是存在很多缺陷:

1615533188596

官網:https://www.docker.com/

1615533217031

04–[理解]-什麼是虛擬化

虛擬化(Virtualization),在Windows系統上安裝VMWare虛擬化軟體,可以將物理機器資源虛擬化到機器上(VM),在虛擬機器上安裝作業系統,執行應用程式。

在計算機中,虛擬化(英語:Virtualization)是一種資源管理技術,是將計算機的各種實體資源,予以抽象、轉換後呈現出來,打破實體結構間的不可切割的障礙,使使用者可以比原來的組態更好的方式來應用這些資源。

1615533757293

​ 虛擬化簡單講,就是把一臺物理計算機虛擬成多臺邏輯計算機,每個邏輯計算機裡面可以執行不同的作業系統,相互不受影響,這樣就可以充分利用硬體資源。

  • 1)、型別一、直接在硬體資源上安裝虛擬化軟體,建立虛擬機器,安裝作業系統,執行應用
  • 2)、型別二、硬體之上安裝作業系統,再安裝虛擬化軟體,建立虛擬機器,安裝作業系統,執行應用

1615533906190

05–[掌握]-初識Docker 容器

​ Docker是一個開源的應用容器引擎(框架)建立容器,部署執行應用,比如建立容器Contanier,執行MySQL資料庫。

1615535123251

Docker將應用及依賴打包到容器Contanier(輕量級,可移植),釋出Deploy到任何流向linux伺服器執行

06–[理解]-容器與虛擬機器比較

Docker是一種容器技術,==解決軟體跨環境遷移的問題===

  • 1)、什麼是虛擬機器

虛擬機器是一個計算機系統的模擬,簡單來說,虛擬機器可以實現在一臺物理計算機上模擬多臺計算機執行任務。

1615535594325

​ VMs也消耗大量系統資源,每個VM不僅執行一個OS的完整copy並且需要所有硬體的虛擬化copy,這消耗大量RAM和CPU。

  • 2)、容器是將作業系統虛擬化
  • 容器是在作業系統之上,每個容器共享OS核心,執行檔案和庫等
  • 容器是非常輕量的,僅僅MB水平並且幾秒即可啟動
  • VM相比,容器僅需OS、支撐程式和庫檔案便可執行應用

1615535739222

虛擬機器VM和容器Contanier比較

1615535830164

07–[掌握]-Docker 服務端和客戶端

​ Docker是一個客戶端-服務端(C/S)架構程式,Docker客戶端只需要向Docker服務端或者守護程序發出請求,服務端或者守護程序完成所有工作返回結果。

  • 1)、Docker 服務端(Server),執行以後就是一個守護程序(後臺執行),類比MySQL資料庫服務
  • ​ 啟動Docker 服務
  • 2)、Docker 客戶端(Client),向服務端傳送請求指令,Server端接收以後,執行操作,完成以後返回結果給客戶端,類比mysql
  • 使用docker命令,操作容器

1612077125442

​ 在同一臺宿主機器上執行Docker守護程序或者客戶端,Docker 容器引擎提供命令列:docker,就是客戶端Client,可以向Docker 服務端傳送請求

1612077210810

docker引擎是一個c/s結構的應用,主要元件見下圖:

  • 1)、映象image:模板,相當Java中定義類
  • 2)、容器Contanier:例項,相當Java中基於類建立物件

1615536410014

1、Server是一個常駐程序 2、REST API 實現了client和server間的互動協議 3、CLI 實現容器和映象的管理,為使用者提供統一的操作介面

08–[掌握]-Docker 結構(映象和容器)

​ Docker使用C/S架構,==Client 通過介面與Server程序通訊實現容器的構建,執行和釋出==。client和server可以執行在同一臺叢集,也可以通過跨主機實現遠端通訊。

構建(build):建立映象、執行(run):建立容器和啟動、釋出(Deploy):應用執行和遷移

  • 1)、映象Image

  • 自定義映象檔案:DockerFile,build構建出映象

  • 可以從註冊中心倉庫中拉取映象,最大公開映象倉庫:hub.docker.com

docker pull elasticsearch

  • 2)、容器Contanier

  • 從映象中建立容器,執行應用。

1615537043833

  • 1)、Docker 映象
  • Docker 映象(Image)就是一個只讀的模板。

1615537480347

映象(Image)就是一堆只讀層(read-only layer)的統一視角

1615537538645

右邊可以看到多個只讀層,它們重疊在一起,除了最下面一層,其它層都會有一個指標指向下一層。

統一檔案系統(Union File System)技術能夠將不同的層整合成一個檔案系統,為這些層提供了一個統一的視角,這樣就隱藏了多層的存在,在使用者的角度看來,只存在一個檔案系統。

1615537623172

映象採用分層的架構,更多為了更好利用資源,進行重用(各層可以被重複使用)。

09–[掌握]-Docker 安裝及服務啟動

​ Docker容器引擎(框架),Docker Server服務端和Docker Client客戶端,將其安裝在一臺機器上。

​ 直接解壓提供壓縮包【CentOS7.7.zip】,匯入至VMWare虛擬軟體中,啟動虛擬機器。

匯入虛擬機器以後,啟動虛擬機器時,選擇已經匯入移動該虛擬機器:192.168.88.10, node1.itcast.cn,root/123456

1612079957733

修改虛擬機器為命令列模式(啟動後為桌面模式):

```ini [[email protected] ~]# systemctl get-default graphical.target

[[email protected] ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

[[email protected] ~]# [[email protected] ~]# systemctl get-default
multi-user.target

[[email protected] ~]# shutdown -r now ```

​ 將Docker安裝到Centos上,注意:建議安裝在Centos7.x以上的版本。整個安裝步驟參考教案即可,採用線上yum源方式安裝,所以需要連線網路。

官網:https://docs.docker.com/engine/install/centos/

1615538753532

安裝完成以後,啟動Docker服務:systemctl start docker

1615539483022

10–[掌握]-Docker 配置阿里雲映象加速

​ 鑑於國內網路問題,後續拉取 Docker 映象十分緩慢,配置加速器來解決,配置阿里雲加速器。

  • 1)、登入阿里雲地址(需要註冊)

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-mLaguI8W-1651888604583)(/img/1615539708242.png)]

  • 2)、獲取加速器地址:

https://yeu2889p.mirror.aliyuncs.com

  • 3)、配置加速器地址配置,執行如下命令即可

ini sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://yeu2889p.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker

Docker 引擎服務啟動、停止和關閉命令:

1615539901884

11–[掌握]-Docker 容器快速執行ContOS 系統

使用Docker建立容器Contanier,執行CentOS 7作業系統。

  • 1)、拉取映象(註冊中心倉庫)

```ini [[email protected] ~]# docker pull centos:7 7: Pulling from library/centos 2d473b07cdd5: Pull complete Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e Status: Downloaded newer image for centos:7 docker.io/library/centos:7

[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 8652b9f0cb4c 3 months ago 204MB ```

  • 2)、建立容器

```ini [[email protected] ~]# docker run -di --name=mycentos centos:7 21cc295413f0730ecdc64605081bab53775f982c56163713c8d0360e6a5f015d

[[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 11 seconds ago Up 10 seconds mycentos ```

  • 3)、進入容器

ini [[email protected] ~]# docker exec -it mycentos /bin/bash [[email protected] /]# [[email protected] /]# hostname 21cc295413f0 [[email protected] /]# [[email protected] /]# vi aa.txt [[email protected] /]# [[email protected] /]# more a aa.txt anaconda-post.log [[email protected] /]# more aa.txt Hello Docker!

12-[掌握]-Docker 幫助命令

​ 已經安裝部署Docker引擎:systemctl start docker

ini [[email protected] ~]# ps -ef|grep docker root 1851 1 0 08:56 ? 00:00:01 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Docker 容器引擎,提供客戶端:docker

1615600508387

```ini [[email protected] ~]# docker version Client: Docker Engine - Community Version: 20.10.5 API version: 1.41 Go version: go1.13.15 Git commit: 55c4c88 Built: Tue Mar 2 20:33:55 2021 OS/Arch: linux/amd64 Context: default Experimental: true

Server: Docker Engine - Community Engine: Version: 20.10.5 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 363e9a8 Built: Tue Mar 2 20:32:17 2021 OS/Arch: linux/amd64

[[email protected] ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server: Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 1 Server Version: 20.10.5 Storage Driver: overlay2 Backing Filesystem: xfs

[[email protected] ~]# docker --help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit

Management Commands: app Docker App (Docker Inc., v0.9.1-beta3) builder Manage builds buildx Build with BuildKit (Docker Inc., v0.5.1-docker) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes

Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/ ```

在Docker容器引擎中,最重要命令就是:映象命令和容器命令

1615600776170

13-[掌握]-Docker 映象命令

首先,看一下映象相關常用命令:搜尋映象search、拉取映象pull、檢視映象list及刪除映象rmi

從註冊中心倉庫中獲取映象,對映象進行管理操作

註冊中心,倉庫地址:https://hub.docker.com

1615600863236

```ini

搜尋映象image

[[email protected] ~]# clear [[email protected] ~]# docker search elasticsearch NAME DESCRIPTION STARS OFFICIAL AUTOMATED elasticsearch Elasticsearch is a powerful open source sear… 4894 [OK]
nshou/elasticsearch-kibana Elasticsearch-7.10.1 Kibana-7.10.1 125 [OK] itzg/elasticsearch Provides an easily configurable Elasticsearc… 71 [OK] elastichq/elasticsearch-hq Official Docker image for ElasticHQ: Elastic… 70 [OK] ```

搜尋映象時,更多在WEB 介面搜尋

1615601089585

當搜尋到映象以後,可以倉庫中將映象拉取pull到本地映象

每個映象image,都有版本tag,所以在拉取映象時,需要選擇合適的版本tag,當拉取映象時,不指定tag版本,預設情況下將會拉取最新tag版本映象。

ini [[email protected] ~]# docker pull elasticsearch:7.11.1 7.11.1: Pulling from library/elasticsearch 7a0437f04f83: Downloading [===> ] 5.934MB/75.18MB 744c7f8ba15d: Downloading [======================> ] 10.09MB/22.29MB a2e5f03a9eab: Download complete edcdb0cfb01b: Downloading [> ] 539.6kB/324.5MB

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-S1117LaN-1651888604585)(/img/1615601240320-1621314383189.png)]

當映象拉取到本地以後,需要檢視映象和刪除映象

  • 1)、檢視映象

1615601300360

  • 2)、刪除映象

1615601348789

ini [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 8652b9f0cb4c 3 months ago 204MB [[email protected] ~]# [[email protected] ~]# docker rmi 8652b9f0cb4c [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker rmi centos:7 [[email protected] ~]#

14-[掌握]-Docker 容器命令之檢視與建立

​ 實際開發中,建立容器執行應用之前,首先獲取對應映象,往往都是從註冊中心倉庫中拉取,當將映象拉取到本地以後,就可以建立容器。

Docker 中容器Container執行以執行緒方式執行,所以使用ps檢視

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-ClkR1pwO-1651888604586)(/img/1615601617510-1621314383189.png)]

ini [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [[email protected] ~]# [[email protected] ~]# docker ps -f status=exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos

  • 2)、容器建立,有2種方式,容器建立時需要指定使用映象

image(模板) -> container(例項)

1615601873246

  • 方式一:啟動互動式容器,docker run -it --name=centos 映象名稱:標籤 /bin/bash

1615601963095

ini [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [[email protected] ~]# [[email protected] ~]# docker run -it centos:7 /bin/bash [[email protected] /]# hostname 46188ebaf87f [[email protected] /]# [[email protected] /]# exit exit [[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]# [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 46188ebaf87f centos:7 "/bin/bash" 32 seconds ago Exited (0) 6 seconds ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos

當建立互動式容器時,建立完成以後,直接執行容器,並且進入容器中,如果退出容器,自動將容器關閉。

  • 2)、方式二:守護式方式建立容器,docker run -di --name=容器名稱 映象名稱:標籤
  • 採用守護式方法建立容器以後,需要登入容器(相當於登入作業系統)
  • 命令:docker exec -it 容器名稱(或者容器id) /bin/bash

ini [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]# [[email protected] ~]# docker run -di --name=mycentos2 centos:7 1741ea301b3f7c60992cd7b29a29747f15c46c6be6a916bbad213e16018a3303 [[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 6 seconds ago Up 6 seconds mycentos2 [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker exec -it mycentos2 /bin/bash [[email protected] /]# [[email protected] /]# hostname 1741ea301b3f [[email protected] /]#

實際專案中,建立容器時採用守護方式建立,使用exec進入容器進行操作。

15-[掌握]-Docker 容器命令之啟動與停止

​ 當容器建立完成以後,有時需要停止容器執行、啟動容器執行,相關命令如下:

1615602427828

可以重啟容器(先停止,再啟動)

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-18MPfKlH-1651888604587)(/img/1615602461159-1621314383189.png)]

ini [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 4 minutes ago Up 4 minutes mycentos2 e56430030fff centos:7 "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago centos7 46188ebaf87f centos:7 "/bin/bash" 8 minutes ago Exited (0) 7 minutes ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 17 hours ago mycentos [[email protected] ~]# [[email protected] ~]# docker start mycentos mycentos [[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 4 minutes ago Up 4 minutes mycentos2 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Up 3 seconds mycentos [[email protected] ~]# [[email protected] ~]# docker stop mycentos2 mycentos2 [[email protected] ~]# [[email protected] ~]# docker kill 21cc295413f0 21cc295413f0 [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1741ea301b3f centos:7 "/bin/bash" 5 minutes ago Exited (137) 25 seconds ago mycentos2 e56430030fff centos:7 "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago centos7 46188ebaf87f centos:7 "/bin/bash" 9 minutes ago Exited (0) 8 minutes ago competent_fermi 21cc295413f0 centos:7 "/bin/bash" 17 hours ago Exited (137) 6 seconds ago mycentos

16-[掌握]-Docker 容器命令之IP 地址

當使用Docker 容器引擎,建立容器以後,每個容器執行時,都有IP地址,屬於Docker容器內部IP地址。

```ini [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4148cf3b3dea centos:7 "/bin/bash" 3 minutes ago Up 3 minutes centos7 827ff98c2a29 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes centos [[email protected] ~]# [[email protected] ~]# ifconfig docker0: flags=4163 mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:d4ff:fef5:9869 prefixlen 64 scopeid 0x20 ether 02:42:d4:f5:98:69 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 13 bytes 1768 (1.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags=4163 mtu 1500 inet 192.168.88.10 netmask 255.255.255.0 broadcast 192.168.88.255 inet6 fe80::20c:29ff:fea0:85 prefixlen 64 scopeid 0x20 ether 00:0c:29:a0:00:85 txqueuelen 1000 (Ethernet) RX packets 485440 bytes 672923357 (641.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 129820 bytes 8330903 (7.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 48 bytes 4080 (3.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 48 bytes 4080 (3.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

veth94d4a57: flags=4163 mtu 1500 inet6 fe80::3408:a4ff:fe46:7687 prefixlen 64 scopeid 0x20 ether 36:08:a4:46:76:87 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vetha19c80a: flags=4163 mtu 1500 inet6 fe80::cc15:1fff:fe6d:991d prefixlen 64 scopeid 0x20 ether ce:15:1f:6d:99:1d txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099 mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:f6:83:da txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ```

如何檢視容器中IP地址呢?

ini [[email protected] ~]# docker inspect centos [ { "Id": "827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1", "Created": "2021-03-13T03:02:42.354813578Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 53376, "ExitCode": 0, "Error": "", "StartedAt": "2021-03-13T03:02:47.515567445Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf", "ResolvConfPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/resolv.conf", "HostnamePath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/hostname", "HostsPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/hosts", "LogPath": "/var/lib/docker/containers/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1/827ff98c2a294a03191f4002bbf582d4e3ce84b0f3211c22c56ed3c561eb6dc1-json.log", "Name": "/centos", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7-init/diff:/var/lib/docker/overlay2/c907f57265207bf28d834e05f41753b198a5b3df2b84ddd064e053d741e3ad5f/diff", "MergedDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/merged", "UpperDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/diff", "WorkDir": "/var/lib/docker/overlay2/daba5b9305c5e683eafbe5e087d1bebee34c98be005db15649d54ad4477b5fe7/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "827ff98c2a29", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos:7", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201113", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "958164ab6fd78db5b500d75ec0c798318a7d1a89cc4e1de3e168e3a062e7f8d9", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/958164ab6fd7", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "1da3cb4de7247d1002b27ced094ea5d7a6a2a2c23d8157b77461cbee886e8f1c", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "051cdc01a196f22b35b248f863b7212fa2256adb4f1decf877a09bd05bdd4d59", "EndpointID": "1da3cb4de7247d1002b27ced094ea5d7a6a2a2c23d8157b77461cbee886e8f1c", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]

刪除容器:當容器不再被使用時,可以直接刪除容器Contanier

1615605219378

當建立容器以後,並且執行時,包含IP地址,提供相關網路介面卡將宿主機IP地址轉換為容器IP地址。此外,如果容器中執行服務,需要暴露埠號,需要設定容器埠號與宿主機埠號對映。

image-20210518175026918

17-[理解]-Docker 應用部署之MySQL

接下來,採用Docker容器部署資料庫和服務:MySQL資料庫。

步驟:①搜尋映象、②拉取映象、③檢視映象、④啟動容器、⑤停止容器、⑥移除容器

```ini [[email protected] ~]# docker search mysql

[[email protected] ~]# docker pull centos/mysql-57-centos7 Using default tag: latest latest: Pulling from centos/mysql-57-centos7 d8d02d457314: Pull complete a11069b6e245: Pull complete 596303fb1aa3: Pull complete a29499e779a7: Pull complete 17d1a52c2e00: Pull complete ed24591227fe: Pull complete de0ad46e3ed9: Pull complete c62e4a984a9c: Pull complete 01d54c6bda68: Pull complete Digest: sha256:e08ee4d43b7356607685b69bde6335e27cf20c020f345b6c6c59400183882764 Status: Downloaded newer image for centos/mysql-57-centos7:latest docker.io/centos/mysql-57-centos7:latest

[[email protected] ~]# docker images centos/mysql-57-centos7 latest f83a2938370c 17 months ago 452MB

```

命令:docker run -dt --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7

其中在建立容器時,需要指定埠號對映

1615605653675

指定埠號對映方式:

1615605683345

```ini [[email protected] ~]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7 b3da50687358586f0b0a430ba138b0a080b39dbf244de4f92152919f51a3dae9

[[email protected] ~]# [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3da50687358 centos/mysql-57-centos7 "container-entrypoin…" 40 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp mysql

[[email protected] ~]# docker exec -it mysql /bin/bash bash-4.2$ ps PID TTY TIME CMD 202 pts/0 00:00:02 bash 211 pts/0 00:00:00 ps bash-4.2$ bash-4.2$ ps -ef|grep mysql mysql 1 0 2 03:22 ? 00:00:02 /opt/rh/rh-mysql57/root/usr/libexec/mysqld --defaults-file=/etc/my.cnf mysql 202 0 12 03:23 pts/0 00:00:02 /bin/bash mysql 212 202 0 03:23 pts/0 00:00:00 ps -ef mysql 213 202 0 03:23 pts/0 00:00:00 grep mysql ```

1615605915549