目录
3.1.4 查看镜像信息 :docker image inspect
1 docker 介绍
1.1 什么是docker?
Docker是管理容器的引擎,为应用打包、部署平台,而非单纯的虚拟化技术
它具有以下几个重要特点和优势:
1. 轻量级虚拟化
:Docker 容器相较于传统的虚拟机更加轻量和高效,能够快速启动和停止,节省系统资源。
例如,启动一个 Docker 容器可能只需要几秒钟,而启动一个虚拟机则可能需要几分钟。
2. 一致性
:确保应用程序在不同的环境中(如开发、测试、生产)具有一致的运行表现。
无论在本地还是云端,应用的运行环境都能保持相同,减少了因环境差异导致的问题。
3. 可移植性
:可以轻松地将 Docker 容器从一个平台迁移到另一个平台,无需担心依赖和环境配置的差
异。
比如,在本地开发的容器可以无缝部署到云服务器上。
4. 高效的资源利用:多个 Docker 容器可以共享主机的操作系统内核,从而更有效地利用系统资
源。
5. 易于部署和扩展:能够快速部署新的应用实例,并且可以根据需求轻松地进行水平扩展。
总之,Docker 极大地简化了应用程序的开发、部署和管理流程,提高了开发效率和运维的便利性。
它在现代软件开发和云计算领域得到了广泛的应用。
1.2 docker在企业中的应用场景
- 在企业中docker作为业务的最小载体而被广泛应用
- 通过docker企业可以更效率的部署应用并更节省资源
IaaS(Infrastructure as a Service),即基础设施即服务
PaaS 是( Platform as a Service )即指平台即服务SaaS ( Software as a Service )软件运营服务是
1.3 docker与虚拟化的对比
特性 | 虚拟机 | Docker 容器 |
---|---|---|
操作系统 | 宿主机上运行虚拟机 OS | 共享宿主机 OS |
存储 | 镜像较大(GB) | 镜像小(MB) |
性能 | 操作系统额外的 cpu、内存消耗 | 几乎无性能损耗 |
移植性 | 笨重、与虚拟化技术耦合度高 | 轻量、灵活迁移 |
隔离性 | 完全隔离 | 安全隔离 |
部署 | 慢、分钟级 | 快速、秒级 |
运行密度 | 一般几十个 | 单机支持上千容器 |
1.4 docker的优势
对于开发人员:Build once、Run anywhere。
对于运维人员:Configure once、Run anything
容器技术大大提升了IT人员的幸福指数!
1.5 容器工作方式
2 部署docker
部署第一个容器
2.1 配置软件仓库
官方站点:https://docs.docker.com/
由于一些原因导致官方站点不能直接访问,所以使用清华大学的镜像源
[root@rockynode-1 docker-page]# vim /etc/yum.repos.d/docker-ce.repo
[docker]
name=docker-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/rhel/9.4/x86_64/stable/
gpgcheck=0
[root@rockynode-1 docker-page]# yum makecache
2.2 docker 安装
[root@rockynode-1 ~]# yum install docker-ce
[root@rockynode-1 ~]# systemctl start docker.service
[root@rockynode-1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2.3 配置docker 镜像加速器
[root@rockynode-1 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://gy3ctkgm.mirror.aliyuncs.com"
]
}
2.4 启动服务
#编辑docker启动文件,设定其使用iptables的网络设定方式,默认使用nftables
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --iptables=true
]# systemctl enable --now docker
]# docker info
2.5 激活内核网络选项(centos7需要的操作)
#在rhel7中 需要
]# vim /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
]# sysctl --system
]# systemctl restart docker
3 Docker的基本操作
3.1 docker 的镜像管理
3.1.1 搜索镜像:docker search
[root@rockynode-1 dockerfile]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 20126 [OK]
nginx/nginx-quic-qns NGINX QUIC interop 1
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 94
nginx/nginx-ingress-operator NGINX Ingress Operator for NGINX and NGINX P… 2
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 43
nginx/unit This repository is retired, use the Docker o… 63
3.1.2 拉取镜像:docker pull
[root@rockynode-1 ~]# docker pull nginx:1.26-alpine
1.26-alpine: Pulling from library/nginx
c6a83fedfae6: Pull complete
e84a595b509f: Pull complete
49fbf0520b78: Pull complete
8e479110e8a8: Pull complete
ecc9b66af851: Pull complete
09732b09eef5: Pull complete
2a633cf0ba84: Pull complete
128ae6f0c7f5: Pull complete
Digest: sha256:d4d72ee8e6d028c5ad939454164d3645be2d38afb5c352277926a48e24abf4fa
Status: Downloaded newer image for nginx:1.26-alpine
docker.io/library/nginx:1.26-alpine
3.1.3 查看本地的镜像:docker images
alpine 版本: nginx 镜像的最小安装发型版本
3.1.4 查看镜像信息 :docker image inspect
[root@rockynode-1 ~]# docker image inspect nginx:1.26-alpine
[
{
"Id": "sha256:9703b2608a982bc40633a891858a59ab10fc1c96156056e888d5754fb427d6ed",
"RepoTags": [
"nginx:1.26-alpine"
],
"RepoDigests": [
"nginx@sha256:d4d72ee8e6d028c5ad939454164d3645be2d38afb5c352277926a48e24abf4fa"
],
"Parent": "",
"Comment": "buildkit.dockerfile.v0",
"Created": "2024-08-14T23:51:24Z",
"DockerVersion": "",
"Author": "",
"Config": {
"Hostname": "",
"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.26.2",
"PKG_RELEASE=1",
"DYNPKG_RELEASE=2",
"NJS_VERSION=0.8.5",
"NJS_RELEASE=1"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 43255493,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/a98984e990b4942e4b338854ebb11c66c9f43e0c0543bcf73ee063ebc784f8b1/diff:/var/lib/docker/overlay2/17e91ccae105f802683110be0d219cdbac4e04e13be6294a63b0fa19b0584966/diff:/var/lib/docker/overlay2/9f7a9117632e9a64241301f62cbf3d6dfe853b4bed2b7d11273a7b9b67f7445b/diff:/var/lib/docker/overlay2/0a50c51660edd2cb13954b23ff9c34c9c4605984d90d30804e6255c314b354d4/diff:/var/lib/docker/overlay2/d2b320a241233388287e2e0907c77caace0cb11a64d3616ba30f364fbb764555/diff:/var/lib/docker/overlay2/71fa88d3d2cbc068e34a35ae87c1acde2c759b148b5b964ed200e93dd5f781b6/diff:/var/lib/docker/overlay2/da17fc4ead7cf90a18c9824732d3e9ffacc9a9d07d05a9b1acc29bd77c13839e/diff",
"MergedDir": "/var/lib/docker/overlay2/4b71bfb93d3acc5b9ef6b252f793bb6c9995a695c0b961c7f8d12fa6fe78c26d/merged",
"UpperDir": "/var/lib/docker/overlay2/4b71bfb93d3acc5b9ef6b252f793bb6c9995a695c0b961c7f8d12fa6fe78c26d/diff",
"WorkDir": "/var/lib/docker/overlay2/4b71bfb93d3acc5b9ef6b252f793bb6c9995a695c0b961c7f8d12fa6fe78c26d/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:78561cef0761903dd2f7d09856150a6d4fb48967a8f113f3e33d79effbf59a07",
"sha256:56983a0546de1109188cf29c574481a59b137c2de453942c5a11abc2d7854d32",
"sha256:88b572fefcc60fc85f0ca66174d71a118800c402d3697c4dc6a90dd501730c18",
"sha256:9405d361c947aa1a6f4561acce59baeedfb5579c4610c361fd6915152c59e9a5",
"sha256:775da40d81efe5b3dad7d5d34eba42f975e393e487e5819c16458aa7006a13c5",
"sha256:d1e692cc0d19c3319c0552b598b1dba876db7fd6b987ffb275d2a97ae79692d3",
"sha256:06c72c2189d8932874b549a828b91e9306ece19bb97093fa45a0057518bb11aa",
"sha256:2d40545659420c3ec78f0a0cf1d743f388035e03ccd5b10745b5a190b4ae1cee"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
3.1.5 导出镜像:docker image save
[root@rockynode-1 ~]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 anaconda-ks.cfg script
[root@rockynode-1 ~]# docker image save nginx:latest -o nginx-latest.tar.gz
[root@rockynode-1 ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg script
模板 图片 下载 桌面 nginx-latest.tar.gz
3.1.6 删除镜像:docker rmi
[root@rockynode-1 ~]# docker rmi nginx:latest
3.1.7 实例:删除镜像重新导入
# 删除镜像
[root@rockynode-1 ~]# docker rmi nginx:latest
Untagged: nginx:latest
Deleted: sha256:5ef79149e0ec84a7a9f9284c3f91aa3c20608f8391f5445eabe92ef07dbda03c
Deleted: sha256:aa557aaf0b93b5b9af247beb198be89632444af2d52b37f2b67bdf2227194625
Deleted: sha256:85dee7d6fa4b70eaa6b43b5dede5255b6794fac35d2aa6a80f8590dad223c1a6
Deleted: sha256:9996f8429bfc2f1e10f561e17bdc0c66eb82e30088725fbdc634a774933ba066
Deleted: sha256:d861b1c34411369feaaaf4921018baa607e89f1c393b8ed950f676dd3349b9ff
Deleted: sha256:01b9716819d1d6863f886e1adcd8bb2bf936dcacb16e1750e873bfd8e1667014
Deleted: sha256:8d8c7099ecd8343e6700c1d348211ab159c0eda2a9d318c5cf600a8eb2588f5e
Deleted: sha256:9853575bc4f955c5892dd64187538a6cd02dba6968eba9201854876a7a257034
# 导入本地保存的镜像
[root@rockynode-1 ~]# docker load -i /root/nginx-latest.tar.gz
9853575bc4f9: Loading layer 77.83MB/77.83MB
72db5db515fd: Loading layer 114MB/114MB
8b87c0c66524: Loading layer 3.584kB/3.584kB
ec1a2ca4ac87: Loading layer 4.608kB/4.608kB
55e54df86207: Loading layer 2.56kB/2.56kB
f4f00eaedec7: Loading layer 5.12kB/5.12kB
5f0272c6e96d: Loading layer 7.168kB/7.168kB
Loaded image: nginx:latest
# 查看镜像是否本地拉取成功
[root@rockynode-1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.26-alpine 9703b2608a98 12 days ago 43.3MB
nginx latest 5ef79149e0ec 12 days ago 188MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
timinglee/game2048 latest 19299002fdbe 7 years ago 55.5MB
timinglee/mario latest 9a35a9e43e8c 8 years ago 198MB
使用 awk 保存所有镜像
首先用awk将现在镜像名与标签给打印出来
~]# docker images | awk 'BEGIN{FS=" "}{if (NR > 1) print $1 ":" $2}'
nginx:1.26-alpine
nginx:latest
busybox:latest
timinglee/game2048:latest
timinglee/mario:latest
~]# docker save `docker images | awk 'BEGIN{FS=" "}{if (NR > 1) print $1 ":" $2}'` -o /root/images.tar.gz
docker删除所有镜像
~]# docker rmi `docker images | awk 'BEGIN{FS=" "}{if (NR > 1) print $1 ":" $2}'`
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
恢复删除的镜像,本地保存的
[root@rockynode-1 ~]# docker load -i /root/images.tar.gz
011b303988d2: Loading layer 5.05MB/5.05MB
36e9226e74f8: Loading layer 51.46MB/51.46MB
192e9fad2abc: Loading layer 3.584kB/3.584kB
6d7504772167: Loading layer 4.608kB/4.608kB
88fca8ae768a: Loading layer 629.8kB/629.8kB
Loaded image: timinglee/game2048:latest
Loaded image: timinglee/mario:latest
78561cef0761: Loading layer 8.082MB/8.082MB
56983a0546de: Loading layer 4.447MB/4.447MB
88b572fefcc6: Loading layer 3.584kB/3.584kB
9405d361c947: Loading layer 4.608kB/4.608kB
775da40d81ef: Loading layer 2.56kB/2.56kB
d1e692cc0d19: Loading layer 5.12kB/5.12kB
06c72c2189d8: Loading layer 7.168kB/7.168kB
2d4054565942: Loading layer 32.09MB/32.09MB
Loaded image: nginx:1.26-alpine
9853575bc4f9: Loading layer 77.83MB/77.83MB
72db5db515fd: Loading layer 114MB/114MB
8b87c0c66524: Loading layer 3.584kB/3.584kB
ec1a2ca4ac87: Loading layer 4.608kB/4.608kB
55e54df86207: Loading layer 2.56kB/2.56kB
f4f00eaedec7: Loading layer 5.12kB/5.12kB
5f0272c6e96d: Loading layer 7.168kB/7.168kB
Loaded image: nginx:latest
d51af96cf93e: Loading layer 4.495MB/4.495MB
Loaded image: busybox:latest
[root@rockynode-1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.26-alpine 9703b2608a98 12 days ago 43.3MB
nginx latest 5ef79149e0ec 12 days ago 188MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
timinglee/game2048 latest 19299002fdbe 7 years ago 55.5MB
timinglee/mario latest 9a35a9e43e8c 8 years ago 198MB
3.1.8 实例:如何使用docker运行一些小型项目
运行docker
docker-page]# docker run -d --rm --name webserver -p 80:80 nginx
e681dae38196e04049034a7881ab3f53000fb4a54f6cf335233d366b7b8b9ab4
[root@rockynode-1 docker-page]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e681dae38196 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp webserver
删除容器
[root@rockynode-1 ~]# docker rm -f webserver
webserver
使用第二个镜像开启一个容器
[root@rockynode-1 ~]# docker run -d --rm --name game1 -p 80:80 timinglee/game2048
7cccfa818ef3fda7342b1a05e14b554e01b41f42c094c671085beb8563d6ee3e
[root@rockynode-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7cccfa818ef3 timinglee/game2048 "/bin/sh -c 'sed -i …" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp game1
实现效果
停止自动删除 因为运行时指定 了--rm,停止自动删除
[root@rockynode-1 ~]# docker stop game1
game1
运行第三个容器:马里奥
[root@rockynode-1 ~]# docker run --rm --name game2 -p 80:8080 timinglee/mario
[root@rockynode-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e381a2de686e timinglee/mario:latest "python3 -m http.ser…" 5 seconds ago Up 5 seconds 0.0.0.0:80->8080/tcp, :::80->8080/tcp game2
浏览器打开可以玩了
3.2 容器的常用操作
3.2.1 容器的启动与交互
[root@rockynode-1 dockerfile]# docker run -it --name test busybox:latest
# ctrl+pq 可以不停止容器退出
/ # [root@rockynode-1 dockerfile]#
3.2.2 查看容器信息
[root@rockynode-1 dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3df692c10553 busybox:latest "sh" 21 seconds ago Up 21 seconds test
[root@rockynode-1 dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3df692c10553 busybox:latest "sh" 28 seconds ago Up 27 seconds test
[root@rockynode-1 dockerfile]# docker exec -it test sh
[root@rockynode-1 docker-page]# docker load -i centos-7.tar.gz
174f56854903: Loading layer 211.7MB/211.7MB
Loaded image: centos:7
[root@rockynode-1 docker-page]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v1 acaead738246 About an hour ago 4.26MB
nginx 1.26-alpine 9703b2608a98 12 days ago 43.3MB
nginx latest 5ef79149e0ec 12 days ago 188MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
timinglee/game2048 latest 19299002fdbe 7 years ago 55.5MB
timinglee/mario latest 9a35a9e43e8c 8 years ago 198MB
[root@rockynode-1 docker-page]# docker run -it --name centos7 centos:7
[root@a1f0476a59ca /]# #进入到容器中,按<ctrl>+<d>退出并停止容器,
#按<ctrl>+<pq>退出但不停止容器
[root@rockynode-1 docker-page]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1f0476a59ca centos:7 "/bin/bash" 3 minutes ago Up 3 minutes centos7
# 重新进入容器
[root@rockynode-1 docker-page]# docker attach centos7
[root@a1f0476a59ca /]#
-d # 后台运行-i # 交互式运行-t # 打开一个终端--name # 指定容器名称-p # 端口映射 -p 80 : 8080 把容器 8080 端口映射到本机 80 端口--rm # 容器停止自动删除容器--network # 指定容器使用的网络
[root@rockynode-1 docker-page]# docker exec -i -t centos7 ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
# 查看当前运行容器
[root@rockynode-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1f0476a59ca centos:7 "/bin/bash" 10 minutes ago Up 4 minutes centos7
# 查看所有容器
[root@rockynode-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1f0476a59ca centos:7 "/bin/bash" 10 minutes ago Up 4 minutes centos7
# 查看容器详细信息
[root@rockynode-1 ~]# docker inspect centos7
[
{
"Id": "a1f0476a59cafbc809e21e82ddbdd1d751c90700f8def848d5592d6760ff2402",
"Created": "2024-08-27T11:53:58.074417126Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12006,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-08-27T11:59:38.632754486Z",
"FinishedAt": "2024-08-27T11:59:20.625915334Z"
},
################################# 省略 ##################################
3.2.3 停止和运行容器
[root@rockynode-1 ~] docker stop centos7 #停止容器
[root@rockynode-1 ~] docker kill centos7 #杀死容器,可以使用信号
[root@rockynode-1 ~] docker start centos7 #开启停止的容器
3.2.4 删除容器
[root@rockynode-1 ~]# docker run -it --name centos7_2 centos:7
[root@258651663ef8 /]# [root@rockynode-1 ~]#
[root@rockynode-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
258651663ef8 centos:7 "/bin/bash" 10 seconds ago Up 10 seconds centos7_2
a1f0476a59ca centos:7 "/bin/bash" 16 minutes ago Up 10 minutes centos7
[root@rockynode-1 ~]# docker stop centos7
centos7
[root@rockynode-1 ~]# docker stop centos7_2
centos7_2
[root@rockynode-1 ~] docker container prune -f # 删除所有停止的容器
Deleted Containers:
258651663ef8c66b76581b1a4e99b453990ee29d26eabae10f2b98caf6d491bd
a1f0476a59cafbc809e21e82ddbdd1d751c90700f8def848d5592d6760ff2402
Total reclaimed space: 24.09MB
[root@rockynode-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@rockynode-1 ~]#
3.2.4 容器内容提交
在平常我们不可能只是只保存容器中,那样对数据是及其不安全的,一般都是创建容器然后保存之后再生成新的生成镜像。
将一层可读变为两层可读 :
而保存生成镜像就需要用到 commit -m --> 就是为原有只读的镜像多添加了一层可写层并保存这一层可写层将他做成一个两层的可读的镜像
dockerfile]# docker run --name test -it busybox:latest
/ # exit
dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dockerfile]# docker start test
dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4eac8f46f01d busybox:latest "sh" 22 seconds ago Up 3 seconds test
dockerfile]# docker exec -it test sh
/ # touch shuyanfile
/ # ls
bin etc lib proc shuyanfile tmp var
dev home lib64 root sys usr
/ # exit
dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4eac8f46f01d busybox:latest "sh" About a minute ago Up 47 seconds test
dockerfile]# docker commit -m "add file shuyanfile" test busybox:v1
sha256:acaead738246c8108e4e88fdc2385966249e26dbc31e9f1402240877c40d3d5e
dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v1 acaead738246 19 seconds ago 4.26MB
nginx 1.26-alpine 9703b2608a98 12 days ago 43.3MB
nginx latest 5ef79149e0ec 12 days ago 188MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
timinglee/game2048 latest 19299002fdbe 7 years ago 55.5MB
timinglee/mario latest 9a35a9e43e8c 8 years ago 198MB
dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4eac8f46f01d busybox:latest "sh" 4 minutes ago Up 3 minutes test
# 删除由busybox:latest 只读层生成的可写容器
dockerfile]# docker rm -f test
test
dockerfile]# docker run -it --rm --name test busybox:v1
/ # ls
bin etc lib proc shuyanfile tmp var
dev home lib64 root sys usr
/ #
运行了一个新的 busybox:latest 容器,
并命名为 test,
同时使用了 -it 选项来开启一个交互式的 shell,
以及 --rm 选项来确保容器在退出后会被自动删除。
3.2.5 系统文件与容器文件交互
[root@rockynode-1 ~]# docker run -it --name=centos7 centos:7
[root@bf0dce166d86 /]# exit
exit
[root@rockynode-1 ~]# docker start centos7
centos7
[root@rockynode-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf0dce166d86 centos:7 "/bin/bash" 14 seconds ago Up 4 seconds centos7
[root@rockynode-1 ~]# docker exec -it centos7 ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@rockynode-1 ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg images.tar.gz script
模板 图片 下载 桌面 dockerfile nginx-latest.tar.gz
[root@rockynode-1 ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg images.tar.gz script
模板 图片 下载 桌面 dockerfile nginx-latest.tar.gz
[root@rockynode-1 ~] docker cp anaconda-ks.cfg centos7:/mnt #复制文件到容器中
Successfully copied 2.56kB to centos7:/mnt
[root@rockynode-1 ~] docker exec -it centos7 ls /mnt #查看容器是否收到
anaconda-ks.cfg
3.2.6 查询容器内部日志
[root@rockynode-1 ~]# docker logs nginx_2