帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #万能命令
帮助文档的地址:
https://docs.docker.com/reference/
镜像命令
docker images 查看所有本地主机上的镜像
[root@VM-12-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 14 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
docker search 搜索镜像
[root@VM-12-4-centos ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10570 [OK]
mariadb MariaDB Server is a high performing open sou… 3949 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 774 [OK]
#可选项,通过收藏来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@VM-12-4-centos ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10570 [OK]
mariadb MariaDB Server is a high performing open sou… 3949 [OK]
docker pull下载镜像
#下载镜像 docker pull 镜像名[:tag]
[root@VM-12-4-centos ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载,docker images的核心,联合文件系统
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
[root@VM-12-4-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
[root@VM-12-4-centos ~]# docker rmi -f 镜像id #删除指定的容器
[root@VM-12-4-centos ~]# docker rmi -f 镜像id 容器id 容器id #删除多个容器
[root@VM-12-4-centos ~]# docker rmi -f $(docker images -aq) #删除全部容器
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-p 随机指定端口
# 测试,启动并进入容器
[root@VM-12-4-centos ~]# docker run -it centos /bin/bash
[root@0725b6a76727 /]#
[root@0725b6a76727 /]# ls #查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#从容器中退回主机
[root@0725b6a76727 /]# exit
exit
[root@VM-12-4-centos /]# ls
bin data etc jdk lib64 media opt proc run srv tmp var
boot dev home lib lost+found mnt patch root sbin sys usr www
列出所有运行的容器
# docker ps 命令
-a #列出当前正在运行的容器 + 带出离十运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[root@VM-12-4-centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-12-4-centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0725b6a76727 centos "/bin/bash" 3 minutes ago Exited (127) About a minute ago lucid_kapitsa
ba8ae85314d5 bf756fb1ae65 "/hello" 16 hours ago Exited (0) 16 hours ago thirsty_ellis
acd9a2776630 bf756fb1ae65 "/hello" 16 hours ago Exited (0) 16 hours ago zealous_zhukovsky
退出容器
exit #直接容器停止并退出
ctrl + P + Q #容器不停止退出
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果强制删除rm -f
docker rm -f $(docker ps -aq) #删除全部的容器
docker ps -a -ql|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行容器
docker kill 容器id #强制停止当前容器
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名!
[root@VM-12-4-centos ~]# docker run -d centos
#问题docker ps,发现centos 停止了
#常见的坑:docker 容器使用后台运行,就必要要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器,没有日志
# 自己编写以端shell脚本
[root@VM-12-4-centos ~]# docker run -d centos /bin/sh -c "while true; do echo kuangshen; sleep 1; done"
[root@VM-12-4-centos ~]# docker ps
CONTAINER ID IMAGE
618e064e4a55 centos
# 显示日志
--tf
--tail number # 要显示日志条数
[root@VM-12-4-centos ~]# docker logs -tf --tail 10 618e064e4a55
查看容器中进程信息 ps
top 命令 docker top 容器id
[root@VM-12-4-centos ~]# docker top 618e064e4a55
UID PID PPID C STIME TTY TIME CMD
root 21151 21132 0 15:32 ?
root 23163 21151 0 15:39 ?
查看镜像的元数据
# docker inspect 容器
docker inspect 容器id
[root@VM-12-4-centos ~]# docker inspect 618e064e4a55
[
{
"Id": "618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5",
"Created": "2021-03-10T07:32:59.228772815Z",
"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": 21151,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-10T07:32:59.593307031Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5/hostname",
"HostsPath": "/var/lib/docker/containers/618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5/hosts",
"LogPath": "/var/lib/docker/containers/618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5/618e064e4a55a69c5fe8e823bc066b9c5bf3feb53919ed4c8dc705bbd52210d5-json.log",
"Name": "/friendly_noyce",
"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/cf71b900f6689c82c489bc506179c527c9222e30b60f6ccbb0f62156933f7a74-init/diff:/var/lib/docker/overlay2/eb75ebbc7ab610f3930e68197fb0c4b0dbcf649abc57316c197e1576fd0606be/diff",
"MergedDir": "/var/lib/docker/overlay2/cf71b900f6689c82c489bc506179c527c9222e30b60f6ccbb0f62156933f7a74/merged",
"UpperDir": "/var/lib/docker/overlay2/cf71b900f6689c82c489bc506179c527c9222e30b60f6ccbb0f62156933f7a74/diff",
"WorkDir": "/var/lib/docker/overlay2/cf71b900f6689c82c489bc506179c527c9222e30b60f6ccbb0f62156933f7a74/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "618e064e4a55",
"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": "20201204",
"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": "352941ee4230e98f75c0fa17d2c7c91f394283263912cd20ae04b3778ff2a535",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/352941ee4230",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a757516e1aa56f0c89df2dfb19dad40709fb4d90c03edbde39230a195af3dfc9",
"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": "c316f0541796dc97d095d803978f4854729079a58edbf5890ac154e113ae3180",
"EndpointID": "a757516e1aa56f0c89df2dfb19dad40709fb4d90c03edbde39230a195af3dfc9",
"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
# 测试
[root@VM-12-4-centos ~]# docker exec -it 618e064e4a55 /bin/bash
[root@618e064e4a55 /]#
[root@618e064e4a55 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@618e064e4a55 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:32 ? 00:00:00 /bin/sh -c while true; do echo kuangshen; sleep 1; done
root 929 0 0 07:48 pts/0 00:00:00 /bin/bash
root 955 1 0 07:48 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 956 929 0 07:48 pts/0 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 测试
[root@VM-12-4-centos ~]# docker attach 618e064e4a55
正在执行当前的代码……
# docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach #进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
# 查看当前主机目录下
[root@VM-12-4-centos home]# ls
kuangshen.java lighthouse www
[root@VM-12-4-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be061e0dbf59 centos "/bin/bash" 36 seconds ago Up 36 seconds serene_murdock
# 进入docker容器内部
[root@VM-12-4-centos home]# docker attach be061e0dbf59
[root@be061e0dbf59 /]# cd /home
[root@be061e0dbf59 home]#
[root@be061e0dbf59 home]#
[root@be061e0dbf59 home]# ls
[root@be061e0dbf59 home]#
# 在容器中新建文件
[root@be061e0dbf59 home]# touch test.java
[root@be061e0dbf59 home]#
[root@be061e0dbf59 home]# ls
test.java
[root@be061e0dbf59 home]# exit
exit
[root@VM-12-4-centos home]#
[root@VM-12-4-centos home]#
[root@VM-12-4-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-12-4-centos home]#
[root@VM-12-4-centos home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be061e0dbf59 centos "/bin/bash" 2 minutes ago Exited (0) 9 seconds ago serene_murdock
[root@VM-12-4-centos home]#
# 将这个文件拷贝出来到主机上
[root@VM-12-4-centos home]# docker cp be061e0dbf59:/home/test.java /home
[root@VM-12-4-centos home]#
[root@VM-12-4-centos home]# ls
kuangshen.java lighthouse test.java www
# 拷贝是一个手动的过程,未来我们使用 -v 卷的技术,可以实现
小结
attach Attach to a running container #当前shell下attach连接指定运行镜像
bulid Bulid an image from a Dockerfile #通过Dockerfile 定制镜像
commit Create a new image from a container changes #提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中
create Create a new container #创建一个新的容器,同run,但不启动容器
diff Inspect changes on a container's filesystem #查看 dokcer容器变化
events Get real time events from the server #从docker服务获取容器实时事件
exec Run a command in an existing container # 在已存在的 容器上运行命令
export Stream the contents of a container as a tar archive #导出容器的内容流作为一个 tar 归档文件【对应import】
history Show the history of an image #展示一个镜像形成历史
iamges List images #列出系统当前镜像
import Create a new filesystem image from the contents of a tarball #从tar包中的内容创建一个新的文件系统映像【对应export】
info Display system-wide infrormantion on a container #查看容器详细信息
kill kill a running container #kill指定docker容器
load Load an image from a tar archive #从tat包中加载一个镜像【对应save】
login Register or Login to the docker registry server #注册或者登陆一个docker 源服务器
logout Log out from a Docker registry server #从当前 Docker register 退出
logs Fetch the logs of a container #输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT #查看映射端口对应的容器内部源端
pause Pause all processes within a container #暂停容器
ps List containers #列出容器列表
pull Pull an image or a repository from the docker registry server #从docker镜像源服务器拉取指定镜像或者库镜像
restart Remove one or more containers #重启运行的容器
rm Remove a running container #移除一个或者多个容器
rmi Remove one or more images #创建一个新的容器并运行一个命令
save Save an image to a tar archive #保存一个镜像为tar包【对应load】
search Search for an image on the Docker Hub #在docker hub中搜索镜像
start Start a stopped containers #启动容器
stop Stop a runing containers #停止容器
tag Tag an image into a repository #给源中镜像打标签
top Lookup the running processes of a container #查看容器运行的进程信息
unpause Unpause a paused container #取消暂停容器
version Show the docker version information #查看docker版本号
wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值