特性 | Docker容器 | 虚拟机 |
---|---|---|
启动速度 | 秒级 | 分钟级 |
计算能力损耗 | 几乎无 | 损耗 50%左右 |
性能 | 接近原生 | 弱于 |
系统支持量(单机) | 上千个 | 几十个 |
隔离性 | 资源隔离/限制 | 完全隔离 |
1)镜像
2)容器
3)仓库
Docker仓库是用来集中保存镜像的地方,当创建了自己的镜像之后,可以使用push命令将它上传到公有仓库(Public)或者私有仓库(Private)。当下次要在另外一台机器上使用这个镜像时,只需从仓库获取
systemctl stop firewalld.service
setenforce 0
yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-utils:提供了 yum-config-manager 工具
#device mapper: 是Linux内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构
#device mapper存储驱动程序需要 device-mapper-persistent-data 和 lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker.service
systemctl enable docker.service
[root@lion yum.repos.d]# docker version
Client: Docker Engine - Community
Version: 24.0.4
API version: 1.43
Go version: go1.20.5
Git commit: 3713ee1
Built: Fri Jul 7 14:54:21 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.4
API version: 1.43 (minimum version 1.12)
Go version: go1.20.5
Git commit: 4ffc614
Built: Fri Jul 7 14:53:26 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@lion yum.repos.d]# docker info
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: 0 # 容器数量
Running: 0
Paused: 0
Stopped: 0
Images: 1 # 镜像数量
Server Version: 20.10.3 # server 版本
Storage Driver: overlay2 # docker 使用的是 overlay2 文件驱动
Backing Filesystem: xfs # 宿主机上的底层文件系统
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs # Cgroups 驱动
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.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-693.el7.x86_64 # 宿主机的相关信息
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 976.3MiB
Name: localhost.localdomain
ID: Y4ES:FTH2:ZJL7:MRVE:RJVB:WJIB:S7BV:C5IZ:LMBR:E4G5:QWSM:SNDT
Docker Root Dir: /var/lib/docker # docker 数据存储目录
Debug Mode: false
Registry: https://index.docker.io/v1/ # registry 地址
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors: # 加速站点
https://6ijb8ubo.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@lion yum.repos.d]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 18768 [OK]
unit Official build of NGINX Unit: Universal Web … 6 [OK]
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 92
nginxproxy/acme-companion Automated ACME SSL certificate generation fo… 116
bitnami/nginx Bitnami nginx Docker Image 168 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 29 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 96
nginxproxy/docker-gen Generate files from docker container meta-da… 12
kasmweb/nginx An Nginx image based off nginx:alpine and in… 6
rancher/nginx-ingress-controller 11
rancher/nginx-ingress-controller-defaultbackend 2
bitnami/nginx-exporter 5
rancher/nginx 2
rapidfort/nginx-ib RapidFort optimized, hardened image for NGIN… 10
rapidfort/nginx RapidFort optimized, hardened image for NGINX 14
vmware/nginx-photon 1
rapidfort/nginx-official RapidFort optimized, hardened image for NGIN… 10
nginxproxy/forego Foreman in Go 0
vmware/nginx 2
rancher/nginx-conf 0
bitnamicharts/nginx 0
linuxserver/nginx An Nginx container, brought to you by LinuxS… 204
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 77 [OK]
elestio/nginx-auto-ssl The simpliest solution to add SSL cert to yo… 0
rancher/nginx-ssl
[root@lion yum.repos.d]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
faef57eae888: Pull complete
76579e9ed380: Pull complete
cf707e233955: Pull complete
91bb7937700d: Pull complete
4b962717ba55: Pull complete
f46d7b05649a: Pull complete
103501419a0a: Pull complete
Digest: sha256:08bc36ad52474e528cc1ea3426b5e3f4bad8a130318e3140d6cfe29c8892c7ef
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@lion yum.repos.d]# sudo mkdir -p /etc/docker
[root@lion yum.repos.d]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
}
[root@lion yum.repos.d]# sudo systemctl daemon-reload
[root@lion yum.repos.d]# sudo systemctl restart docker
[root@lion yum.repos.d]# cd /var/lib/docker
[root@lion docker]# ls
buildkit containers engine-id image network overlay2 plugins runtimes swarm tmp volumes
[root@lion docker]# cat /var/lib/docker/image/overlay2/repositories.json
[root@lion docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 2 weeks ago 187MB
字段 | 作用 |
---|---|
REPOSITORY | 镜像属于的仓库 |
IMAGE ID | 镜像的唯一ID 号,唯一标识一个镜像 |
TAG | 镜像的标签信息,标记同一个仓库中的不同镜像 |
CREATED | 镜像创建时间 |
VIRTUAL SIZE | 镜像大小 |
[root@lion docker]# docker inspect 镜像ID号
[root@lion docker]# docker tag nginx:latest nginx:web
[root@lion docker]# docker images | grep nginx
nginx latest 021283c8eb95 2 weeks ago 187MB
nginx web 021283c8eb95 2 weeks ago 187MB
docker rmi 仓库名称:标签 #当一个镜像有多个标签时,只是删除其中指定的标签
docker rmi 镜像ID号 #会彻底删除该镜像
[root@lion docker]# docker rmi nginx:web
Untagged: nginx:web
[root@lion docker]# docker save -o nginx nginx:latest
[root@lion docker]# ls -lh
总用量 183M
drwx--x--x. 4 root root 138 7月 19 10:29 buildkit
drwx--x---. 2 root root 6 7月 19 10:29 containers
-rw-------. 1 root root 36 7月 19 10:29 engine-id
drwx------. 3 root root 22 7月 19 10:29 image
drwxr-x---. 3 root root 19 7月 19 10:29 network
-rw-------. 1 root root 183M 7月 19 15:20 nginx
drwx--x---. 10 root root 4.0K 7月 19 14:54 overlay2
drwx------. 4 root root 32 7月 19 10:29 plugins
drwx------. 2 root root 6 7月 19 14:54 runtimes
drwx------. 2 root root 6 7月 19 10:29 swarm
drwx------. 2 root root 6 7月 19 15:20 tmp
drwx-----x. 2 root root 50 7月 19 14:54 volumes
[root@lion docker]# docker load < nginx
Loaded image: nginx:latest
[root@lion docker]# docker load -i nginx
Loaded image: nginx:latest
docker tag nginx:latest 用户名/nginx:web #添加新的标签时必须在前面加上自己的dockerhub的username
docker login #登录公共仓库
Username:用户名
password:密码
docker push 用户名/nginx:web #上传镜像
[root@lion docker]# docker create -it nginx:latest /bin/bash
ddb9e7f308c902d4acc144b057699e69a33a9ac7808e2b4a23d37e74c5398c95
字段 | 作用 |
---|---|
-i | 让容器开启标准输入接受用户输入命令 |
-t | 让 Docker 分配一个伪终端 tty |
-it | 合起来实现和容器交互的作用,运行一个交互式会话 shell |
[root@lion docker]# docker ps -a #-a 选项可以显示所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 17 seconds ago Created condescending_jemison
[root@lion docker]# docker start ddb9e7f308c9
ddb9e7f308c9
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 10 seconds 80/tcp condescending_jemison
可以直接执行 docker run 命令, 等同于先执行 docker create 命令,再执行 docker start 命令
注意:容器是一个与其中运行的 shell 命令/进程共存亡的终端,命令/进程运行容器运行, 命令/进程结束容器退出
docker 容器默认会把容器内部第一个进程,也就是 pid=1 的程序作为docker容器是否正在运行的依据,如果docker容器中 pid = 1 的进程挂了,那么docker容器便会直接退出,也就是说Docker容器中必须有一个前台进程,否则认为容器已经挂掉
当利用 docker run 来创建容器时, Docker 在后台的标准运行过程是:
(1)检查本地是否存在指定的镜像。当镜像不存在时,会从公有仓库下载;
(2)利用镜像创建并启动一个容器;
(3)分配一个文件系统给容器,在只读的镜像层外面挂载一层可读写层;
(4)从宿主主机配置的网桥接口中桥接一个虚拟机接口到容器中;
(5)分配一个地址池中的 IP 地址给容器;
(6)执行用户指定的应用程序,执行完毕后容器被终止运行。
[root@lion docker]# docker run centos:7 /usr/bin/bash -c ls /
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b381924589ab centos:7 "/usr/bin/bash -c ls…" 21 seconds ago Exited (0) 20 seconds ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 9 minutes ago Up 5 minutes 80/tcp condescending_jemison
[root@lion docker]# docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello;done" #可以看出容器始终处于 UP,运行状态
b28970abe73d0f80d927ad9485aeffdcf691c0f13ba16937da9b84bcb5a7713b
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 9 seconds ago Up 8 seconds loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" About a minute ago Exited (0) About a minute ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 11 minutes ago Up 7 minutes 80/tcp condescending_jemison
[root@lion docker]# docker run -itd --name test1 centos:7 /bin/bash
95540c0e0fdf21f4124d195b1f17e664ef659454c2d6750f0a799e9fe4bf6bc9
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95540c0e0fdf centos:7 "/bin/bash" 2 minutes ago Up 2 minutes test1
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 4 minutes ago Exited (137) 12 seconds ago loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" 6 minutes ago Exited (0) 6 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 15 minutes ago Up 11 minutes 80/tcp condescending_jemison
[root@lion docker]# docker stop 95540c0e0fdf
95540c0e0fdf
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95540c0e0fdf centos:7 "/bin/bash" 3 minutes ago Exited (137) 3 seconds ago test1
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 5 minutes ago Exited (137) About a minute ago loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" 7 minutes ago Exited (0) 7 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 16 minutes ago Up 12 minutes 80/tcp condescending_jemison
[root@lion docker]# docker start ddb9e7f308c9 #进入容器前,确保容器正在运行
ddb9e7f308c9
[root@lion docker]# docker exec -it ddb9e7f308c9 /bin/bash
root@ddb9e7f308c9:/# ls
bin dev docker-entrypoint.sh home lib32 libx32 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib lib64 media opt root sbin sys usr
root@ddb9e7f308c9:/# exit #退出容器后,容器仍在运行
exit
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95540c0e0fdf centos:7 "/bin/bash" 11 minutes ago Up 5 minutes test1
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 13 minutes ago Exited (137) 8 minutes ago loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" 14 minutes ago Exited (0) 14 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 24 minutes ago Up 19 minutes 80/tcp condescending_jemison
[root@lion docker]# docker run -it centos:7 bash
[root@9c424938966a /]# exit
exit
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c424938966a centos:7 "bash" 38 seconds ago Exited (127) 4 seconds ago naughty_tu
95540c0e0fdf centos:7 "/bin/bash" 14 minutes ago Up 8 minutes test1
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 16 minutes ago Exited (137) 12 minutes ago loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" 18 minutes ago Exited (0) 18 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 23 minutes 80/tcp condescending_jemison
[root@lion docker]# echo abc123 > ~/test.txt
[root@lion docker]# docker cp ~/test.txt ddb9e7f308c9:/opt/
Successfully copied 2.05kB to ddb9e7f308c9:/opt/
[root@lion docker]# docker cp ddb9e7f308c9:/opt/test.txt ~/abc123.txt
Successfully copied 2.05kB to /root/abc123.txt
1)导出格式:docker export 容器ID/名称 > 文件名
[root@lion docker]# docker export ddb9e7f308c9 > centos7.tar
[root@lion docker]# docker export -o centos7.tar ddb9e7f308c9
2)导入格式:cat 文件名 | docker import – 镜像名称:标签
[root@lion docker]# cat centos7.tar | docker import - centos7:test
sha256:620fb5cb0758c30618e818b1db94327a76c97e40ae6eda603f0b9f34116caaad
[root@lion docker]# docker import centos7.tar -- centos7:test
sha256:0ead35dac113babe0ca49cd14d25d615c3bef23a10c7b77714245da5394cc220
[root@lion docker]# docker stop 95540c0e0fdf
95540c0e0fdf
[root@lion docker]# docker rm 95540c0e0fdf
95540c0e0fdf
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c424938966a centos:7 "bash" 32 minutes ago Exited (127) 31 minutes ago naughty_tu
b28970abe73d centos:7 "/usr/bin/bash -c 'w…" 48 minutes ago Up 41 seconds loving_solomon
b381924589ab centos:7 "/usr/bin/bash -c ls…" 49 minutes ago Exited (0) 49 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" 59 minutes ago Up 55 minutes 80/tcp condescending_jemison
[root@lion docker]# docker rm -f b28970abe73d
b28970abe73d
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c424938966a centos:7 "bash" 32 minutes ago Exited (127) 32 minutes ago naughty_tu
b381924589ab centos:7 "/usr/bin/bash -c ls…" 50 minutes ago Exited (0) 50 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" About an hour ago Up 55 minutes 80/tcp condescending_jemison
[root@lion docker]# docker ps -a | awk 'NR>=2{print "docker stop "$1}' | bash
9c424938966a
b381924589ab
ddb9e7f308c9
[root@lion docker]# docker ps -a | awk 'NR>=2{print $1}'| xargs docker stop
9c424938966a
b381924589ab
ddb9e7f308c9
[root@lion docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c424938966a centos:7 "bash" 34 minutes ago Exited (127) 33 minutes ago naughty_tu
b381924589ab centos:7 "/usr/bin/bash -c ls…" 51 minutes ago Exited (0) 51 minutes ago competent_borg
ddb9e7f308c9 nginx:latest "/docker-entrypoint.…" About an hour ago Exited (137) 4 seconds ago condescending_jemison
[root@lion docker]# docker ps -a | awk 'NR>=2{print "docker rm "$1}' | bash
[root@lion docker]# docker ps -a | awk 'NR>=2{print $1}'| xargs docker rm
[root@lion docker]# docker images | awk 'NR>=2{print "docker rmi "$3}' | bash
Untagged: centos7:test
Deleted: sha256:0ead35dac113babe0ca49cd14d25d615c3bef23a10c7b77714245da5394cc220
Deleted:
......
[root@lion docker]# docker images | grep none | awk '{print $3}' | xargs docker rmi
"docker rmi" requires at least 1 argument.
See 'docker rmi --help'.
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
[root@lion docker]# docker rm $(docker ps -a -q)
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers