docker(容器命令)
接上篇:docker(镜像命令)
1、docker run [OPTIONS] image [COMMAND] [ARG......]:新建+启动容器
OPTIONS参数说明:有些是一个减号,有些是两个减号;
--name=容器新名称 :为容器指定一个名称;
-d :后台运行容器并返回容器ID,即启动 守护式容器(后台运行);
-i :以交互式运行容器,通常与 -t 同时使用;
-t :为容器重新分配一个伪输入终端,通常与 -i 同时使用;
即启动交互式容器,前台有伪终端,等待交互;
-P :随机端口映射,大写P;
-p :指定端口映射,小写p;
1.1、启动交互式容器
[root@hadoop100 docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest ba6acccedd29 22 months ago 72.8MB hello-world latest feb5d9fea6a5 22 months ago 13.3kB redis 6.0.8 16ecd2772934 2 years ago 104MB -- -it:以交互式运行容器 -- ubuntu:latest:运行当前ubuntu最新版容器(最新版 latest可以省略不写,但不是最新版需要写版本号) -- /bin/bash:这里希望有一个交互式的shell,因此使用了终端 /bin/bash 也可以直接使用 bash 要退出终端使用命令 exit [root@hadoop100 docker]# docker run -it ubuntu:latest /bin/bash -- 当前使用交互式模式启动了容器,并且可以看到容器ID,linux内核命令可以执行 root@ecd5334aedef:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@ecd5334aedef:/# cd /etc/ root@ecd5334aedef:/etc# cd ~ root@ecd5334aedef:~# exit exit -- 在当前终端(2)下启动容器并指定一个名称 myu1 但提示容器名称已被使用 [root@hadoop100 docker]# docker run -it --name=myu1 ubuntu:latest bash docker: Error response from daemon: Conflict. The container name "/myu1" is already in use by container "8f264dccdbc7c6e7e5069121e40ca75b7ca07b6e8fc48ad39484a3aec4d633b8". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'. -- 在当前终端(2)下启动容器并指定一个名称 myu01 [root@hadoop100 docker]# docker run -it --name=myu01 ubuntu:latest bash root@ca990a3a0b7c:/# ls
1.2、启动守护式容器
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -- 启动容器,没有使用交互模式运行(后面没有使用bash也会默认使用bash脚本) [root@hadoop100 ~]# docker run -d ubuntu:latest e5bd768dd501e5e51908b66d15d7d2cf7648e49b567c6d1a434b1c6cd4336877 -- 容器启动成功,但是查询时没有,已经停止 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@hadoop100 ~]# docker run -it ubuntu:latest root@3ba77eab5b30:/# [root@hadoop100 ~]# [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ba77eab5b30 ubuntu:latest "bash" 11 seconds ago Up 11 seconds nice_chatelet [root@hadoop100 ~]#
2、docker ps [OPTIONS]:列出所有正在运行的容器
OPTIONS参数说明:
-a :列出当前所有正在运行的容器+历史上运行过的;
-l(这里是小写L) :显示最近创建的容器;
-n(后面接数字) :显示最近创建的n个容器;
-q :静默模式,只显示容器编号;
另起一个终端(2)执行 docker ps命令(上一个终端执行了命令 docker run -it ubuntu:latest /bin/bash 以交互式模式执行终端。因此处于交互状态下)
[root@hadoop100 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c8a73b76b6a ubuntu:latest "/bin/bash" 12 minutes ago Up 12 minutes nostalgic_borg -- 在当前终端(2)下启动容器并指定一个名称 myu1 但提示容器名称已被使用 [root@hadoop100 docker]# docker run -it --name=myu1 ubuntu:latest bash docker: Error response from daemon: Conflict. The container name "/myu1" is already in use by container "8f264dccdbc7c6e7e5069121e40ca75b7ca07b6e8fc48ad39484a3aec4d633b8". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'. -- 在当前终端(2)下启动容器并指定一个名称 myu01 [root@hadoop100 docker]# docker run -it --name=myu01 ubuntu:latest bash root@ca990a3a0b7c:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@ca990a3a0b7c:/#
CONTAINER ID :容器ID
IMAGE :镜像
COMMAND :命令
CREATED :创建时间
STATUS :状态
PORTS :端口号
NAMES :容器名称
另起一个终端(3)执行 docker ps命令
[root@hadoop100 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca990a3a0b7c ubuntu:latest "bash" 2 hours ago Up 2 hours myu01 3c8a73b76b6a ubuntu:latest "/bin/bash" 2 hours ago Up 2 hours nostalgic_borg [root@hadoop100 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca990a3a0b7c ubuntu:latest "bash" 2 hours ago Up 2 hours myu01 3c8a73b76b6a ubuntu:latest "/bin/bash" 2 hours ago Up 2 hours nostalgic_borg ecd5334aedef ubuntu:latest "/bin/bash" 3 hours ago Exited (0) 3 hours ago infallible_greider 8f264dccdbc7 ubuntu "bash" 5 weeks ago Exited (0) 5 weeks ago myu1 dd74fbb1ecf7 hello-world "/hello" 5 weeks ago Exited (0) 5 weeks ago sad_black 782d1109ec08 hello-world "/hello" 6 weeks ago Exited (0) 6 weeks ago strange_lichterman c5db7569cccb hello-world "/hello" 6 months ago Exited (0) 6 months ago epic_meitner [root@hadoop100 docker]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca990a3a0b7c ubuntu:latest "bash" 2 hours ago Up 2 hours myu01 [root@hadoop100 docker]# docker ps -n flag needs an argument: 'n' in -n See 'docker ps --help'. [root@hadoop100 docker]# docker ps -n 5 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca990a3a0b7c ubuntu:latest "bash" 2 hours ago Up 2 hours myu01 3c8a73b76b6a ubuntu:latest "/bin/bash" 2 hours ago Up 2 hours nostalgic_borg ecd5334aedef ubuntu:latest "/bin/bash" 3 hours ago Exited (0) 3 hours ago infallible_greider 8f264dccdbc7 ubuntu "bash" 5 weeks ago Exited (0) 5 weeks ago myu1 dd74fbb1ecf7 hello-world "/hello" 5 weeks ago Exited (0) 5 weeks ago sad_black [root@hadoop100 docker]# docker ps -q ca990a3a0b7c 3c8a73b76b6a [root@hadoop100 docker]# docker ps -n 5 -q ca990a3a0b7c 3c8a73b76b6a ecd5334aedef 8f264dccdbc7 dd74fbb1ecf7 [root@hadoop100 docker]#
3、退出容器
1、exit退出:run进去容器,exit退出,容器停止
2、ctrl+p+q退出:run进去容器,ctrl+p+q退出,容器不会停止
-- 当前有两个启动容器 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 5 minutes ago Up 5 minutes bold_payne 5c7d992ddfb7 ubuntu:latest "bash" 6 minutes ago Up 6 minutes dreamy_meitner [root@hadoop100 ~]# -- 适用exit退出其中一个容器 5c7d992ddfb7 [root@hadoop100 ~]# docker run -it ubuntu:latest bash root@5c7d992ddfb7:/# exit exit [root@hadoop100 ~]# -- 5c7d992ddfb7 容器退出了,只剩下一个容器 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 6 minutes ago Up 6 minutes bold_payne [root@hadoop100 ~]# -- 使用ctrl+p+q退出容器b7d6358a0bed [root@hadoop100 ~]# docker run -it ubuntu:latest bash root@b7d6358a0bed:/# [root@hadoop100 ~]# [root@hadoop100 ~]# -- b7d6358a0bed容器没有停止 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 8 minutes ago Up 8 minutes bold_payne [root@hadoop100 ~]#
4、启动已经停止运行的容器 docker start 容器ID/容器名称
5、重启容器 docker restart 容器ID/容器名称
6、停止容器 docker stop 容器ID/容器名称
7、强制停止容器 docker kill 容器ID/容器名称
-- 列举出当前正在运行的容器 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 14 minutes ago Up 14 minutes bold_payne 5c7d992ddfb7 ubuntu:latest "bash" 15 minutes ago Up About a minute dreamy_meitner -- 停止容器 b7d6358a0bed [root@hadoop100 ~]# docker stop b7d6358a0bed b7d6358a0bed [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c7d992ddfb7 ubuntu:latest "bash" 15 minutes ago Up About a minute dreamy_meitner -- 停止容器 dreamy_meitner [root@hadoop100 ~]# docker stop dreamy_meitner dreamy_meitner [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -- 启动已经停止的容器 dreamy_meitner [root@hadoop100 ~]# docker start dreamy_meitner dreamy_meitner -- 启动已经停止的容器 b7d6358a0bed [root@hadoop100 ~]# docker start b7d6358a0bed b7d6358a0bed [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 16 minutes ago Up 2 seconds bold_payne 5c7d992ddfb7 ubuntu:latest "bash" 17 minutes ago Up 12 seconds dreamy_meitner -- 重启容器 b7d6358a0bed [root@hadoop100 ~]# docker restart b7d6358a0bed b7d6358a0bed [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 17 minutes ago Up 2 seconds bold_payne 5c7d992ddfb7 ubuntu:latest "bash" 17 minutes ago Up 56 seconds dreamy_meitner -- 强制停止容器 b7d6358a0bed [root@hadoop100 ~]# docker kill b7d6358a0bed b7d6358a0bed [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c7d992ddfb7 ubuntu:latest "bash" 18 minutes ago Up About a minute dreamy_meitner [root@hadoop100 ~]
8、删除已经停止的容器
docker rm 容器ID/容器名称 :删除已经停止的容器
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7d6358a0bed ubuntu:latest "bash" 28 minutes ago Up 7 seconds bold_payne 5c7d992ddfb7 ubuntu:latest "bash" 29 minutes ago Up 12 minutes dreamy_meitner -- 删除容器 b7d6358a0bed (容器正在运行,无法删除) [root@hadoop100 ~]# docker rm b7d6358a0bed Error response from daemon: You cannot remove a running container b7d6358a0bedcfab46478b1b9f6cc3d98d29605461e18886e41385e377a5a0be. Stop the container before attempting removal or force remove -- 停止容器 b7d6358a0bed [root@hadoop100 ~]# docker stop b7d6358a0bed b7d6358a0bed -- 删除容器 [root@hadoop100 ~]# docker rm b7d6358a0bed b7d6358a0bed -- 启动容器(启动失败。容器已经被删除) [root@hadoop100 ~]# docker start b7d6358a0bed Error response from daemon: No such container: b7d6358a0bed Error: failed to start containers: b7d6358a0bed [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c7d992ddfb7 ubuntu:latest "bash" 32 minutes ago Up 15 minutes dreamy_meitner
docker rm -f 容器ID/容器名称 :强制删除容器
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c7d992ddfb7 ubuntu:latest "bash" 36 minutes ago Up 19 minutes dreamy_meitner [root@hadoop100 ~]# docker rm 5c7d992ddfb7 Error response from daemon: You cannot remove a running container 5c7d992ddfb789c7ab66d1a6d4ca8dc84a613d28b5d9530c45c46cf9ef8d17d8. Stop the container before attempting removal or force remove -- 强制删除 -f [root@hadoop100 ~]# docker rm -f 5c7d992ddfb7 5c7d992ddfb7 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker rm $(docker ps -a -q) :删除多个已经停止的容器
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 084d9e7b119a ubuntu:latest "bash" 7 seconds ago Up 5 seconds peaceful_stonebraker [root@hadoop100 ~]# docker ps -a -q 084d9e7b119a -- 正在运行的容器无法删除(必须使用 -f 强制删除) [root@hadoop100 ~]# docker rm $(docker ps -a -q) Error response from daemon: You cannot remove a running container 084d9e7b119a41082fda09fefa8e914aede18fb45479bcf6945ce95f3df947fb. Stop the container before attempting removal or force remove
docker rm -f $(docker ps -a -q) :强制删除多个容器
-- 当前正在运行的容器 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -- 当前所有正在运行的容器+历史上运行过的容器 [root@hadoop100 ~]# docker ps -a -q 7e4ed81a28ff b322f1dad6da b776aaed0456 ca990a3a0b7c 3c8a73b76b6a ecd5334aedef 8f264dccdbc7 dd74fbb1ecf7 782d1109ec08 c5db7569cccb -- 强制删除当前所有正在运行的容器+历史上运行过的容器 [root@hadoop100 ~]# docker rm -f $(docker ps -a -q) 7e4ed81a28ff b322f1dad6da b776aaed0456 ca990a3a0b7c 3c8a73b76b6a ecd5334aedef 8f264dccdbc7 dd74fbb1ecf7 782d1109ec08 c5db7569cccb [root@hadoop100 ~]# docker ps -a -q [root@hadoop100 ~]#
docker ps -a -q | xargs docker rm
docker ps -a -q | xargs docker rm -f
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 78c6086f2b35 ubuntu:latest "/bin/bash" 22 seconds ago Up 21 seconds flamboyant_franklin 084d9e7b119a ubuntu:latest "bash" 5 minutes ago Up 5 minutes peaceful_stonebraker [root@hadoop100 ~]# docker ps -a -q 78c6086f2b35 084d9e7b119a [root@hadoop100 ~]# docker ps -a -q | xargs docker rm Error response from daemon: You cannot remove a running container 78c6086f2b3543ab77da853d6107b914da6da74097bf704a3328c91f07ebd678. Stop the container before attempting removal or force remove Error response from daemon: You cannot remove a running container 084d9e7b119a41082fda09fefa8e914aede18fb45479bcf6945ce95f3df947fb. Stop the container before attempting removal or force remove [root@hadoop100 ~]# docker ps -a -q | xargs docker rm -f 78c6086f2b35 084d9e7b119a [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@hadoop100 ~]# docker ps -a -q [root@hadoop100 ~]#
9、启动一个守护式容器:docker run -d 容器名称
-- 以后台守护式云校redis [root@hadoop100 ~]# docker run -d redis:6.0.8 66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb -- 守护式进程redis容器实例已经运行 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 6379/tcp awesome_brahmagupta [root@hadoop100 ~]#
10、查看容器日志:docker logs 容器ID
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 6379/tcp awesome_brahmagupta -- 查看容器日志 [root@hadoop100 ~]# docker logs 66449b253a1a 1:C 17 Oct 2023 02:34:11.698 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 17 Oct 2023 02:34:11.699 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 17 Oct 2023 02:34:11.700 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 17 Oct 2023 02:34:11.704 * Running mode=standalone, port=6379. 1:M 17 Oct 2023 02:34:11.705 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 17 Oct 2023 02:34:11.705 # Server initialized 1:M 17 Oct 2023 02:34:11.705 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 1:M 17 Oct 2023 02:34:11.705 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never'). 1:M 17 Oct 2023 02:34:11.706 * Ready to accept connections [root@hadoop100 ~]#
11、查看容器内运行的进程:docker top 容器ID
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 6379/tcp awesome_brahmagupta [root@hadoop100 ~]# docker top 66449b253a1a UID PID PPID C STIME TTY TIME CMD polkitd 3725 3706 0 10:34 ? 00:00:01 redis-server *:6379 [root@hadoop100 ~]#
12、查看容器内部细节:docker inspect 容器ID
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 6379/tcp awesome_brahmagupta -- 查看容器内部细节(是一个JSON串信息) [root@hadoop100 ~]# docker inspect 66449b253a1a [ { "Id": "66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb", "Created": "2023-10-17T02:34:11.117933502Z", "Path": "docker-entrypoint.sh", "Args": [ "redis-server" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 3725, "ExitCode": 0, "Error": "", "StartedAt": "2023-10-17T02:34:11.64502544Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:16ecd277293476392b71021cdd585c40ad68f4a7488752eede95928735e39df4", "ResolvConfPath": "/var/lib/docker/containers/66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb/resolv.conf", "HostnamePath": "/var/lib/docker/containers/66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb/hostname", "HostsPath": "/var/lib/docker/containers/66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb/hosts", "LogPath": "/var/lib/docker/containers/66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb/66449b253a1a4944104d8ba62f61c2e1f72db3c50e1eebf83115a555ecea9ddb-json.log", "Name": "/awesome_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": "host", "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": false, "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/d0651da6c8a143a1ed628171e4ff11d59d2f3f289af72411a9e6b5de1b5c1e3b-init/diff:/var/lib/docker/overlay2/fe8261a2a847e45dadc3eea12c58976830dc4e0586f511280dbe7c330bf36c06/diff:/var/lib/docker/overlay2/49fed9d1e8afd51ec740e45d6a4106924da6fe43dda9710e511decc5efc403a5/diff:/var/lib/docker/overlay2/58cee0e131beb5236c6851c5ce8abe4b2803d1fc8a5f10b7cc08fdd282b9a8f7/diff:/var/lib/docker/overlay2/bac3fc7887bc30b13113df88525aaa0bc2c2f5759457249ac7d7ef7608dfe0ad/diff:/var/lib/docker/overlay2/a205d9ee7e64fe27277322d1433626c6088c92bc86e388b376d0c05b61be7330/diff:/var/lib/docker/overlay2/8af9de9678923291514443f2f6cd0907d8a2a227eb62ae7bce959988d1dc6ffe/diff", "MergedDir": "/var/lib/docker/overlay2/d0651da6c8a143a1ed628171e4ff11d59d2f3f289af72411a9e6b5de1b5c1e3b/merged", "UpperDir": "/var/lib/docker/overlay2/d0651da6c8a143a1ed628171e4ff11d59d2f3f289af72411a9e6b5de1b5c1e3b/diff", "WorkDir": "/var/lib/docker/overlay2/d0651da6c8a143a1ed628171e4ff11d59d2f3f289af72411a9e6b5de1b5c1e3b/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "0371c7786661ce386bc9d5a4ae1632846ef6f06e48c76e53bb26cfdd3732817c", "Source": "/var/lib/docker/volumes/0371c7786661ce386bc9d5a4ae1632846ef6f06e48c76e53bb26cfdd3732817c/_data", "Destination": "/data", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "66449b253a1a", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "6379/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.12", "REDIS_VERSION=6.0.8", "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.8.tar.gz", "REDIS_DOWNLOAD_SHA=04fa1fddc39bd1aecb6739dd5dd73858a3515b427acd1e2947a66dadce868d68" ], "Cmd": [ "redis-server" ], "Image": "redis:6.0.8", "Volumes": { "/data": {} }, "WorkingDir": "/data", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "621bc5b2e1ce66dde1572ecbdef3fba17740ff61b2018e6963a1586524db8aec", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "6379/tcp": null }, "SandboxKey": "/var/run/docker/netns/621bc5b2e1ce", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "74e834c2f418d8a1fcc85ab3542833074450234bc05dfa6ec37f936d30ad30be", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "da9bec09ac81291be97d00627880be96ab6307eb992454aadb962842315b52c7", "EndpointID": "74e834c2f418d8a1fcc85ab3542833074450234bc05dfa6ec37f936d30ad30be", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@hadoop100 ~]#
13、进入正在运行的容器,并以命令行交互
命令1:docker exec -it 容器ID /bin/bash(docker exec -it 容器ID bash)
[root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 6379/tcp awesome_brahmagupta -- 以前台命令行方式运行ubuntu [root@hadoop100 ~]# docker run -it ubuntu /bin/bash -- 可以执行linux命令 root@dc8efe7427b0:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var -- ctrl+p+q退出,容器不会停止 root@dc8efe7427b0:/# read escape sequence -- 查看正在运行的容器 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 37 seconds ago Up 36 seconds silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 6379/tcp awesome_brahmagupta -- 查看exec命令帮助信息 [root@hadoop100 ~]# docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables --env-file list Read in a file of environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container -- 使用docker exec -it 容器ID bash命令重新进入正在运行的容器 [root@hadoop100 ~]# docker exec -it dc8efe7427b0 /bin/bash root@dc8efe7427b0:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@dc8efe7427b0:/#
命令2:docker attach 容器ID /bin/bash
[root@hadoop100 ~]# docker attach dc8efe7427b0 root@dc8efe7427b0:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@dc8efe7427b0:/# read escape sequence [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 16 minutes ago Up 16 minutes silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 6379/tcp awesome_brahmagupta [root@hadoop100 ~]# docker attach dc8efe7427b0 root@dc8efe7427b0:/# exit exit [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 6379/tcp awesome_brahmagupta
-- 当前正在运行的容器ubuntu [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 10 minutes ago Up 10 minutes silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 29 minutes ago Up 29 minutes 6379/tcp awesome_brahmagupta -- 使用exec命令重新进入容器 [root@hadoop100 ~]# docker exec -it dc8efe7427b0 bash root@dc8efe7427b0:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var -- 使用 ctrl+p+q退出。容器不会停止 root@dc8efe7427b0:/# read escape sequence [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 11 minutes ago Up 11 minutes silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 30 minutes ago Up 30 minutes 6379/tcp awesome_brahmagupta -- 使用exec命令重新进入容器 [root@hadoop100 ~]# docker exec -it dc8efe7427b0 bash -- 使用exit退出容器,容器应该停止 root@dc8efe7427b0:/# exit exit -- 由于是使用exec命令重新进入正在运行的容器,容器并没有停止 [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 11 minutes ago Up 11 minutes silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 30 minutes ago Up 30 minutes 6379/tcp awesome_brahmagupta -- 使用attach命令进入正在运行的容器 [root@hadoop100 ~]# docker attach dc8efe7427b0 root@dc8efe7427b0:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var -- -- 使用 ctrl+p+q退出。容器不会停止 root@dc8efe7427b0:/# read escape sequence [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc8efe7427b0 ubuntu "/bin/bash" 16 minutes ago Up 16 minutes silly_hawking 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 6379/tcp awesome_brahmagupta -- 使用attach命令进入正在运行的容器 [root@hadoop100 ~]# docker attach dc8efe7427b0 -- 使用exit命令退出容器,容器停止 root@dc8efe7427b0:/# exit exit [root@hadoop100 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66449b253a1a redis:6.0.8 "docker-entrypoint.s…" 35 minutes ago Up 35 minutes 6379/tcp awesome_brahmagupta
14、从容器拷贝文件到主机上
docker cp 容器ID:容器内路径 目的主机路径
-- 启动ubuntu容器 [root@hadoop100 /]# docker run -it ubuntu /bin/bash root@4ee329a6bec4:/# cd /tmp -- 在容器内 /tmp目录下创建空文件hello.txt root@4ee329a6bec4:/tmp# touch hello.txt root@4ee329a6bec4:/tmp# ls hello.txt -- ctrl+p+q退出容器,容器不会停止 root@4ee329a6bec4:/tmp# read escape sequence -- 当前容器正在运行 [root@hadoop100 /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee329a6bec4 ubuntu "/bin/bash" About a minute ago Up About a minute sleepy_robinson -- 使用cp命令将容器内的文件拷贝dao主机指定目录下 [root@hadoop100 /]# docker cp 4ee329a6bec4:/tmp/hello.txt /ithailin [root@hadoop100 /]# cd /ithailin [root@hadoop100 ithailin]# ll 总用量 0 -rw-r--r--. 1 root root 0 10月 17 11:33 hello.txt
15、导入和导出容器
export:导出容器的内容作为一个tar归档文件:docker export 容器ID > filename.tar
import:从tar归档文件中的内容创建一个新的文件系统再导入为镜像:cat filename.tar | docker import - 镜像用户/镜像名:镜像版本号
-- 当前正在运行的容器ubuntu [root@hadoop100 ithailin]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee329a6bec4 ubuntu "/bin/bash" 2 hours ago Up 2 hours sleepy_robinson -- 将当前容器导出成一个tar归档文件 [root@hadoop100 ithailin]# docker export 4ee329a6bec4 > abcd.tar [root@hadoop100 ithailin]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee329a6bec4 ubuntu "/bin/bash" 2 hours ago Up 2 hours sleepy_robinson -- 删除当前正在运行的容器(当前已经没有运行的容器了) [root@hadoop100 ithailin]# docker rm -f 4ee329a6bec4 4ee329a6bec4 [root@hadoop100 ithailin]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -- 当前目录下有刚才导出的tar归档文件 [root@hadoop100 ithailin]# ls abcd.tar hello.txt -- 将当前归档文件导入成镜像,版本号随便填 [root@hadoop100 ithailin]# cat abcd.tar | docker import - ithailin/ubuntu:2.0 sha256:a1c9845d93c41ffdfbf377483338a39baed19031f305fea3e70ac625fe1cd10a -- 当前生成了刚才导入的镜像 [root@hadoop100 ithailin]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ithailin/ubuntu 2.0 a1c9845d93c4 21 seconds ago 72.8MB ubuntu latest ba6acccedd29 2 years ago 72.8MB hello-world latest feb5d9fea6a5 2 years ago 13.3kB redis 6.0.8 16ecd2772934 2 years ago 104MB -- 使用当前镜像。运行一个容器实例 [root@hadoop100 ithailin]# docker run -it a1c9845d93c4 /bin/bash root@533b387280fc:/# cd /tmp -- 当前根据tar归档文件生成的镜像,再启动的容器,里面的hello.txt文件还在 root@533b387280fc:/tmp# ls hello.txt
16、常用命令