# Docker常用命令
帮助命令
docker version #显示docker的版本信息 docker info #显示docker的系统信息,包括镜像和容器的信息 docker 命令 --help #万能命令#
帮助文档的地址:Reference documentation | Docker Documentation
镜像命令
docker images 查看所有本地主机上的镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 8 weeks ago 13.3kB centos latest 5d0da3dc9764 2 months ago 231MB #解释 REPOSITORY 镜像的仓库源 TAG 镜像的标签 IMAGE ID 镜像的ID CREATED 镜像的创建时间 SIZE 镜像的大小 #可选项 [root@localhost ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all #列出所有的镜像 Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet #只显示镜像的id Only show image IDs
docker search 搜索镜像
[root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11707 [OK] mariadb MariaDB Server is a high performing open sou… 4458 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 870 [OK] #可选项 通过搜藏来过滤 --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的 [root@localhost ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11707 [OK] mariadb MariaDB Server is a high performing open sou… 4458 [OK]
docker pull 下载镜像
#下载镜像 docker pull 镜像名[:tag] [root@localhost ~]# docker pull mysql Using default tag: latest #如果不写tag,默认就是latest,默认是下载最新版本 latest: Pulling from library/mysql a10c77af2613: Pull complete #分层下载,docker image的核心,联合文件系统 b76a7eb51ffd: Pull complete 258223f927e4: Pull complete 2d2c75386df9: Pull complete 63e92e4046c9: Pull complete f5845c731544: Pull complete bd0401123a9b: Pull complete 3ef07ec35f1a: Pull complete c93a31315089: Pull complete 3349ed800d44: Pull complete 6d01857ca4c1: Pull complete 4cc13890eda8: Pull complete Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #真实地址 docker pull mysql #等价于他 docker.io/library/mysql:latest #指定版本下载 [root@localhost ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql a10c77af2613: Already exists b76a7eb51ffd: Already exists 258223f927e4: Already exists 2d2c75386df9: Already exists 63e92e4046c9: Already exists f5845c731544: Already exists bd0401123a9b: Already exists 2724b2da64fd: Pull complete d10a7e9e325c: Pull complete 1c5fd9c3683d: Pull complete 2e35f83a12e9: Pull complete Digest: sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
查看镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 8b43c6af2ad0 3 days ago 448MB mysql latest b05128b000dd 3 days ago 516MB hello-world latest feb5d9fea6a5 8 weeks ago 13.3kB centos latest 5d0da3dc9764 2 months ago 231MB
docker rmi 删除镜像
通过ID来删除镜像 docker rmi -f b05128b000dd
[root@localhost ~]# docker rmi -f b05128b000dd Untagged: mysql:latest Untagged: mysql@sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b Deleted: sha256:b05128b000ddbafb0a0d2713086c6a1cc23280dee3529d37f03c98c97c8cf1ed Deleted: sha256:2920230e18d6833c32c9f851905df9d3e2958a43b771c84908234ac031b25a45 Deleted: sha256:a790dd6a368bc9aa7d1b251b46ac2fc718ebae5a38ed51ff89ff99955dadaa35 Deleted: sha256:cd87c1db4b159f37f092e73a52c10d5ccb837ed7bfcdc3b008038540390454a4 Deleted: sha256:7f92300b04af4aef96d5ef6fab1e27456cef354eca04733d396ad74478bee7d8 Deleted: sha256:6a59f55eb4945598b4889ea269d79f8b99563c96e97ba2373e19712732d20352
通过文件名来删除镜像 docker rmi -f hello-world
[root@localhost ~]# docker rmi -f hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 8b43c6af2ad0 3 days ago 448MB centos latest 5d0da3dc9764 2 months ago 231MB
递归删除所有的镜像 docker rmi -f $(docker images -aq)
[root@localhost ~]# docker rmi -f $(docker images -aq) Untagged: mysql:5.7 Untagged: mysql@sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29 Deleted: sha256:8b43c6af2ad08d95cdcb415d245446909a6cbc1875604c48c4325972e5b00442 Deleted: sha256:aad43f4d2f66438acd2d156216cd544a728851238714975c38d9a690f68afc57 Deleted: sha256:7b9addbc002c1e828aee7ec5c2679b04a591b6fa2b96002701ddee9d4ed54395 Deleted: sha256:b00f8e4e6ce8920fb563615503f232799ab380b338c3f2cbb5e86a2d762a6e80 Deleted: sha256:8fbabb17fd7b46a59cc15301741bf73a527b862f59cc6e84fae15b4dd5c425c0 Deleted: sha256:87030c256d8077b4d969e5819f5da01ed08f29e115eaec61b58b3f3134175e1e Deleted: sha256:b1694d0bb0b1be63e940478b93aa34f46e18f8371539ccee3b5d580cbf576812 Deleted: sha256:f323fd0baccb89f82a5711fa6291f3b4c977b85c3bbba59b1080205b498133b1 Deleted: sha256:47a2799e90faa9d9aaaa4b63457390dcbf5b26ce67f0926821c50b982d741e32 Deleted: sha256:156f55d34ef3e567ef39380f8d86f7c946927a099a43205de8721e60bfef526e Deleted: sha256:bb282bb84eb90a6040504a46f462ebe55cb9623df13219fc39f434a53ccd1687 Deleted: sha256:77b323d4ec74aad770337f99a60e862a64ccc53f4775b5f4945df0e606f78b90 Untagged: centos:latest Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image #参数说明 --name="Name" 容器名字 tomcat01 tomcat02 用来区分容器 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容 -P 指定容器的端口 -P 8080:8080 -p 主机端口:容器端口(常用) -p 容器端口 -p ip:主机端口:容器端口 -p 随机指定端口
测试,启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash [root@7ba14fb3b452 /]# ^C #查看容器内的centos [root@7ba14fb3b452 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@7ba14fb3b452 /]# #退出容器 [root@localhost ~]# docker run -it centos /bin/bash [root@7ba14fb3b452 /]# ^C [root@7ba14fb3b452 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@7ba14fb3b452 /]# exit #退出容器 #退出容器 exit [root@localhost ~]#
列出所有的运行的容器
[root@localhost ~]# docker ps #列出当前正在运行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker ps -a 列出当前正在运行的容器,顺便带出历史运行过的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7ba14fb3b452 centos "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago busy_wing 5f415bad2fb4 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago practical_chebyshev 4d30ccd944b1 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago infallible_edison dafca18369df centos "/bin/bash" 16 hours ago Exited (255) 2 hours ago peaceful_bartik ef2162727f75 centos "/bin/bash" 16 hours ago Exited (255) 2 hours ago frosty_darwin 45394faf426d centos "/bin/bash" 16 hours ago Exited (127) 16 hours ago inspiring_mclaren ac1c22a5dcbb feb5d9fea6a5 "/hello" 18 hours ago Exited (0) 18 hours ago awesome_swirles [root@localhost ~]# -q #只显示容器的编号 -n=? #显示最近创建的容器 [root@localhost ~]# docker ps -n=1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7ba14fb3b452 centos "/bin/bash" 15 minutes ago Exited (0) 12 minutes ago busy_wing
退出容器
exit #直接停止容器并退出 Ctrl + P + Q #容器不停止退出
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f docker rm -f $(docker ps -aq) #删除所有的容器 docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器 docker restart 容器id #重启容器 docker stop 容器id #停止当前正在运行的容器 docker kill 容器id #强制停止容器
常用其他命令
后台启动容器
#命令: docker run -d 镜像名字 [root@localhost ~]# docker run -d centos 02ffce41d9492a0a930bceea05cb4f289f03a7847751db9fbb9e9c437c670894 注意:问题 docker ps 发现centos 停止了 #常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止 #nginx ,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
#自己写一段shell脚本 docker run -d centos /bin/bash -c "while true;do echo [root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo wanggangqiang;sleep 1;done" adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES adc6bcbe6c85 centos "/bin/bash -c 'while…" 5 seconds ago Up 5 seconds beautiful_clarke #显示日志 -tf #显示全部日志 docker logs -tf adc6bcbe6c85 --tail number #显示number条日志 docker logs -tf --tail 10 adc6bcbe6c85 #查看10条日志 [root@localhost ~]# docker logs -tf --tail 10 adc6bcbe6c85 2021-11-21T07:47:31.136969700Z wanggangqiang 2021-11-21T07:47:32.143970714Z wanggangqiang 2021-11-21T07:47:33.149449041Z wanggangqiang 2021-11-21T07:47:34.151805544Z wanggangqiang 2021-11-21T07:47:35.156257345Z wanggangqiang 2021-11-21T07:47:36.161474343Z wanggangqiang 2021-11-21T07:47:37.164030237Z wanggangqiang 2021-11-21T07:47:38.165738586Z wanggangqiang 2021-11-21T07:47:39.168629319Z wanggangqiang 2021-11-21T07:47:40.175546455Z wanggangqiang 2021-11-21T07:47:41.179857347Z wanggangqiang 2021-11-21T07:47:42.182496293Z wanggangqiang 2021-11-21T07:47:43.185036941Z wanggangqiang 2021-11-21T07:47:44.187959688Z wanggangqiang 2021-11-21T07:47:45.189697176Z wanggangqiang 2021-11-21T07:47:46.193821052Z wanggangqiang 2021-11-21T07:47:47.195414864Z wanggangqiang 2021-11-21T07:47:48.198234472Z wanggangqiang 2021-11-21T07:47:49.200004085Z wanggangqiang 2021-11-21T07:47:50.207752734Z wanggangqiang 2021-11-21T07:47:51.212203442Z wanggangqiang 2021-11-21T07:47:52.216442517Z wanggangqiang
查看容器进程信息 命令:[root@localhost ~]# docker top 容器id
[root@localhost ~]# docker top adc6bcbe6c85 UID PID PPID C STIME TTY TIME CMD root 7192 7174 0 15:47 ? 00:00:00 /bin/bash -c while true;do echo wanggangqiang;sleep 1;done root 7949 7192 0 15:57 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器元数据 命令: docker inspect 容器id
[root@localhost ~]# docker inspect adc6bcbe6c85 [ { "Id": "adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee", "Created": "2021-11-21T07:47:08.434840037Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo wanggangqiang;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7192, "ExitCode": 0, "Error": "", "StartedAt": "2021-11-21T07:47:08.692611435Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee/resolv.conf", "HostnamePath": "/var/lib/docker/containers/adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee/hostname", "HostsPath": "/var/lib/docker/containers/adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee/hosts", "LogPath": "/var/lib/docker/containers/adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee/adc6bcbe6c858dc694aaf99f5770579254740b5f1c510257883e732b29939cee-json.log", "Name": "/beautiful_clarke", "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/ef56577b97d7c6d7aebb98c79f0c270c33e2308da71b79e50d04898fad3204c8-init/diff:/var/lib/docker/overlay2/4398044d4d814d9ec62eed8ede66f55622c2fe97bde633298875caf90031396c/diff", "MergedDir": "/var/lib/docker/overlay2/ef56577b97d7c6d7aebb98c79f0c270c33e2308da71b79e50d04898fad3204c8/merged", "UpperDir": "/var/lib/docker/overlay2/ef56577b97d7c6d7aebb98c79f0c270c33e2308da71b79e50d04898fad3204c8/diff", "WorkDir": "/var/lib/docker/overlay2/ef56577b97d7c6d7aebb98c79f0c270c33e2308da71b79e50d04898fad3204c8/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "adc6bcbe6c85", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash", "-c", "while true;do echo wanggangqiang;sleep 1;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "4a3e11cc442a83ac64f81ab8913c8853fc32b713015d31863fcdb7bb25165160", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/4a3e11cc442a", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "ca22c1585fafd94bea0930d555edf3b89a1d695cf3fd112db6c1f9a072dc1ab4", "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": "f0a522171fefc923c8c4c5729d902393e387598ffa73d159cdbe86925cfb9d8c", "EndpointID": "ca22c1585fafd94bea0930d555edf3b89a1d695cf3fd112db6c1f9a072dc1ab4", "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@localhost ~]#
进入当前正在运行的容器 docker exec -it 容器id bashShelld
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES adc6bcbe6c85 centos "/bin/bash -c 'while…" 17 minutes ago Up 17 minutes beautiful_clarke [root@localhost ~]# docker exec -it adc6bcbe6c85 /bin/bash [root@adc6bcbe6c85 /]# #方式二 [root@localhost ~]# docker attach adc6bcbe6c85 正在运行的代码。。。 #docker extc #进入容器后开启一个新的终端,可以在里面操作(常用) #docker attach #进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43995b0e52ef centos "/bin/bash" 13 seconds ago Up 12 seconds blissful_hypatia [root@localhost ~]# docker attach 43995b0e52ef #进入容器内部 [root@43995b0e52ef /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@43995b0e52ef /]# cd home [root@43995b0e52ef home]# ls [root@43995b0e52ef home]# ls [root@43995b0e52ef home]# touch test.java #创建test.java文件 [root@43995b0e52ef home]# ls test.java [root@43995b0e52ef home]# read escape sequence [root@localhost ~]# docker cp 43995b0e52ef:/home/test.java /home #复制容器内的文件到主机上 [root@localhost ~]# ls anaconda-ks.cfg dump.rdb initial-setup-ks.cfg [root@localhost ~]# cd / [root@localhost /]# ls 1 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost /]# cd home [root@localhost home]# ls mysql test.java wang [root@localhost home]#