Docker 常用命令
Posted fenixg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 常用命令相关的知识,希望对你有一定的参考价值。
Docker 的常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息
docker 命令 --help # 帮助命令
帮助文档的地址:https://docs.docker.com/engine/api/
镜像命令
docker images 查看所有本地的主机上的镜像
[root@localhost fenixgao]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
--all , -a # 列出所有镜像
--quiet , -q # 只显示镜像的id
docker search搜索镜像
[root@localhost fenixgao]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9629 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3497 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 702 [OK]
# 可选项,通过收藏来过滤
-f, --filter filter
# 搜索mysql stars>5000的项目
[root@localhost fenixgao]# docker search mysql -f =5000
[root@localhost fenixgao]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9629 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@localhost fenixgao]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
8559a31e96f4: Already exists # 分层下载,docker iamge的核心 联合文件系统
d51ce1c2e575: Already exists # 之前存在的可以共用
c2344adc4858: Already exists
fcf3ceff18fc: Already exists
16da0c38dc5b: Already exists
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
c75914a65ca2: Pull complete
1ae8042bdd09: Pull complete
453ac13c00a3: Pull complete
9e680cd72f08: Pull complete
a6b5dc864b6c: Pull complete
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 # 数字签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@localhost fenixgao]# docker pull mysql:5.7
5.7: Pulling from library/mysql
8559a31e96f4: Already exists
d51ce1c2e575: Already exists
c2344adc4858: Already exists
fcf3ceff18fc: Already exists
16da0c38dc5b: Already exists
b905d1797e97: Already exists
4b50d1c6b05c: Already exists
d85174a87144: Pull complete
a4ad33703fa8: Pull complete
f7a5433ce20d: Pull complete
3dcd2a278b4a: Pull complete
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker images 查看现有镜像
[root@localhost fenixgao]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 9cfcce23593a 6 days ago 448MB
mysql latest be0dbf01a0f3 6 days ago 541MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
docker rmi 删除镜像 (i --- images)
[root@localhost fenixgao]# docker rmi -f 镜像id # 删除指定的镜像
[root@localhost fenixgao]# docker rmi -f 镜像id 镜像id 镜像id # 删除多个镜像
[root@localhost fenixgao]# docker rmi -f $(docker images -aq) # 删除所有镜像
容器命令
说明:我们有了镜像才可以创建容器,接下来的练习是下载一个centOS镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] iamge
# 参数说明
--name ="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
# 测试,启动并进入容器
[root@localhost fenixgao]# docker run -it centos /bin/bash
[root@06d5a4c927e9 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@06d5a4c927e9 /]#
# root@localhost fenixgao VS root@06d5a4c927e9
# 从容器中退回主机
[root@06d5a4c927e9 /]# exit
exit
[root@localhost fenixgao]# ls
perl5 公共 模板 视频 图片 文档 下载 音乐 桌面
列出所有运行的容器
# docker ps 命令
# docker ps : 列出容器
-a # 列出当前正在运行的容器 + 带出历史运行过的容器
-n=? # 显示最近创建的容器(个数)
-q # 只显示容器的编号
[root@localhost fenixgao]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost fenixgao]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06d5a4c927e9 centos "/bin/bash" 4 minutes ago Exited (0) About a minute ago gallant_benz
3a36d82cd9cd bf756fb1ae65 "/hello" 18 hours ago Exited (0) 18 hours ago vigilant_brahmagupta
edd862859f68 bf756fb1ae65 "/hello" 22 hours ago Exited (0) 22 hours ago optimistic_babbage
退出容器
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 fenixgao]# docker run -d centos
# 问题 docker ps ,发现centos 停止了
# 常见的坑: docker 容器使用后台运行,就必须有一个前台进程,docker发现没有应用,就会自动停止
# nigix,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了。
查看日志
docker logs -f -t --tail 容器,没有日志
# 自己别写一段shell脚本
while true; do $(echo date); sleep 1; done
[root@localhost fenixgao]# docker run -d centos sh -c "while true; do $(echo date); sleep 1; done"
[root@localhost fenixgao]# docker ps -a
CONTAINER ID IMAGE
7cb6fd0cbd2f centos
# 显示日志
-tf #显示日志
--tail #要显示日志条数
[root@localhost fenixgao]# docker logs -tf --tail 10 7cb6fd0cbd2f
查看容器中进程信息
# 命令 docker top 容器id
[root@localhost fenixgao]# docker top 7cb6fd0cbd2f
UID PID PPID C
root 34960 34944 0
root 35670 34960 0
查看镜像的元数据
# 命令
[root@localhost fenixgao]# docker inspect 容器id
# 测试
[root@localhost fenixgao]# docker inspect 7cb6fd0cbd2f
[
{
"Id": "7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151",
"Created": "2020-06-16T08:17:18.098228Z",
"Path": "sh",
"Args": [
"-c",
"while true; do date; sleep 1; done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 34960,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-06-16T08:17:18.80397629Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
"ResolvConfPath": "/var/lib/docker/containers/7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151/hostname",
"HostsPath": "/var/lib/docker/containers/7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151/hosts",
"LogPath": "/var/lib/docker/containers/7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151/7cb6fd0cbd2f700f15cd701c71bb42f10bf56ee999a1d96cb4c9551c75690151-json.log",
"Name": "/cranky_bhabha",
"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,
"Capabilities": null,
"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/10e53752c467a753f8b958dbd5f043e86a951e1d090989193b39331c4ee37739-init/diff:/var/lib/docker/overlay2/8ee607c97da990b08a22ab3f01c6db38048f72ffcb4c54498414774b1b6db731/diff",
"MergedDir": "/var/lib/docker/overlay2/10e53752c467a753f8b958dbd5f043e86a951e1d090989193b39331c4ee37739/merged",
"UpperDir": "/var/lib/docker/overlay2/10e53752c467a753f8b958dbd5f043e86a951e1d090989193b39331c4ee37739/diff",
"WorkDir": "/var/lib/docker/overlay2/10e53752c467a753f8b958dbd5f043e86a951e1d090989193b39331c4ee37739/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "7cb6fd0cbd2f",
"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": [
"sh",
"-c",
"while true; do date; sleep 1; done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200114",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4a16e48db54fd2874ceebca4055cfb02f88d0552302939be872ba2b1309e3d1d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4a16e48db54f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "cd88ea1e0c98fb288568faeab4b1ac67a26c50ea814da612dab7c2d3fc24eb7b",
"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": "274d73aeb9d2a83160f03fc5b33ce09fa4104f3061a1fb93a3039a1c343693a1",
"EndpointID": "cd88ea1e0c98fb288568faeab4b1ac67a26c50ea814da612dab7c2d3fc24eb7b",
"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
}
}
}
}
]
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
docker exec -it 容器id bashShell
[root@localhost fenixgao]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7cb6fd0cbd2f centos "sh -c ‘while true; …" 10 minutes ago Up 10 minutes cranky_bhabha
[root@localhost fenixgao]# docker exec -it 7cb6fd0cbd2f /bin/bash
[root@7cb6fd0cbd2f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@7cb6fd0cbd2f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:17 ? 00:00:01 sh -c while true; do date; sleep 1; done
root 1328 0 0 08:28 pts/0 00:00:00 /bin/bash
root 1355 1 0 08:28 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 1356 1328 0 08:28 pts/0 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 测试
[root@localhost fenixgao]# docker attach afea7d36247b
正在执行当前的代码...
#docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
# 新建并查看一个容器
[root@localhost home]# clear
[root@localhost home]# docker run --name Test -it centos /bin/bash
[root@197884e6803b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 进入容器的home文件夹并新建一个test.java
[root@197884e6803b /]# cd /home
[root@197884e6803b home]# touch test.java
[root@197884e6803b home]# [root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
197884e6803b centos "/bin/bash" About a minute ago
# 使用docker attach 进入刚建立的容器 检验是否有test.java
[root@localhost home]# docker attach 197884e6803b
[root@197884e6803b home]# ls
test.java
[root@197884e6803b home]# exit
exit
# 使用docker cp 将容器中的test.java 拷贝到主机的home文件夹中
[root@localhost home]# docker cp 197884e6803b:/home/test.java /home
[root@localhost home]# ls
fenixgao gaofei.java test.java
[root@localhost home]#
# 拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现 /home(主机内)和/home(容器内)自动同步
小结
命令帮助手册 docker --help
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container‘s changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container‘s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container‘s filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
以上是关于Docker 常用命令的主要内容,如果未能解决你的问题,请参考以下文章
Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段