帮助启动类命令
启动docker:systemctl start docker
停止docker:systemctl stop docker
重启docker:systemctl restart docker
查看docker:systemctl status docker
开启启动:systemctl enable docker
查看概要信息:docker info
查看帮助:docker --help
查看具体命令帮助:docker XXX --help
镜像命令
docker images
列出本机的镜像列表
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
ubuntu 12.04 5b117edd0b76 5 years ago 104MB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签版本号
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一个镜像仓库源可以有多个TAG版本,代表着这个仓库源的不同版本。我们使用REPOSITORY:TAG来定义不用的镜像。如果不指定镜像的标签版本号默认使用最新版本例如:ubuntu = ubuntu:latest
-a 列出本地所有镜像(含历史镜像) -q 只显示镜像ID
docker search
➜ ~ docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1735 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demonstr… 151
tutum/hello-world Image to test docker deployments. Has Apache… 88 [OK]
dockercloud/hello-world Hello World! 19 [OK]
crccheck/hello-world Hello World web server in under 2.5 MB 15 [OK]
arm32v7/hello-world Hello World! (an example of minimal Dockeriz… 3
ansibleplaybookbundle/hello-world-db-apb An APB which deploys a sample Hello World! a… 2 [OK]
ppc64le/hello-world Hello World! (an example of minimal Dockeriz… 2
rancher/hello-world 1
ansibleplaybookbundle/hello-world-apb An APB which deploys a sample Hello World! a… 1 [OK]
datawire/hello-world Hello World! Simple Hello World implementati… 1 [OK]
thomaspoignant/hello-world-rest-json This project is a REST hello-world API to bu… 1
freddiedevops/hello-world-spring-boot 0
strimzi/hello-world-consumer 0
koudaiii/hello-world 0
strimzi/hello-world-producer 0
tsepotesting123/hello-world 0
strimzi/hello-world-streams 0
okteto/hello-world 0
dandando/hello-world-dotnet 0
garystafford/hello-world Simple hello-world Spring Boot service for t… 0 [OK]
rsperling/hello-world3 0
kevindockercompany/hello-world 0
armswdev/c-hello-world Simple hello-world C program on Alpine Linux… 0
businessgeeks00/hello-world-nodejs 0
NAME:镜像名称
DESCRIPTION:镜像描述
STARS:镜像点赞数
OFFICIAL:是否官方镜像
AUTOMATED:是否自动构建
列出前五个 - - limit 5
➜ ~ docker search --limit 5 hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1735 [OK]
tutum/hello-world Image to test docker deployments. Has Apache… 88 [OK]
rancher/hello-world 1
okteto/hello-world 0
armswdev/c-hello-world Simple hello-world C program on Alpine Linux… 0
docker pull
镜像下载 docker pull name[:tag],不带tag默认为:latest
docker system df
查看docker中镜像、容器、数据卷 存储信息。
➜ ~ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 3 240.8MB 0B (0%)
Containers 5 1 64B 64B (100%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
docker rmi
镜像删除。使用方式 docker rmi 镜像名称orID
//普通删除
➜ ~ docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 35192b0a28d9 is using its referenced image feb5d9fea6a5
//强制删除
➜ ~ docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
//多个删除
docker rmi -f hello-world redis:6.0.8
支持值传递
docker rmi -f $( docker images -qa )
➜ ~ docker rmi -f $( docker images -qa )
Untagged: ubuntu:12.04
Untagged: ubuntu@sha256:18305429afa14ea462f810146ba44d4363ae76e4c8dfc38288cf73aa07485005
Deleted: sha256:5b117edd0b767986092e9f721ba2364951b0a271f53f1f41aff9dd1861c2d4fe
Error response from daemon: conflict: unable to delete 9b9cb95443b5 (cannot be forced) - image is being used by running container f0a3c7a04de8
容器命令
新建启动容器run
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
启动交互式容器(前台命令行)
option 说明(常用):有些是一个减号,有些是两个减号
--name = "容器新名字" 为容器指定一个名称;
-d 后台运行容器并返回容器ID,也即启动守护式容器(后台运行)
-i 以交互模式运行容器,通常与-t 同时使用;
-t 为容器重新分配一个伪输入终端,通常与-i同时使用;
// -itd 也就是启动交互式容器(前台命令行)
➜ ~ docker run -it ubuntu /bin/bash
root@7de897b9bcc8:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
==对于非后台运行的容器。命令行输入 exit的时候该容器停止!==
使用 docker run -d …… 启动容器后台运行,必须有个前台进程类似(top、tail),否则docker会自动退出。这是docker机制问题。即便是有些容器启动后有些服务以后台的形式运行例如nginx。那么没有前台进程运行也会被docker回收。
查看启动容器ps
查看正在运行的容器 docker ps [option]
option:-a 列出所有当前正在运行及历史运行过的容器
-l 查看最近创建的容器
-n 显示最近创建的n个容器
-q 静默模式,只显示容器编号
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ac10078401e ubuntu "bash" 12 minutes ago Up 12 minutes pedantic_einstein
➜ ~ docker run -it --name=huzd888 ubuntu
root@f79ddbc74183:/#
//另外开启一个命令行
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f79ddbc74183 ubuntu "bash" 6 seconds ago Up 5 seconds huzd888
0ac10078401e ubuntu "bash" 17 minutes ago Up 17 minutes pedantic_einstein
➜ ~
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f79ddbc74183 ubuntu "bash" 3 minutes ago Exited (0) 2 minutes ago huzd888
e8c069353bf6 ubuntu "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago nostalgic_moore
7de897b9bcc8 ubuntu "/bin/bash" 12 minutes ago Exited (0) 9 minutes ago confident_feistel
36c42fb6c11e ubuntu "bash" 20 minutes ago Exited (0) 20 minutes ago sharp_kilby
0ac10078401e ubuntu "bash" 21 minutes ago Up 21 minutes pedantic_einstein
7c05b21fab7d ubuntu "bash" 21 minutes ago Exited (0) 21 minutes ago awesome_sinoussi
1104997c959d ubuntu "-d" 22 minutes ago Created gifted_pascal
5ad0e7209de6 ubuntu "-dit" 26 minutes ago Created recursing_elion
64e7fbacf8c4 ubuntu "-dit --name=huzd/ub…" 29 minutes ago Created confident_booth
e0d37f677f51 ubuntu "--name=/huzd/hellow…" 45 minutes ago Created pensive_moore
6b349cad7256 feb5d9fea6a5 "/hello" 3 hours ago Exited (0) 3 hours ago vigorous_brattain
35192b0a28d9 feb5d9fea6a5 "/hello" 3 hours ago Exited (0) 3 hours ago angry_satoshi
f0a3c7a04de8 9b9cb95443b5 "/bin/bash" 9 hours ago Exited (0) 43 minutes ago huzd
4f155cce1c21 9b9cb95443b5 "/usr/bin/top -b" 9 hours ago Exited (0) 9 hours ago recursing_hellman
82afcf100e41 5b117edd0b76 "/bin/bash" 24 hours ago Exited (0) 24 hours ago optimistic_elbakyan
➜ ~ docker ps -qa
f79ddbc74183
e8c069353bf6
7de897b9bcc8
36c42fb6c11e
0ac10078401e
7c05b21fab7d
1104997c959d
5ad0e7209de6
64e7fbacf8c4
e0d37f677f51
6b349cad7256
35192b0a28d9
f0a3c7a04de8
4f155cce1c21
82afcf100e41
➜ ~ docker ps -n5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f79ddbc74183 ubuntu "bash" 6 minutes ago Exited (0) 5 minutes ago huzd888
e8c069353bf6 ubuntu "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago nostalgic_moore
7de897b9bcc8 ubuntu "/bin/bash" 15 minutes ago Exited (0) 12 minutes ago confident_feistel
36c42fb6c11e ubuntu "bash" 23 minutes ago Exited (0) 23 minutes ago sharp_kilby
0ac10078401e ubuntu "bash" 24 minutes ago Up 24 minutes pedantic_einstein
退出容器
- 输入exit退出。容器也停止
- 使用crtl + q + p 退出,但容器不停止
启动已停止容器
docker start 容器ID or 容器名
➜ ~ docker ps -n4
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" About a minute ago Up About a minute crazy_blackwell
043be07668d7 ubuntu "bash" 2 minutes ago Up 2 minutes trusting_galileo
f79ddbc74183 ubuntu "bash" 19 minutes ago Exited (0) 18 minutes ago huzd888
e8c069353bf6 ubuntu "/bin/bash" 24 minutes ago Exited (0) 24 minutes ago nostalgic_moore
➜ ~ docker start f79ddbc74183
f79ddbc74183
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 2 minutes ago Up 2 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 2 minutes ago Up 2 minutes trusting_galileo
f79ddbc74183 ubuntu "bash" 20 minutes ago Up 4 seconds huzd888
0ac10078401e ubuntu "bash" 37 minutes ago Up 37 minutes pedantic_einstein
停止容器
docker stop 容器ID or 容器名
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 2 minutes ago Up 2 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 2 minutes ago Up 2 minutes trusting_galileo
f79ddbc74183 ubuntu "bash" 20 minutes ago Up 4 seconds huzd888
0ac10078401e ubuntu "bash" 37 minutes ago Up 37 minutes pedantic_einstein
➜ ~
➜ ~ docker stop f79ddbc74183
f79ddbc74183
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 3 minutes ago Up 3 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 3 minutes ago Up 3 minutes trusting_galileo
0ac10078401e ubuntu "bash" 39 minutes ago Up 39 minutes pedantic_einstein
强制停止容器
docker kill 容器ID or 容器名
option: -f 强制删除容器实例
➜ ~ docker kill 0ac10078401e
0ac10078401e
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 5 minutes ago Up 5 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 5 minutes ago Up 5 minutes trusting_galileo
删除已停止容器
docker rm 容器ID
➜ ~ docker ps -n10
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 6 minutes ago Up 6 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 7 minutes ago Up 7 minutes trusting_galileo
f79ddbc74183 ubuntu "bash" 24 minutes ago Exited (0) 3 minutes ago huzd888
e8c069353bf6 ubuntu "/bin/bash" 29 minutes ago Exited (0) 29 minutes ago nostalgic_moore
7de897b9bcc8 ubuntu "/bin/bash" 33 minutes ago Exited (0) 30 minutes ago confident_feistel
36c42fb6c11e ubuntu "bash" 42 minutes ago Exited (0) 42 minutes ago sharp_kilby
0ac10078401e ubuntu "bash" 42 minutes ago Exited (137) About a minute ago pedantic_einstein
7c05b21fab7d ubuntu "bash" 43 minutes ago Exited (0) 42 minutes ago awesome_sinoussi
1104997c959d ubuntu "-d" 43 minutes ago Created gifted_pascal
5ad0e7209de6 ubuntu "-dit" 47 minutes ago Created recursing_elion
➜ ~ docker rm e8c069353bf6
e8c069353bf6
➜ ~ docker ps -n10
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcbe48b54131 ubuntu "bash" 7 minutes ago Up 7 minutes crazy_blackwell
043be07668d7 ubuntu "bash" 7 minutes ago Up 7 minutes trusting_galileo
f79ddbc74183 ubuntu "bash" 25 minutes ago Exited (0) 3 minutes ago huzd888
7de897b9bcc8 ubuntu "/bin/bash" 33 minutes ago Exited (0) 30 minutes ago confident_feistel
36c42fb6c11e ubuntu "bash" 42 minutes ago Exited (0) 42 minutes ago sharp_kilby
0ac10078401e ubuntu "bash" 42 minutes ago Exited (137) About a minute ago pedantic_einstein
7c05b21fab7d ubuntu "bash" 43 minutes ago Exited (0) 43 minutes ago awesome_sinoussi
1104997c959d ubuntu "-d" 43 minutes ago Created gifted_pascal
5ad0e7209de6 ubuntu "-dit" 48 minutes ago Created recursing_elion
64e7fbacf8c4 ubuntu "-dit --name=huzd/ub…" 51 minutes ago Created confident_booth
多个同时删除
docker rm -f ${docker ps -a -q }
docker ps -a -q | xargs docker rm
➜ ~ docker rm 7de897b9bcc8 36c42fb6c11e 0ac10078401e
7de897b9bcc8
36c42fb6c11e
0ac10078401e
top
docker top [OPTIONS] CONTAINER [ps OPTIONS]
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7caa76b4c6b0 ubuntu "/bin/bash" 2 hours ago Up 2 hours upbeat_brahmagupta
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
➜ ~ docker top 7caa76b4c6b0
UID PID PPID C STIME TTY TIME CMD
root 4038 4013 0 09:25 ? 00:00:00 /bin/bash
inspect
查看容器元数据 docker inspect [OPTIONS] NAME|ID [NAME|ID...]
➜ ~ docker top 7caa76b4c6b0
UID PID PPID C STIME TTY TIME CMD
root 4038 4013 0 09:25 ? 00:00:00 /bin/bash
➜ ~ docker inspect 7caa76b4c6b0
[
{
"Id": "7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45",
"Created": "2022-06-16T09:23:22.814058984Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4038,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-06-16T09:25:48.888285864Z",
"FinishedAt": "2022-06-16T09:24:14.041582692Z"
},
"Image": "sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1",
"ResolvConfPath": "/var/lib/docker/containers/7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45/hostname",
"HostsPath": "/var/lib/docker/containers/7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45/hosts",
"LogPath": "/var/lib/docker/containers/7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45/7caa76b4c6b0f5584406726c2d70fa4b3303a1188bddb69c2795e77672e4fc45-json.log",
"Name": "/upbeat_brahmagupta",
"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": "private",
"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": null,
"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/1a2704e8c28a58a8f8b48d0a7bea1019da14b6d2b4f4f51b8fbdfc0a5733d11b-init/diff:/var/lib/docker/overlay2/fb67f2bd971c8751b01604a16f638b1da3ff710c651d2788e36df92ca539e5fb/diff",
"MergedDir": "/var/lib/docker/overlay2/1a2704e8c28a58a8f8b48d0a7bea1019da14b6d2b4f4f51b8fbdfc0a5733d11b/merged",
"UpperDir": "/var/lib/docker/overlay2/1a2704e8c28a58a8f8b48d0a7bea1019da14b6d2b4f4f51b8fbdfc0a5733d11b/diff",
"WorkDir": "/var/lib/docker/overlay2/1a2704e8c28a58a8f8b48d0a7bea1019da14b6d2b4f4f51b8fbdfc0a5733d11b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "7caa76b4c6b0",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "ubuntu",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "2a8f54adc3a9d49beacec2f4f6f1874f0e48f04103754cf8b6c6fb62675e469a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2a8f54adc3a9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5d16f483c8524a1644890270e534edc88535e7a5daa8a03d52db79d7023357c4",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "51c177158cd97cfaacac1283770e47c2edaaf5498f4e42e6fbe4be0701cd6366",
"EndpointID": "5d16f483c8524a1644890270e534edc88535e7a5daa8a03d52db79d7023357c4",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
➜ ~
exec
**docker exec :**在运行的容器中执行命令
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
如果在停止的容器中运行命令会报错。例如:
➜ ~ docker exec -it geoserver /bin/bash
Error response from daemon: Container 20facd76b57b62bde8ef6cc41eead939e8a4906272d36ef78dc7048388b41f98 is not running
正在运行的环境执行:
➜ ~ docker ps # 查看正在运行的docker容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7caa76b4c6b0 ubuntu "/bin/bash" 2 hours ago Up 2 hours upbeat_brahmagupta
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
➜ ~ docker exec -it cbb /bin/bash #通过docker exec 进入正在执行的命令
root@cbb8f18af774:/# pwd
/
root@cbb8f18af774:/# exit #退出当前容器
exit
➜ ~ docker ps #容器依然在后台运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7caa76b4c6b0 ubuntu "/bin/bash" 2 hours ago Up 2 hours upbeat_brahmagupta
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
attach
**docker attach :**连接到正在运行中的容器。
docker attach [OPTIONS] CONTAINER
bash-3.2$ docker ps #查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7caa76b4c6b0 ubuntu "/bin/bash" 2 hours ago Up 2 hours upbeat_brahmagupta
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
bash-3.2$ docker attach 7ca #进入正在运行的容器
root@7caa76b4c6b0:/# ls #查看命令
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@7caa76b4c6b0:/# exit #容器中输入exit退出命令
exit
bash-3.2$ docker ps #查看容器进程发现 7caa76b4c6b0 已经退出。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
exec 和 attach 的区别
attach 直接进入容器已启动的命令终端,不会启动新的进程用exit会退出,会导致容器的停止。
exec 是在容器中打开新的终端,并且可以启动新的进程,exit退出不会导致容器的停止。
cp文件拷贝
**docker cp :**用于容器与主机之间的数据拷贝。
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- 容器拷贝到主机
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH 主机拷贝到容器
➜ Desktop docker cp /Users/huzd/Desktop/test.json f0c93ffc94d5:/
➜ Desktop docker cp /Users/huzd/Desktop/test.json f0c93ffc94d5:/cc.json
➜ Desktop docker cp f0c93ffc94d5:/a.txt /Users/huzd/Desktop
➜ Desktop ls
a.txt
test.json
➜ Desktop docker cp f0c93ffc94d5:/a.txt /Users/huzd/Desktop/huzd.txt
➜ Desktop ls
a.txt
huzd.txt
test.json
➜ Desktop
import 导入容器
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
➜ Desktop docker ps # 查看当前正在运行的镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f0c93ffc94d5 redis "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 6379/tcp testredis
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
➜ Desktop docker rm -f f0c93ffc94d5 #强制删除正在运行的容器
f0c93ffc94d5
➜ Desktop docker ps #查看后台运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbb8f18af774 ubuntu "tail -f /dev/null" 2 hours ago Up 2 hours beautiful_cohen
➜ Desktop cat redis_docker_bak.tar| docker import - huzd/iredis:1.1.1 #导入我们备份的容器作为一个新的镜像
sha256:b292881e118dd94d5ae6e8c7942284d812293b41a23b4d880f86026dc6a63117
➜ Desktop docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
huzd/iredis 1.1.1 b292881e118d 4 seconds ago 109MB #我们基于当初运行的容器导出的镜像文件。
docker/disk-usage-extension 0.2.4 8046bf511714 25 hours ago 2.97MB
kartoza/geoserver 2.20.4 2c8bec02cf4e 2 months ago 1.43GB
nginx latest 605c77e624dd 5 months ago 141MB
redis latest 7614ae9453d1 5 months ago 113MB
ubuntu latest ba6acccedd29 8 months ago 72.8MB
hello-world latest feb5d9fea6a5 8 months ago 13.3kB
➜ Desktop docker run -it b292881e118d /bin/bash #基于我们导入的进行运行一个新的容器
root@3c1f7895a3ca:/# ls #查看我们之前的文件是否存在。发现全部存在。
a.txt bin boot cc.json data dev etc home lib lib64 media mnt opt proc root run sbin srv sys test.json tmp usr var
root@3c1f7895a3ca:/#
export 导出容器
将容器导出为一个压缩文件。
➜ Desktop docker export f0c93ffc94d5 > redis_docker_bak.tar
➜ Desktop ls
redis_docker_bak.tar
小总结
本文由 huzd 创作,采用 知识共享署名4.0 国际许可协议进行许可本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名最后编辑时间
为:
2022/06/30 22:30