【1】Docker的安装
注意:如果之前安装过docker其他版本,请删除干净。
docker-01 | 10.0.0.51 | 2G |
docker-02 | 10.0.0.52 | 2G |
docker-01
[root@docker-01 ~]# cp -rp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite ‘/etc/localtime’? y[root@docker-01 ~]# ntpdate ntp1.aliyun.com[root@docker-01 ~]# hwclock --systohc[root@docker-01 ~]# vim /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
[root@docker-01 ~]# wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
--2023-07-17 21:26:12-- http://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1919 (1.9K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/docker-ce.repo’100%[=========================================================>] 1,919 --.-K/s in 0s 2023-07-17 21:26:12 (10.2 MB/s) - ‘/etc/yum.repos.d/docker-ce.repo’ saved [1919/1919][root@docker-01 ~]# sed -i 's+donload.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo[root@docker-01 ~]# yum -y install docker-ce[root@docker-01 ~]# mkdir docker-ce
[root@docker-01 ~]# find /var/cache/yum/x86_64/7/ -type f -name "*.rpm" | xargs mv -t docker-ce/[root@docker-01 ~]# docker version
Client: Docker Engine - CommunityVersion: 24.0.4API version: 1.43Go version: go1.20.5Git commit: 3713ee1Built: Fri Jul 7 14:54:21 2023OS/Arch: linux/amd64Context: default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?[root@docker-01 ~]# systemctl start docker
[root@docker-01 ~]# systemctl enable docker[root@docker-01 ~]# tar -zcvf docker-ce.tar.gz docker-ce/
[root@docker-01 ~]# scp -rp docker-ce.tar.gz root@10.0.0.52:/root
The authenticity of host '10.0.0.52 (10.0.0.52)' can't be established.
ECDSA key fingerprint is 91:14:1f:5c:f9:69:6e:12:5f:fa:dd:61:bc:ef:3e:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.52' (ECDSA) to the list of known hosts.
root@10.0.0.52's password:
docker-ce.tar.gz 100% 116MB 116.5MB/s 00:01
docker-02
[root@docker-02 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 2523 100 2523 0 0 6100 0 --:--:-- --:--:-- --:--:-- 6108[root@docker-02 ~]# tar xf docker-ce.tar.gz
[root@docker-02 ~]# cd docker-ce/
[root@docker-02 docker-ce]# yum localinstall -y *.rpm[root@docker-02 docker-ce]# systemctl start docker
[root@docker-02 docker-ce]# systemctl enable docker[root@docker-02 docker-ce]# docker version
Client: Docker Engine - CommunityVersion: 24.0.4API version: 1.43Go version: go1.20.5Git commit: 3713ee1Built: Fri Jul 7 14:54:21 2023OS/Arch: linux/amd64Context: default
【2】docker常用命令
搜索镜像 - search
[root@docker-01 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 18762 [OK]
unit Official build of NGINX Unit: Universal Web … 6 [OK]
下载镜像 - pull
[root@docker-01 ~]# docker pull nginx: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
查看存在的镜像 - image ls
[root@docker-01 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB## 或使用 images
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB
删除镜像 - image rm
[root@docker-01 ~]# docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:08bc36ad52474e528cc1ea3426b5e3f4bad8a130318e3140d6cfe29c8892c7ef
Deleted: sha256:021283c8eb95be02b23db0de7f609d603553c6714785e7a673c6594a624ffbda
Deleted: sha256:a9de33035096cdf7bbaf7f3e1291701c0620d2a0e66152228abef35a79002876
Deleted: sha256:d66c35807d98c6f37bd2a14c6506a42d27a40fbdb564e233f7a78aafdc636c59
Deleted: sha256:a4c423818ed6dc12a545c349d0dc36a5695446448e07229e96c7235a126c2520
Deleted: sha256:c04094edc9df98c870e281f3b947a7782ca6d542d8715814ac06786466af3659
Deleted: sha256:c9c467815e8fe87d99f0f500495cf7f4f9096cf6c116ef2782e84bb17a4a5e06
Deleted: sha256:4645f26713fbea51190f5de52b88fbe27b42efd61c0dba87c81fa16df9a8f649
Deleted: sha256:24839d45ca455f36659219281e0f2304520b92347eb536ad5cc7b4dbb8163588[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE## 或使用 rmi
[root@docker-01 ~]# docker pull nginx:latest
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB
[root@docker-01 ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:08bc36ad52474e528cc1ea3426b5e3f4bad8a130318e3140d6cfe29c8892c7ef
Deleted: sha256:021283c8eb95be02b23db0de7f609d603553c6714785e7a673c6594a624ffbda
Deleted: sha256:a9de33035096cdf7bbaf7f3e1291701c0620d2a0e66152228abef35a79002876
Deleted: sha256:d66c35807d98c6f37bd2a14c6506a42d27a40fbdb564e233f7a78aafdc636c59
Deleted: sha256:a4c423818ed6dc12a545c349d0dc36a5695446448e07229e96c7235a126c2520
Deleted: sha256:c04094edc9df98c870e281f3b947a7782ca6d542d8715814ac06786466af3659
Deleted: sha256:c9c467815e8fe87d99f0f500495cf7f4f9096cf6c116ef2782e84bb17a4a5e06
Deleted: sha256:4645f26713fbea51190f5de52b88fbe27b42efd61c0dba87c81fa16df9a8f649
Deleted: sha256:24839d45ca455f36659219281e0f2304520b92347eb536ad5cc7b4dbb8163588
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
导出镜像 - save
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB
alpine latest c1aabb73d233 4 weeks ago 7.33MB
[root@docker-01 ~]# docker save nginx:latest -o docker_nginx_latest.tar.gz
[root@docker-01 ~]# ls
anaconda-ks.cfg docker_nginx_latest.tar.gz
将多个镜像导出到同一个tar包
[root@docker-01 ~]# docker save -o docker_nginx_latest_alpine_laster.tar.gz nginx:latest alpine:latest
[root@docker-01 ~]# ls
anaconda-ks.cfg docker_nginx_latest_alpine_laster.tar.gz
导入镜像 - load
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker-01 ~]# docker load -i docker_nginx_latest.tar.gz
24839d45ca45: Loading layer [==================================================>] 77.81MB/77.81MB
b821d93f6666: Loading layer [==================================================>] 113.2MB/113.2MB
1998c5cd2230: Loading layer [==================================================>] 3.584kB/3.584kB
f36897eea34d: Loading layer [==================================================>] 4.608kB/4.608kB
9fdfd12bc85b: Loading layer [==================================================>] 2.56kB/2.56kB
434c6a715c30: Loading layer [==================================================>] 5.12kB/5.12kB
3c9d04c9ebd5: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB[root@docker-01 ~]# docker load -i docker_nginx_latest_alpine_laster.tar.gz
Loaded image: nginx:latest
78a822fe2a2d: Loading layer [==================================================>] 7.622MB/7.622MB
Loaded image: alpine:latest
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB
alpine latest c1aabb73d233 4 weeks ago 7.33MB
导入镜像 - import (使用import导入的镜像,没有名称和tag)
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker-01 ~]# docker import docker_nginx_latest.tar.gz
sha256:3a1b69daa7117d4087d303a9b9f8264bd6ddd89fc0aa98de15279bc54fd6417f
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 3a1b69daa711 8 seconds ago 191MB
镜像打标签tag - tag
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 50e01e030252 7 seconds ago 191MB
[root@docker-01 ~]# docker tag 50e01e030252 nignx:latest
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nignx latest 50e01e030252 36 seconds ago 191MB
批量导入镜像
[root@docker-01 ~]# ls
anaconda-ks.cfg docker_alpine_latest.tar.gz docker_nginx_latest.tar.gz
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker-01 ~]# for i in `ls *.tar.gz` ; do docker load -i ${i} ; done
78a822fe2a2d: Loading layer [==================================================>] 7.622MB/7.622MB
Loaded image: alpine:latest
24839d45ca45: Loading layer [==================================================>] 77.81MB/77.81MB
b821d93f6666: Loading layer [==================================================>] 113.2MB/113.2MB
1998c5cd2230: Loading layer [==================================================>] 3.584kB/3.584kB
f36897eea34d: Loading layer [==================================================>] 4.608kB/4.608kB
9fdfd12bc85b: Loading layer [==================================================>] 2.56kB/2.56kB
434c6a715c30: Loading layer [==================================================>] 5.12kB/5.12kB
3c9d04c9ebd5: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 021283c8eb95 12 days ago 187MB
alpine latest c1aabb73d233 4 weeks ago 7.33MB
查看镜像属性信息 - inspect
[root@docker-01 ~]# docker image inspect nignx:latest
[{"Id": "sha256:50e01e0302529b75135cdb604390e902edf0d2d33b15a4aad38549364d8a783d","RepoTags": ["nignx:latest"],"RepoDigests": [],"Parent": "","Comment": "Imported from -","Created": "2023-07-17T14:00:39.736859118Z","Container": "","ContainerConfig": {"Hostname": "","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": null,"Cmd": null,"Image": "","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": null},"DockerVersion": "24.0.4","Author": "","Config": {"Hostname": "","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": null,"Cmd": null,"Image": "","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": null},"Architecture": "amd64","Os": "linux","Size": 191044723,"VirtualSize": 191044723,"GraphDriver": {"Data": {"MergedDir": "/var/lib/docker/overlay2/26712e0da7400f397726cf91ce493a57ddba45fd2888eef4c47a02cb09b3c611/merged","UpperDir": "/var/lib/docker/overlay2/26712e0da7400f397726cf91ce493a57ddba45fd2888eef4c47a02cb09b3c611/diff","WorkDir": "/var/lib/docker/overlay2/26712e0da7400f397726cf91ce493a57ddba45fd2888eef4c47a02cb09b3c611/work"},"Name": "overlay2"},"RootFS": {"Type": "layers","Layers": ["sha256:3535316b441652de429ba1b43116475d9096fe29db21febf279c6d34edf7a8f0"]},"Metadata": {"LastTagTime": "2023-07-17T22:01:13.237222336+08:00"}}
]
清理无名、无效的镜像 - prune
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 7a3926d49b5c 3 seconds ago 7.62MB
nignx latest 50e01e030252 2 minutes ago 191MB
[root@docker-01 ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:7a3926d49b5c85905e58e97413bb3867e8b4584b4d88a22392ae8d70de6ebd0a
deleted: sha256:827bcdd7ee4f04b14a30f473be61643971d64a7bacb639c1ad1f99d133598987Total reclaimed space: 7.625MB
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nignx latest 50e01e030252 2 minutes ago 191MB
批量删除镜像、删除没有容器使用的镜像 - system prune
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nignx latest 50e01e030252 3 minutes ago 191MB
alpine latest c1aabb73d233 4 weeks ago 7.33MB
[root@docker-01 ~]# docker system prune -a
WARNING! This will remove:- all stopped containers- all networks not used by at least one container- all images without at least one container associated to them- all build cacheAre you sure you want to continue? [y/N] y
Deleted Images:
untagged: nignx:latest
deleted: sha256:50e01e0302529b75135cdb604390e902edf0d2d33b15a4aad38549364d8a783d
deleted: sha256:3535316b441652de429ba1b43116475d9096fe29db21febf279c6d34edf7a8f0
untagged: alpine:latest
deleted: sha256:c1aabb73d2339c5ebaa3681de2e9d9c18d57485045a4e311d9f8004bec208d67
deleted: sha256:78a822fe2a2d2c84f3de4a403188c45f623017d6a4521d23047c9fbb0801794cTotal reclaimed space: 198.4MB
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker-01 ~]#
创建、启动容器 - run -dit
[root@docker-01 ~]# docker run -dit nginx:latest
a5ff63d7068da615129595535c0be909ee2055feb836e0584370e19a0fb7181d
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5ff63d7068d nginx:latest "/docker-entrypoint.…" 10 seconds ago Up 10 seconds 80/tcp romantic_shtern
[root@docker-01 ~]#
创建、启动并直接进入容器 - run -it (但是退出后,容器会死掉)
[root@docker-01 ~]# docker run -it alpine:latest
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # exit
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6e57a2ae517 alpine:latest "/bin/sh" 15 seconds ago Exited (0) 2 seconds ago suspicious_ishizaka
创建、启动容器让容器一直处于运行状态
[root@docker-01 ~]# docker run -dit alpine:latest
1f602a2a733ef393edfb32e4958d5169dd40e6f8000e746e96c0743934a2b8e3
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f602a2a733e alpine:latest "/bin/sh" 6 seconds ago Up 5 seconds hungry_chatelet[root@docker-01 ~]# docker exec -it 1f602a2a733e /bin/ash
/ # exit
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f602a2a733e alpine:latest "/bin/sh" 34 seconds ago Up 34 seconds hungry_chatelet
创建容器,但是不启动 - create
[root@docker-01 ~]# docker create alpine:latest
dff9b9819f1ea4d9a472552c5cccf950e8900e08925388f22b304cc0a4881844
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dff9b9819f1e alpine:latest "/bin/sh" 3 seconds ago Created trusting_liskov
创建容器,并指定容器名称 - name
[root@docker-01 ~]# docker run -dit --name test centos:latest
f2e7ddb5c9a3518d6145cd40c4f05813ee788ca33d36cdc2a155896e97d8f08c
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2e7ddb5c9a3 centos:latest "/bin/bash" 8 seconds ago Up 7 seconds test
docker服务重启后,自动拉起容器运行
[root@docker-01 ~]# docker run -dit --name test-01 --restart=unless-stopped nginx:latest
96ce404fc5aab56a290d0da1146b1c5222951d4c02e84e7ac97017527347f8c2
[root@docker-01 ~]# ----restart=unless-stopped :docker重启后,自动拉起(但是不包含在守护进程重启之前就停止的容器)
----restart=no : docker重启后, 不自动拉起容器
--restart=always :docker重启后, 总是拉起容器
--restart=on-failure:3 :docker服务不正常退出并重启后,自动拉起服务,若超过指定3次未拉起容器则放弃
创建容器,并指定容器最大使用内存 - memory
[root@docker-01 ~]# docker run -dit --name test-mem --memory 256m centos:latest
927a4400a7570dc04768219aac08188aca5412cd15b6eba91eb42a63c28f9e3b
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
927a4400a757 centos:latest "/bin/bash" 5 seconds ago Up 5 seconds test-mem[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
927a4400a757 test-mem 0.00% 536KiB / 256MiB 0.20% 648B / 0B 0B / 0B 1[root@docker-01 ~]# docker inspect test-mem | grep -i mem"Name": "/test-mem","Memory": 268435456,"CpusetMems": "","MemoryReservation": 0,"MemorySwap": 536870912,"MemorySwappiness": null,
容器配置swap - memory-swap
[root@docker-01 ~]# docker run -dit --name test --memory 300m --memory-swap 300m nginx:latest
f86590ceb190c3f4768841e00daa3498e8a52d03ba37acc2c5321f1c7a655ff4[root@docker-01 ~]# docker run -dit --name test-01 --memory 300m --memory-swap 300m centos:latest
ad33ab277600a5ecb0c6074d37a0cd9510113827e73bea8d032f2a09245a5cb1[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
ad33ab277600 test-01 0.00% 540KiB / 300MiB 0.18% 648B / 0B 0B / 0B 1
f86590ceb190 test 0.00% 6.344MiB / 300MiB 2.11% 648B / 0B 26.4MB / 0B 2[root@docker-01 ~]# docker inspect test | grep -i mem"Memory": 314572800,"CpusetMems": "","MemoryReservation": 0,"MemorySwap": 314572800,"MemorySwappiness": null,[root@docker-01 ~]# docker inspect test-01 | grep -i mem"Memory": 314572800,"CpusetMems": "","MemoryReservation": 0,"MemorySwap": 314572800,"MemorySwappiness": null,
设置容器不被kill
- 容器在运行中,如果内存使用超出最大限制后,docker会将容器杀死,但是容器中有些服务会很重要,不能被杀死
[root@docker-01 ~]# docker run -dit --name test --memory 256m --memory-swap 256m --no-healthcheck=false nginx:latest
0ea31b515ca8dbb3fcf4976647ec9761198d4f432be98f5ef4ce8bb417c4c4ac[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ea31b515ca8 nginx:latest "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 80/tcp test[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
0ea31b515ca8 test 0.00% 1.797MiB / 256MiB 0.70% 648B / 0B 0B / 0B 2
容器内存扩容
[root@docker-01 ~]# docker inspect test-01 | grep -i mem"Memory": 268435456,"CpusetMems": "","MemoryReservation": 0,"MemorySwap": 268435456,"MemorySwappiness": null,[root@docker-01 ~]# docker update --memory 512m --memory-swap 512m test-01
test-01[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
835765b53151 test-01 0.00% 1.801MiB / 512MiB 0.35% 648B / 0B 0B / 0B 2
[root@docker-01 ~]# docker inspect test-01 | grep -i mem"Memory": 536870912,"CpusetMems": "","MemoryReservation": 0,"MemorySwap": 536870912,"MemorySwappiness": null,
容器只可以在cpu0上运行
[root@docker-01 ~]# docker run -dit --name test --memory 256m --memory-swap 256m --cpuset-cpus 0 nginx:latest
5f4ea4c54fba032131d8df05d3f47d28d88054882ad174ab092166632d2467e2
[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5f4ea4c54fba test 0.00% 1.801MiB / 256MiB 0.70% 648B / 0B 0B / 0B 2
容器可以在cpu0-cpu1之间运行
[root@docker-01 ~]# docker run -dit --name test --memory 256m --memory-swap 256m --cpuset-cpus '0-1' nginx:latest
容器只可以在cpu0、cpu1运行
[root@docker-01 ~]# docker run -dit --name test-02 --memory 256m --memory-swap 256m --cpuset-cpus '0,1' nginx:latest
容器只能使用50%的CPU资源
[root@docker-01 ~]# docker run -dit --name test-01 --memory 256m --memory-swap 256m --cpuset-cpus 0 --cpu-shares 50 nginx:latest
835765b53151d23cadc688aee2d69e373cf7ab4fe1931600b35e0e55b2cb9695[root@docker-01 ~]# docker inspect test-01 | grep -i cpu"CpuShares": 50,"NanoCpus": 0,"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "0","CpusetMems": "","CpuCount": 0,"CpuPercent": 0,
容器重命名 - rename
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5ff63d7068d nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp romantic_shtern
[root@docker-01 ~]# docker rename a5ff63d7068d nginx
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5ff63d7068d nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp nginx
[root@docker-01 ~]#
进入容器、attach命令退出会容器停止 - attach
[root@docker-01 ~]# docker attach 4f2dd90101d7
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # exit
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2dd90101d7 alpine:latest "/bin/sh" 31 seconds ago Exited (0) 2 seconds ago practical_poincare
- 但是我们使用attach进入容器后,想退出但是不想容器死掉,使用 crtl + p + q,偷偷退出
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6e4f2cdbdb7 alpine:latest "/bin/sh" 8 seconds ago Up 7 seconds jovial_meninsky
[root@docker-01 ~]# docker attach d6e4f2cdbdb7
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # read escape sequence
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6e4f2cdbdb7 alpine:latest "/bin/sh" 27 seconds ago Up 27 seconds jovial_meninsky
[root@docker-01 ~]#
- 有意思的是:使用attach命令,不管是多少人进入这个容器,都是用的相同的终端下工作,所以说画面都是是同步的,执行的过程大家都能看到,当如果有人执行了exit,自己也会退出容器也会死掉
进入容器exec,退出容器不会停止 - exec
[root@docker-01 ~]# docker exec -it jovial_meninsky /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys test tmp usr var
/ # exit
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6e4f2cdbdb7 alpine:latest "/bin/sh" 6 minutes ago Up 20 seconds jovial_meninsky
那么使用exec进入的容器为什么退出时容器不会死掉?他和attach有什么区别?
- 1、exec 进入容器时需要指定方式/bin/sh,也就是说会在进入容器时生成新的终端并在此终端下作业,所以说不管多少人使用exec进入相同的容器,都不会是同一个终端,并不会影响其他人在容器中的作业
查看启动、运行中的容器 - ps
[root@docker-01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6e4f2cdbdb7 alpine:latest "/bin/sh" 14 minutes ago Up 22 seconds jovial_meninsky# 或使用 container ls
[root@docker-01 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6e4f2cdbdb7 alpine:latest "/bin/sh" 14 minutes ago Up 39 seconds jovial_meninsky
查看所有容器 - ps -a
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cc6e2bd2878 nginx:latest "/docker-entrypoint.…" 30 seconds ago Created boring_hoover
d6e4f2cdbdb7 alpine:latest "/bin/sh" 16 minutes ago Up About a minute jovial_meninsky
[root@docker-01 ~]#
查看最新创建的容器
[root@docker-01 ~]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cc6e2bd2878 nginx:latest "/docker-entrypoint.…" About a minute ago Created boring_hoover
删除容器 - rm(只能删除停止的容器)
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
546bc01de58e alpine:latest "/bin/sh" 2 minutes ago Up 6 seconds pedantic_morse
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 6 seconds 80/tcp musing_jackson
[root@docker-01 ~]# docker stop 546bc01de58e
546bc01de58e[root@docker-01 ~]# docker rm pedantic_morse
pedantic_morse
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 36 seconds 80/tcp musing_jackson
[root@docker-01 ~]#
强制删除正在运行中的容器 - f
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 46 seconds 80/tcp musing_jackson
[root@docker-01 ~]# docker rm -f musing_jackson
musing_jackson
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker-01 ~]#
批量删除容器 - rm(rm:只能删除停止的容器 ,-f :删除所有,包含运行中的容器)
[root@docker-01 ~]# docker rm -f $(docker ps -aq)
4cc6e2bd2878
d6e4f2cdbdb7[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
删除已经停止的容器
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
796a3ad06ca9 nginx:latest "/docker-entrypoint.…" 22 seconds ago Exited (0) 9 seconds ago test-01
cccbebdc927e nginx:latest "/docker-entrypoint.…" 27 seconds ago Up 26 seconds 80/tcp test-02
61b7b8736291 nginx:latest "/docker-entrypoint.…" 33 seconds ago Exited (0) 2 seconds ago test-04
d4ef395e0e5e nginx:latest "/docker-entrypoint.…" 36 seconds ago Up 36 seconds 80/tcp test-03[root@docker-01 ~]# docker ps -a | grep Exit | awk '{print $NF}' | xargs docker rm
test-01
test-04
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cccbebdc927e nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp test-02
d4ef395e0e5e nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp test-03
停止容器 - stop
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up About a minute 80/tcp musing_jackson
[root@docker-01 ~]# docker stop 4ff3ae3ab2b7
4ff3ae3ab2b7
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 5 minutes ago Exited (0) 4 seconds ago musing_jackson
[root@docker-01 ~]#
批量停止容器
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
546bc01de58e alpine:latest "/bin/sh" 3 seconds ago Up 3 seconds pedantic_morse
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 80/tcp musing_jackson
[root@docker-01 ~]# docker stop $(docker ps -aq)
546bc01de58e
4ff3ae3ab2b7
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
546bc01de58e alpine:latest "/bin/sh" 29 seconds ago Exited (137) 4 seconds ago pedantic_morse
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 34 seconds ago Exited (0) 14 seconds ago musing_jackson
[root@docker-01 ~]#
我们发现使用stop停止容器,过程会很慢,可以使用kill强制停止
[root@docker-01 ~]# docker kill $(docker ps -aq)
546bc01de58e
4ff3ae3ab2b7
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
546bc01de58e alpine:latest "/bin/sh" About a minute ago Exited (137) 3 seconds ago pedantic_morse
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 2 minutes ago Exited (137) 3 seconds ago musing_jackson
[root@docker-01 ~]#
重启容器 - restart
[root@docker-01 ~]# docker restart musing_jackson
musing_jackson
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ff3ae3ab2b7 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 2 seconds 80/tcp musing_jackson
[root@docker-01 ~]#
查看容器中使用的系统
[root@docker-01 ~]# docker run -dit alpine:latest
6f422cf9500c53f10f12119a2706b8faf33303947ec9aea8dbf6428c81e8c77d
[root@docker-01 ~]# docker exec trusting_engelbart cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.18.2
PRETTY_NAME="Alpine Linux v3.18"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
[root@docker-01 ~]#
将更改过的容器提交为一个新的镜像 - commit
[root@docker-01 ~]# docker exec -it cool_mendeleev /bin/bash
[root@cd3eb28ffbd0 /]# cat > /etc/yum.repos.d/nginx.repo << OK
> [nginx-stable]
> name=nginx stable repo
> baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
> gpgcheck=0
> enabled=1
> gpgkey=https://nginx.org/keys/nginx_signing.key
> module_hotfixes=true
> OK
[root@cd3eb28ffbd0 /]# yum -y install nginx
[root@a121db69daff /]# nginx
[root@cd3eb28ffbd0 /]# exit
exit[root@docker-01 ~]# docker commit cool_mendeleev nginx/centos:7
sha256:daa47d59f276ad84e8a0e09547f2958f62e7bf15431850f64d5e7d3bbdb6495d
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx/centos 7 daa47d59f276 3 seconds ago 458MB
nginx latest 021283c8eb95 12 days ago 187MB
alpine latest c1aabb73d233 4 weeks ago 7.33MB
centos 7 eeb6ee3f44bd 22 months ago 204MB
centos latest 5d0da3dc9764 22 months ago 231MB[root@docker-01 ~]# docker run -dit -p 80:80 nginx/centos:7
a121db69daff2260798d732c2ad37aa9c0cbb5a444e1c62d6aa03ec0950d3306
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a121db69daff nginx/centos:7 "/bin/bash" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp objective_ptolemy
cd3eb28ffbd0 centos:7 "/bin/bash" 3 minutes ago Up 3 minutes
在容器和宿主机中互相复制文件 - cp
[root@docker-01 ~]# touch test
[root@docker-01 ~]# mkdir dir[root@docker-01 ~]# docker cp test objective_ptolemy:/root
Successfully copied 1.54kB to objective_ptolemy:/root
[root@docker-01 ~]# docker cp dir objective_ptolemy:/root
Successfully copied 1.54kB to objective_ptolemy:/root[root@docker-01 ~]# docker exec -it a121db69daff /bin/bash
[root@a121db69daff /]# cd root/
[root@a121db69daff ~]# ls
anaconda-ks.cfg dir test
查看容器和镜像是否进行改变 - diff
[root@docker-01 ~]# docker diff objective_ptolemy
C /var
C /var/log
C /var/log/nginx
C /var/log/nginx/access.log
C /var/log/nginx/error.log
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
C /run
A /run/nginx.pid
C /root
A /root/test
A /root/dir
A /root/llll
C /root/.bash_history
监控容器 - events
[root@docker-01 ~]# docker events
2023-07-17T23:24:20.956090809+08:00 container exec_die a121db69daff2260798d732c2ad37aa9c0cbb5a444e1c62d6aa03ec0950d3306 (execID=c83af3df8e9971ef24f8edc266a21096a4e5e6c6aabb65efae559e653bb48716, exitCode=0, image=nginx/centos:7, name=objective_ptolemy, 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)
将容器进程的文件导出到本地 - export
[root@docker-01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a121db69daff nginx/centos:7 "/bin/bash" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp objective_ptolemy
cd3eb28ffbd0 centos:7 "/bin/bash" 15 minutes ago Up 15 minutes cool_mendeleev
[root@docker-01 ~]# docker export cd3eb28ffbd0 > test.tar
[root@docker-01 ~]# ls
anaconda-ks.cfg test.tar
将export导出的文件,创建一个镜像 - import
[root@docker-01 ~]# docker import test.tar centos:7.1
sha256:783d6a9fa04516bb4cb1c781413c916f55781576e53f6ef83a8b365c94c8b4de
[root@docker-01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.1 783d6a9fa045 3 seconds ago 431MB
查看容器的资源使用情况 - stats
[root@docker-01 ~]# docker stats a121db69daffCONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a121db69daff objective_ptolemy 0.00% 2.93MiB / 1.781GiB 0.16% 2.71kB / 2.29kB 3.96MB / 0B 3[root@docker-01 ~]# docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a121db69daff objective_ptolemy 0.00% 396KiB / 1GiB 0.04% 648B / 0B 0B / 0B 1
cd3eb28ffbd0 cool_mendeleev 0.00% 64.07MiB / 1.781GiB 3.51% 58.5MB / 233kB 16.3MB / 9.65MB 1
查看容器日志 - logs
[root@docker-01 ~]# docker logs cool_mendeleev
查看容器进程 - top
[root@docker-01 ~]# docker top objective_ptolemy
UID PID PPID C STIME TTY TIME CMD
root 21989 21968 0 23:17 ? 00:00:00 /bin/bash
root 22115 21968 0 23:18 ? 00:00:00 nginx: master process nginx
systemd+ 22116 22115 0 23:18 ? 00:00:00 nginx: worker process