docker vsersuib #显示docker 的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量。
docker 命令 --help #万能命令
帮助文档的地址: https://wwww.docker.com/reference/comandline/
镜像命令,
docker images 查询本地的主机上的镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的iD
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a --all #列出所有镜像
-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 Only show unmeric IDs #显示镜像的ID
docker search 搜索镜像
docker search mysql
[root@localhost /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11869 [OK]
mariadb MariaDB Server is a high performing open sou… 4536 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 888 [OK]
percona Percona Server is a fork of the MySQL relati… 566 [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… 11869 [OK]
mariadb MariaDB Server is a high performing open sou… 4536 [OK]
[root@localhost /]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11869 [OK]
mariadb MariaDB Server is a high performing open sou… 4536 [OK]
[root@localhost /]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11869 [OK]
docker pull 下载镜像
#下载镜像 docker mysql
[root@localhost /]# docker pull mysql
Using default tag: latest #如果不写tag ,默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载docker iamge
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真是地址
#下面两个命令一样
docker pull mysql
docker pull docker.io/library/mysql:latest
docker rmi 删除镜像,
[root@localhost /]# docker images 查询
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 days ago 448MB
mysql latest 3218b38490ce 5 days ago 516MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
[root@localhost /]# docker rmi c20987f18b13# 删除指定id的容器
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
[root@localhost /]# docker images #查看是否删除,
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 days ago 516MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
[root@localhost /]# docker rmi -f $(docker images -qa) 删除所有容器
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
容器命令
#有镜像才可以创建容器,linux下载一个centos
[root@localhost /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
新建容器并启动
#docker run [可选参数] image
#参数说明,
--name="Name" 容器名字,tomcat01 tomcat02 用来名区分容器
-d 后台运行
-it 使用交互方式进入容器查看内容
-P 指定容器的端口 -p 8080:8080
-p ip 主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
-p
测试进入容器
root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 3 months ago 231MB
[root@localhost /]# docker run -it centos /bin/bash
[root@dcf5f91d07d9 /]# ls 查看容器的版本
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@dcf5f91d07d9 /]# exit 从容器退出主机命令
exit
[root@localhost /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr www
列出所有的运行的容器,
#docker ps
-a #列出当前正在运行的容器+带出历史运行过的容器。
-n=?#显示最近创建的容器
-q #只显示,容器的编号。
[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
dcf5f91d07d9 centos "/bin/bash" 2 minutes ago Exited (0) About a minute ago nice_proskuriakova
1f97f8974016 feb5d9fea6a5 "/hello" 6 hours ago Exited (0) 6 hours ago eloquent_hermann
退出容器
exit #直接容器停止并退出
Ctrl—P+Q 容器不停止,退出
[root@localhost /]# docker run -it centos /bin/bash
[root@d3313f0a3aaa /]#
[root@d3313f0a3aaa /]# [root@localhost /]#
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3313f0a3aaa centos "/bin/bash" 31 seconds ago Up 26 seconds zealous_hodgkin
删除容器
docker rm 容器id s#删除指定的容器,不能删除正在运行的容器,如果强制删除,rm-f
docker rm -f $(docker ps -aq) #删除所有容器,
docker ps -a -qn | xargs docker rm #删除所有容器,
启动和停止容器的操作
docker start 容器id #启动,
docker restart 容器id #重启
docker stop 容器id #停止
docker kill 容器id #删除
常用的其他命令
#docker run -d
docker run -d centos
#docker ps,发现centos 停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程 docker 发现没有应用,就会自动停止
#nginx 容器启动后发现自己没有提供服务,就会立刻停止。就是没有程序了
查看日志命令
docker logs --help
docker logs -f -t --tail 容器没有日志
[root@localhost ~]# docker run -d centos /bin/sh -c "while true; do echo kuangshen;sleep 1;done"
46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3
#自己编写一条命令
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46c8d6ec64d8 centos "/bin/sh -c 'while t…" 15 seconds ago Up 11 seconds great_euclid
#显示日志
#-tf number #显示日志条数,
#docker logs -tf --tail 10 容器id
[root@localhost ~]# docker logs -tf --tail 10 46c8d6ec64d8
-f动态显示 跟踪日志输出 -t时间戳
查看容器中的进程信息
linux里 通过ps 查看进程信息
top
docker top 容器id
[root@localhost ~]# docker top 46c8d6ec64d8
UID PID PPID C STIME TTY TIME CMD
root 23162 23140 0 21:43 ? 00:00:00 /bin/sh -c while true; do echo kuangshen;sleep 1;done
root 23945 23162 0 21:50 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
#查看容器中的进程信息
查看进程的元数据
docker ps inspect 查看容器内部信息
[root@localhost ~]# docker inspect 46c8d6ec64d8
[
{ #id的前几位就是容器id
"Id": "46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3",
"Created": "2021-12-26T13:43:50.812743975Z", #创建时间
"Path": "/bin/sh", #控制台
"Args": [
"-c",
"while true; do echo kuangshen;sleep 1;done" #传递的参数
],
"State": {
"Status": "running", #运行参数
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23162,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-26T13:43:53.92232962Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},#下面是显示从哪个进程过来的
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3/hostname",
"HostsPath": "/var/lib/docker/containers/46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3/hosts",
"LogPath": "/var/lib/docker/containers/46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3/46c8d6ec64d835ba559e80dcee3beb8965b157ea8edbb2b348c52adf2ac60ab3-json.log",
"Name": "/great_euclid",
"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/0041d378482c221f99d515ea76a815ba753d057b67de8af5f4377c19d990ba6a-init/diff:/var/lib/docker/overlay2/496c93719225d03e3b5a66fc3e20aeba2978bf70e2991c5e3a25ba685db9de63/diff",
"MergedDir": "/var/lib/docker/overlay2/0041d378482c221f99d515ea76a815ba753d057b67de8af5f4377c19d990ba6a/merged",
"UpperDir": "/var/lib/docker/overlay2/0041d378482c221f99d515ea76a815ba753d057b67de8af5f4377c19d990ba6a/diff",
"WorkDir": "/var/lib/docker/overlay2/0041d378482c221f99d515ea76a815ba753d057b67de8af5f4377c19d990ba6a/work"
},
"Name": "overlay2"
},
"Mounts": [],#挂载
"Config": {
"Hostname": "46c8d6ec64d8", #默认的容器名字
"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/sh",
"-c",
"while true; do echo kuangshen;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": "5ede825d79872ccb78fd2937dd63d4ded52c2454efe855a28fa62c16d20f3e95",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/5ede825d7987",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "07321854e7f47b51c640aae7672ad3076f44d9ac406010094779999edf7d833e",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": { #桥接的网卡
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "a8aba8e5a420e4effbb8fa118dfb537e4322fbc2bcb2ba2032a6ae3f228a5a74",
"EndpointID": "07321854e7f47b51c640aae7672ad3076f44d9ac406010094779999edf7d833e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行容器的命令
docker
#通常我们都是使用后台方式运行的,需要进入容器修改一些配置
#命令
docker exec it 容器id bashShell1
[root@localhost ~]# docker ps查看
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46c8d6ec64d8 centos "/bin/sh -c 'while t…" 26 minutes ago Up 26 minutes great_euclid
988f85987835 centos "/bin/bash" 52 minutes ago Up 52 minutes awesome_banach
[root@localhost ~]# docker exec -it 46c8d6ec64d8 /bin/bash
[root@46c8d6ec64d8 /]# 成功进入 ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@46c8d6ec64d8 /]# ps -sf
UID PID PENDING BLOCKED IGNORED CAUGHT STAT TTY TIME COMMAND
0 1585 00000000 00010000 00380004 4b817efb Ss pts/0 0:00 /bin/bash
0 1648 00000000 00000000 00000000
#方式二
测试
docker attach 容器id
[root@localhost ~]# docker attach 46c8d6ec64d8
正在执行当前代码
#docker exec #进入容器后,开启一个新的终端 ,可以在里面操作(常用)
#docker attach #进程正在执行进程的终端。不会启动新的进程
从容器拷贝到主机的命令
docker
[root@localhost home]# ls
! kuangshen.java shitou
[root@localhost home]# docker attach ab6bbe27f40c #打开终端进入容器
[root@ab6bbe27f40c /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@ab6bbe27f40c /]# touch test.java #在容器里建立test.java
[root@ab6bbe27f40c /]# exit #退出
exit
[root@localhost home]# docker ps 查看运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost home]# docker ps -a 容器虽然停止运行,但是数据还在
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab6bbe27f40c centos "/bin/bash" 3 minutes ago Exited (0) 13 seconds ago elated_cohen
[root@localhost home]# docker cp ab6bbe27f40c:/test.java /home/
#容器id 文件在容器的路径,名字 要复制到的路径
[root@localhost home]# ls 查看test.java 已经存在了
kuangshen.java shitou test.java
#拷贝是一个手动的过程,为了-v卷的技术可以实现