帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令
帮助文档的地址:https://docs.docker.com/reference/
镜像命令
docker images 查看所有本地的主机上的镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1d622ef86b13 2 months ago 73.9MB
hello-world latest bf756fb1ae65 6 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
Options:
-a, --all # 列出所有的镜像
-q, --quiet # 只显示镜像的ID
docker search 搜索镜像
# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9735 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3552 [OK]
# 可选项 ,通过收藏来过滤
--filter=START=3000 搜索出来的镜像就是STARS大于3000
# docker search mysql -f STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9735 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3552 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名:版本
# docker pull mysql
Using default tag: latest # 如果不写 tag,默认就是 latest
latest: Pulling from library/mysql
8559a31e96f4: Pull complete # 分层下载,docker iamge的核心 联合文件系统
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
571e8a282156: Pull complete
e7cc823c6090: Pull complete
61161ba7d2fc: Pull complete
74f29f825aaf: Pull complete
d29992fd199f: Pull complete
Digest: sha256:fe0a5b418ecf9b450d0e59062312b488d4d4ea98fc81427e3704f85154ee859c # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本
# 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
0a52a5c57cd9: Pull complete
3b816a39d367: Pull complete
13ee22d6b3bb: Pull complete
e517c3d2ba35: Pull complete
Digest: sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
# docker rmi -f 镜像id # 删除指定的镜像
# docker rmi -f 镜像id 镜像id 镜像id 镜像id 镜像id # 删除多个镜像
# docker rmi -f 6e447ce4863d # 删除指定的镜像
Untagged: mysql:latest
Untagged: mysql@sha256:fe0a5b418ecf9b450d0e59062312b488d4d4ea98fc81427e3704f85154ee859c
Deleted: sha256:6e447ce4863d6c90060feaf987711478b4f0bd6e555f6a2a29d43d2163865194
Deleted: sha256:074ea4181381471cd9a1da6b8dc86e8c8c720f814e4a1c7a7ce213c63d293b0c
Deleted: sha256:607eb412516c455f51a1bdc95197cf1d88411d5ab175c477a42b75380abac882
Deleted: sha256:1fc2b8fbd17ce0a796ce69817842d035f26bf609b8cbe792e6c68a6ce4ed2cfe
Deleted: sha256:620e22d166022716e2d8984417abecdd637b30e3815e9f3b543e93cdde1d09e9
Deleted: sha256:f67b13ad42222e45e0eaf18adbc9f408e2dc32cdb814be4f3243a6ab0b5bcd0a
# 删除全部镜像
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 随机指定端口
# 测试,启动并进入容器
# docker run -it centos /bin/bash
[root@3cff09aecbf1 /]# ls # 查看容器内的centos ,基础版本,很多命令是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退出
[root@3cff09aecbf1 /]# exit
exit
列出所有的运行容器
# docker ps 命令
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建过的容器
-q # 只显示容器的编号
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cff09aecbf1 centos "/bin/bash" About a minute ago Exited (0) 51 seconds ago silly_satoshi
079fe1603ad2 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago silly_wu
6307cc5d8bfe bf756fb1ae65 "/hello" 43 hours ago Exited (0) 43 hours ago unruffled_lewin
21d1c28f6898 bf756fb1ae65 "/hello" 7 days ago Exited (0) 7 days ago strange_euler
24f08655d3f1 bf756fb1ae65 "/hello" 8 days ago Exited (0) 8 days ago eloquent_joliot
93e02570da1d 1d622ef86b13 "/bin/bash" 4 weeks ago Exited (0) 4 weeks ago elated_sammet
d3f4a3a568a2 1d622ef86b13 "/bin/echo 'hello wo…" 4 weeks ago Exited (0) 4 weeks ago frosty_joliot
# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cff09aecbf1 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago silly_satoshi
# docker ps -aq
3cff09aecbf1
079fe1603ad2
6307cc5d8bfe
21d1c28f6898
24f08655d3f1
93e02570da1d
d3f4a3a568a2
退出容器
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 镜像名
# docker run -d centos
# 问题 docker ps ,发现 centos 停止了
# 常见的坑:docker 容器使用后台运行, 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止
查看日志
docker logs -f -t --tail 容器,没有日志
# 自己写一段shell脚本
# docker run -d centos /bin/sh -c "while true;do echo hello;sleep 1;done"
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ded628d05e27 centos "/bin/sh -c 'while t…" 4 seconds ago Up 3 seconds beautiful_agnesi
# 显示日志
-t # 显示时间戳
-f # 持续显示
--tail number # 要显示的日志条数
# docker logs -tf --tail 10 ded628d05e27
查看容器中的进程信息 ps
# docker top ded628d05e27
UID PID PPID C STIME TTY TIME
root 24029 23996 0 15:49 ? 00:00:00
root 24619 24029 0 15:54 ? 00:00:00
查看镜像的元数据
# 命令
docker inspect 容器id
# 测试
# docker inspect ded628d05e27
[
{
"Id": "ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47",
"Created": "2020-07-16T07:49:59.556948441Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo haha;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 24029,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-07-16T07:50:00.01160079Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
"ResolvConfPath": "/var/lib/docker/containers/ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47/hostname",
"HostsPath": "/var/lib/docker/containers/ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47/hosts",
"LogPath": "/var/lib/docker/containers/ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47/ded628d05e2791eccb46315edf3a7b930b06461160ec39fb68cd396e7df6fc47-json.log",
"Name": "/beautiful_agnesi",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"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/755cca44d41b0f8e9789f10899e4600b0bf8d41f0b3f521c9e75870f99a5b80f-init/diff:/var/lib/docker/overlay2/67dc3dc0bffa381df00b7d5fbfbf7858c69fd8944aca7237d5cdc2b2d135bda6/diff",
"MergedDir": "/var/lib/docker/overlay2/755cca44d41b0f8e9789f10899e4600b0bf8d41f0b3f521c9e75870f99a5b80f/merged",
"UpperDir": "/var/lib/docker/overlay2/755cca44d41b0f8e9789f10899e4600b0bf8d41f0b3f521c9e75870f99a5b80f/diff",
"WorkDir": "/var/lib/docker/overlay2/755cca44d41b0f8e9789f10899e4600b0bf8d41f0b3f521c9e75870f99a5b80f/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "ded628d05e27",
"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 liuchun;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200611",
"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": "b2d4bd3bcb555b12d6eec770aa63a6c62a3e147e7ed8146cf2c87c8746bbd739",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/b2d4bd3bcb55",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "ac535c97e73f1412e91d241fda1799076ca5b7aaa794d153ccd0886d7b666411",
"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": "d85d4dacedfa6a22ff37e1af25614c3e844d6d37c6a6051544aa5410dc3d77fc",
"EndpointID": "ac535c97e73f1412e91d241fda1799076ca5b7aaa794d153ccd0886d7b666411",
"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 exec -it 容器id bashShell
# 测试
# docker exec -it ded628d05e27 /bin/bash
[root@ded628d05e27 /]# ps
PID TTY TIME CMD
3140 pts/0 00:00:00 bash
3156 pts/0 00:00:00 ps
[root@ded628d05e27 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ded628d05e27 /]#
# 方式二
docker attach 容器id
# docker attach ded628d05e27
正在执行当前的代码
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的终端
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的地主机路径
# 复制容器内 hmoe下的test.txt 到当前路径下
# docker cp 26ea8be56027:/home/test.txt .
# 拷贝是一个手动过程,未来使用 -v 卷的技术,可以实现自动同步
小结
attach Attach local standard input, output, and error streams to a running container
# 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile
# 通过 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
# 查看 docker 容器变化
events Get real time events from the server
# 从 docker 服务获取容器实时事件
exec Run a command in a running container
# 在已存在的容器上运行命令
export Export a container's filesystem as a tar archive
# 导出容器的内容流作为一个 tar 归档文件 对应[import]
history Show the history of an image
# 展示镜像的历史
images List images
# 列出当前镜像
import Import the contents from a tarball to create a filesystem image
# 从tar包中的内容创建一个新的文件系统映像 对应[export]
info Display system-wide information
# 显示系统相关信息
inspect Return low-level information on Docker objects
# 查看容器详细信息
kill Kill one or more running containers
# 杀死指定docker容器
load Load an image from a tar archive or STDIN
# 从 tar 包中加载一个镜像 对应[save]
login Log in to a Docker registry
# 注册或登录一个 docker 源服务器
logout Log out from a Docker registry
# 从当前 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
# 从docker镜像源服务器拉取指定镜像
push Push an image or a repository to a registry
# 推送指定镜像或则库镜像至docker源服务器
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)
# 保存一个或多个镜像成 tar 包 对应[load]
search Search the Docker Hub for images
# 从docker源中搜索镜像
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
# 显示docker 的版本信息
wait Block until one or more containers stop, then print their exit codes
# 截取容器停止是的退出状态值