帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 万能命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/docker/
搜索镜像地址:https://hub.docker.com/
镜像命令
* docker images 查看所有本地的主机上的镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示id
* docker search 搜索镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12373 [OK]
mariadb MariaDB Server is a high performing open sou… 4762 [OK]
# 可选项,通过搜索来过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12373 [OK]
mariadb MariaDB Server is a high performing open sou… 4762 [OK]
* docker pull 下载镜像
# 下载镜像 docker pull 镜像名:tag[:tag]
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker pull mysql
Using default tag: latest # 如果不写tag(tag指定版本),默认就是latest(最新版本)
latest: Pulling from library/mysql
f003217c5aae: Pull complete # 分层下载,docker image的核心 联合文件系统
65d94f01a09f: Pull complete
43d78aaa6078: Pull complete
a0f91ffbdf69: Pull complete
59ee9e07e12f: Pull complete
04d82978082c: Pull complete
70f46ebb971a: Pull complete
db6ea71d471d: Pull complete
c2920c795b25: Pull complete
26c3bdf75ff5: Pull complete
9ec1f1f78b0e: Pull complete
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 下面两个命令等价
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker pull mysql:5.7
^[[15~5.7: Pulling from library/mysql
f003217c5aae: Already exists # 重复依赖不再下载
65d94f01a09f: Already exists
43d78aaa6078: Already exists
a0f91ffbdf69: Already exists
59ee9e07e12f: Already exists
04d82978082c: Already exists
70f46ebb971a: Already exists
ba61822c65c2: Pull complete
dec59acdf78a: Pull complete
0a05235a6981: Pull complete
c87d621d6916: Pull complete
Digest: sha256:1a73b6a8f507639a8f91ed01ace28965f4f74bb62a9d9b9e7378d5f07fab79dc
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f26e21ddd20d 9 days ago 450MB
mysql latest 667ee8fb158e 9 days ago 521MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
* docker rmi 删除镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f 镜像id # 删除指定的镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f 镜像id 镜像id # 删除多个镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f $(docker images -aq) # 删除全部的镜像
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
* 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行,ja nohup
-it 使用交互方式进行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-P 随机指定端口(大写)
# 测试,启动并进入容器
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker run -it centos /bin/bash
[root@3222c9af3e6a /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@3222c9af3e6a /]# exit
exit
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# ls
12311.txt.gz 1.txt 222.txt 3.txt a1.txt a3.txt a5.txt a7.txt a9.txt bzip2.txt.bz2 gzip.txt.gz mulu1 testzip
123123.txt.bz2 2222.txt 2.txt a10.txt a2.txt a4.txt a6.txt a8.txt bzip2.txt gzip.txt muli1 mulu2
* 列出所有的运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器,带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3222c9af3e6a centos "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago tender_lederberg
2307467c5bcd feb5d9fea6a5 "/hello" 6 weeks ago Exited (0) 6 weeks ago brave_mccarthy
* 退出容器
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@iZ2zei1yo0mi0ze1ngipksZ ~]# docker run -d centos
# 问题 docker ps,发现centos停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就没有程序了
* 查看日志
docker logs -f -t --tail 容器,没有日志
# 自己编写一段shell脚本
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker run -d centos /bin/sh -c "while true;do echo diandian;sleep 1;done"
3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b5834825e65 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds gracious_shtern
# 显示日志
-tf #显示日志(-t显示时间戳)
--tail number #显示日志条数
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker logs -tf --tail 10 3b5834825e65
* 查看容器中的进程信息
# 命令 docker top 容器id
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker top 3b5834825e65
UID PID PPID C STIME TTY TIME CMD
root 4597 4578 0 09:23 ? 00:00:00 /bin/sh -c while true;do echo diandian;sleep 1;done
root 10956 4597 0 09:29 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
* 查看镜像的元数据
# 命令
docker inspect 容器id
# 测试
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker inspect 3b5834825e65
[
{
"Id": "3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199",
"Created": "2022-04-13T01:23:56.752578267Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo diandian;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4597,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-13T01:23:57.074372054Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199/hostname",
"HostsPath": "/var/lib/docker/containers/3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199/hosts",
"LogPath": "/var/lib/docker/containers/3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199/3b5834825e654e40568cf7acd22955939933e3060532dd33509ea53cc6e54199-json.log",
"Name": "/gracious_shtern",
"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/7875b5153889356b2e86577a1090c7ad6e0c1ebcc1727ba321b30ed827f28a44-init/diff:/var/lib/docker/overlay2/86b2f760bb54d0ac7426b2c588ef9e3edc79cb6a9c801ec75b51be61ddb8b57f/diff",
"MergedDir": "/var/lib/docker/overlay2/7875b5153889356b2e86577a1090c7ad6e0c1ebcc1727ba321b30ed827f28a44/merged",
"UpperDir": "/var/lib/docker/overlay2/7875b5153889356b2e86577a1090c7ad6e0c1ebcc1727ba321b30ed827f28a44/diff",
"WorkDir": "/var/lib/docker/overlay2/7875b5153889356b2e86577a1090c7ad6e0c1ebcc1727ba321b30ed827f28a44/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3b5834825e65",
"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 diandian;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": "e3c7dfbfc4058fde4d0711129efb13e308458ef96b8ec306998d44c2d0bba56a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e3c7dfbfc405",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "fa4ef3535282bf1813bfe7d5403fa90c4f5abcc53b43d8fa911f2e0a344854e3",
"Gateway": "172.18.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:12:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9ef033d0714d330565e2eee726b0c6e4d7cb4830cfe56e46377bd007d24af518",
"EndpointID": "fa4ef3535282bf1813bfe7d5403fa90c4f5abcc53b43d8fa911f2e0a344854e3",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03",
"DriverOpts": null
}
}
}
}
]
* 进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bashShell
# 测试
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker exec -it 3b5834825e65 /bin/bash
[root@3b5834825e65 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@3b5834825e65 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Apr13 ? 00:00:27 /bin/sh -c while true;do echo diandian;sleep 1;done
root 21118 0 0 01:21 pts/0 00:00:00 /bin/bash
root 21181 21118 0 01:22 pts/0 00:00:00 ps -ef
root 21182 1 0 01:22 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
# 方式二
docker attach 容器id
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker attach 3b5834825e65
正在执行当前的代码...
# docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
* 从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目标主机路径
# 当前主机,拷贝文件
[root@iZ2zei1yo0mi0ze1ngipksZ home]# docker cp 56338d1b7156:/home/diandian.java /home
[root@iZ2zei1yo0mi0ze1ngipksZ home]# ls
diandian.java
# 拷贝是一个手动过程,未来我们使用 -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 # 从服务获取容器实时时间
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包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
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