初學docker容器
- docker是什么
- docker與虛擬機的區別
- docker使用場景
- docker的原理
- docker三個統一和docker三大組件
- docker引擎
- docker-server組態檔
- docker的部署(20版)
- docker鏡像操作
docker是什么
- docker是一種輕量級的虛擬機
- 在linux容器中運行應用并且開源
docker與虛擬機的區別
| 不同點 | 容器 | 虛擬機 |
|---|---|---|
| 啟動速度上 | 秒級 | 分鐘級 |
| 運行性能上 | 接近原生(直接運行在內核中的90%) | 50%左右的損失 |
| 數量 | 依據行程,可以有很多行程成百上千個 | 一般幾十臺(作業系統級別) |
| 隔離 | 行程級別 | 系統級別(更徹底的隔離) |
| 磁盤占用 | MB | GB(作業系統的鏡像一般在幾個G左右) |
| 作業系統 | 主要支持linux | 幾乎所有系統 |
| 封裝程度 | 只打包專案代碼和依賴關系,共享宿主機內核 | 完整的作業系統,與宿主機互相隔離 |
同時docker解決了vm的環境孤島問題,docker可以自定義傳遞引數
docker使用場景
- 用來打包應用程式簡化部署
- 脫離底層硬體任意遷移
- 持續集成和持續交付(CI/CD):開發到測驗發布
- 部署微服務
- 提供PAAS產品
docker的原理
cgroup資源控制與namespace名稱空間結合控制管理6個名稱空間資源實作完整隔離/完全隔離
- mount :檔案系統,掛載點
- user :操作行程的用戶和用戶組
- pid :行程編號
- uts :主機名和主機域
- ipc:信號量,訊息佇列,共享記憶體(不同的應用呼叫記憶體資源時使用不同的記憶體空間)
- net:網路設備,網路協議堆疊,埠等
該mount命名空間:管理檔案系統掛載點
該pid命名空間:行程隔離(pid:行程id)
該uts命名空間:隔離內核和版本識別符號(uts:Unix時間共享系統)
該ipc命名空間:管理訪問ipc資源(ipc:行程間互相通信)
該net命名空間:管理網路介面(net:網路)
docker三個統一和docker三大組件
docker把容器化技術做成了標準化平臺
- docker引擎統一了基礎設施環境—》docker環境
- docker引擎統一了程式打包方式—》docker鏡像
- docker引擎統一了程式部署方式—》docker容器–》基于鏡像,運行為容器(可運行的環境)
實作了一次構建,多次,多處使用
三大組件: - 鏡像:作為模板,一組資源的集合,包含了應用程式軟體包,應用程式相關的依賴包,運行應用程式所需要的基礎環境
- 容器:運行狀態/運行時狀態,基于鏡像的一種運行時狀態
- 倉庫:存放鏡像模板的地方,倉庫分類:公共倉庫–》docker hub,私有倉庫–》registry harbor
docker引擎
docker引擎具有以下主要組件的C/S應用程式(客戶端-服務器)
server端:服務器是一種長期運行的程式,成為守護行程
client端:REST API指定程式可以用來與守護程式進行通信并指示其操作的介面
docker-server組態檔
在/etc/docker/daemon.json中配置
{
"graph":"/data/docker", //資料目錄
"storage-driver":"overlay2", //存盤引擎
"insecure-registries" ["registry.access.redhat.com","quary.io"] //私有倉庫
"registry-mirrors": ["https://cn90fxk6.mirror.aliyuncs.com"] //鏡像加速
"bip":"172.17.0.1/24", //docker網路
"exec-opts":["native.cgroupdriver=systemd"], //啟動時的額外引數
"live-restore":true //當docker容器存盤引擎掛了后,使用docker跑起來的容器還能運行
}
docker的部署(20版)
- 安裝依賴包
[root@node1 nginx]# yum install -y yum-utils device-mapper-persistent-data lvm2
- 設定阿里云鏡像源
[root@node1 yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 安裝docker-ce社區版
[root@node1 yum.repos.d]# yum install -y docker-ce
- 然后啟動docker
[root@node1 yum.repos.d]# systemctl enable docker
[root@node1 yum.repos.d]# systemctl start docker
-
設定鏡像加速,去阿里云的鏡像加速分類

-
將底下的代碼直接復制到命令列就可以了,要選對應的作業系統

-
查看是否設定成功
[root@node1 yum.repos.d]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://cn90fxk6.mirror.aliyuncs.com"]
}
docker鏡像操作
[root@node1 yum.repos.d]# 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. //由服務端的守護行程從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/
- 查詢docker版本
[root@node1 yum.repos.d]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:54:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.9
GitCommit: e25210fe30a0a703442421b0f60afac609f950a3
runc:
Version: 1.0.1
GitCommit: v1.0.1-0-g4144b63
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@node1 yum.repos.d]# 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.6.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 14
Running: 5
Paused: 0
Stopped: 9
Images: 63
Server Version: 20.10.8
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: e25210fe30a0a703442421b0f60afac609f950a3
runc version: v1.0.1-0-g4144b63
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 5.712GiB
Name: node1
ID: QL6Y:HC6L:E57G:UWHJ:E7FY:J47A:YF6Z:GLL2:DETH:DY4C:STNH:ZGFS
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://cn90fxk6.mirror.aliyuncs.com/
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
- 搜索鏡像
[root@node1 yum.repos.d]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15420 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2063 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 816 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 240
linuxserver/nginx An Nginx container, brought to you by LinuxS… 152
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 141 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 135 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 106 [OK]
jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 92 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 72 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 56 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 55
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 47
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 24 [OK]
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 20
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 19
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
bitwarden/nginx The Bitwarden nginx web server acting as a r… 11
flashspys/nginx-static Super Lightweight Nginx Image 10 [OK]
mailu/nginx Mailu nginx frontend 9 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1
[root@node1 yum.repos.d]# docker search centos:7
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
benwang6/tedu-jdk oracle jdk 8u281 centos:7 JAVA_HOME=/usr/jdk… 5
vikingco/python Python Stack Docker Base Image: Based on cen… 1
sndnvaps/docker-golang build latest golang in centos:7 1 [OK]
legerete/nginx-php71 LA[->]P - Centos:7 + Nginx + PHP 7.1 1 [OK]
peltikalle/basepython Base image with Centos:7 and Python 3.5.2 1 [OK]
mjstealey/mariadb-galera MariaDB Galera cluster in Docker - based fro… 1 [OK]
acktsw/java oracle jdk 8u171 , centos:7, timeZone:+8, e… 0 [OK]
macedigital/nodejs Latest NodeJS for CentOS:7 0 [OK]
grossws/nginx nginx (mainline) on grossws/centos:7 0 [OK]
europeanspallationsource/oracle-jdk-maven-jenkins ICS oracle-jdk + maven + jenkins users image… 0
pbieberstein/acic-findr CentOS:7 with dependencies to run 'Findr' (h… 0 [OK]
sjoeboo/rbenv Simple base container from CentOS:7 w/ rbenv… 0 [OK]
alvintz/centos centos:7.2.1511 0 [OK]
geomatikk/centos FROM centos:7 with maven 3.6.1 and openjdk-1… 0
waffleimage/centos7 Centos:7 with systemd and ssh running 0
cristo/netacuity Docker image on Centos:7 to run NetAcuity 0 [OK]
badwolf/centos from official centos:7 add gcc,gcc++,make,vi 0 [OK]
mesosphere/freeipa-server A freeIPA v4.3 container based on centos:7. … 0
acktsw/centos centos:7 0 [OK]
bbania/centos Build image based on centos:7 0
a2747/centos7 derivative images from centos:7 0
21plus2/server-jre Dockerimage base on centos:7 with server-jre 0 [OK]
europeanspallationsource/oracle-jdk-maven ICS oracle-jdk + maven image based on centos… 0
qiyue/mycat centos:7 + jdk:1.8 + mycat 0
weihoop/mysql 基于weihoop/centos:7.4.1708制作
- 下載鏡像
[root@node1 yum.repos.d]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a330b6cecb98: Pull complete
5ef80e6f29b5: Pull complete
f699b0db74e3: Pull complete
0f701a34c55e: Pull complete
3229dce7b89c: Pull complete
ddb78cb2d047: Pull complete
Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node1 yum.repos.d]# docker images //查看鏡像串列
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 2 days ago 133MB
- 獲取鏡像資訊
[root@node1 yum.repos.d]# docker inspect 822b7ec2aaf2
[
{
"Id": "sha256:822b7ec2aaf2122b8f80f9c7f45ca62ea3379bf33af4e042b67aafbf6eac1941",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e"
],
"Parent": "",
"Comment": "",
"Created": "2021-09-03T07:40:16.355730864Z",
"Container": "367d32086ac12447d36e75c9b7acbe1b5156a34a91370b9200e68783be75506c",
"ContainerConfig": {
"Hostname": "367d32086ac1",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.1",
"NJS_VERSION=0.6.1",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:d4315787e4fec867791beba140dd0e44f657cb6e4a9d75c676c7946089c20da9",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
- 添加鏡像標簽
[root@node1 yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 123 822b7ec2aaf2 2 days ago 133MB
nginx latest 822b7ec2aaf2 2 days ago 133MB
- 洗掉鏡像
[root@node1 yum.repos.d]# docker rmi nginx:123
Untagged: nginx:123
[root@node1 yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 2 days ago 133MB
- 匯出鏡像
[root@node1 ~]# docker save -o nginx_images nginx:latest
[root@node1 ~]# ls
12 1.sh 2333 45 apps initial-setup-ks.cfg ks.cfg nginx_images 模板 圖片 下載 桌面
123 1.txt 234 anaconda-ks.cfg docker_home jdk-8u91-linux-x64.tar.gz nginx 公共 視頻 檔案 音樂
- 匯入鏡像,將原有的先洗掉再匯入
[root@node1 ~]# docker load < nginx_images
d000633a5681: Loading layer [==================================================>] 72.53MB/72.53MB
63b5f2c0d071: Loading layer [==================================================>] 64.86MB/64.86MB
875b5b50454b: Loading layer [==================================================>] 3.072kB/3.072kB
ed94af62a494: Loading layer [==================================================>] 4.096kB/4.096kB
8e58314e4a4f: Loading layer [==================================================>] 3.584kB/3.584kB
d47e4d19ddec: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 2 days ago 133MB
- 查詢容器
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 創建容器
[root@node1 ~]# docker create -it nginx:latest /bin/bash
9eade02412f5ecc3e9e2006de2f59845ca50ed4a52741ad9f0a8fb43ce5086f3
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9eade02412f5 nginx:latest "/docker-entrypoint.…" 3 seconds ago Created keen_chatterjee
//-i是讓容器的標準輸入保持打開
//-t是分配一個偽終端
//-d是后臺守護行程的方式運行
- 啟動容器
[root@node1 ~]# docker start 9eade02412f5
9eade02412f5
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9eade02412f5 nginx:latest "/docker-entrypoint.…" About a minute ago Up 2 seconds 80/tcp keen_chatterjee
- 一次性執行來啟動容器
[root@node1 ~]# docker run centos:7 /usr/bin/bash -c ls /
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
- 停止容器
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9eade02412f5 nginx:latest "/docker-entrypoint.…" 4 minutes ago Exited (137) 6 seconds ago //非0狀態值都是停止狀態,137是stop命令停止的指數 keen_chatterjee
- 進入容器,兩種方式都可以,但是exec需要在容器運行時才能進入
[root@node1 ~]# docker run -it nginx:latest /bin/bash
root@d36a26b3e1d2:/#
[root@node1 ~]# docker exec -it 9eade02412f5 /bin/bash
root@9eade02412f5:/#
docker run -it 會創建前臺行程,但是會在輸入exit后終止行程
docker exec -it 會連接到容器,可以像ssh一樣進入容器內部,進行操作,可以通過exit退出
- 容器匯出
[root@node1 ~]# docker export 9eade02412f5 > nginx_1
[root@node1 ~]# ls
12 1.sh 2333 45 apps initial-setup-ks.cfg ks.cfg nginx_1 公共 視頻 檔案 音樂
- 容器匯入(生成鏡像)
[root@node1 ~]# cat nginx_1 | docker import - nginx:latest
sha256:ae834c84afd17bb12708bd2dd4d53e8432c43d6378d0ed405e1fd580dd6f77ad
- 洗掉容器
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95b40e409895 nginx:latest "/bin/bash" 9 seconds ago Exited (0) 4 seconds ago sad_heyrovsky
[root@node1 ~]# docker rm 95b40e409895
95b40e409895
強制洗掉正在運行中的容器可以加一個-f
- 批量洗掉容器
[root@node1 ~]# docker ps -a | awk '{print "docker rm "$1}' | bash
3e0845eacc5c
5e0ecd151d57
b8da82b16ba7
2930dd871f01
668103e78d6c
或者
[root@node1 ~]# docker rm `docker ps -qa`
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/298117.html
標籤:其他
上一篇:一道面試題--設計炸船互動游戲
