docker version # 查看docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker --help # 帮助命令
docker images # 查看本机上所有镜像
docker search # 搜索镜像
docker pull # 下载镜像 docker image pull
docker rmi # 删除镜像
[root@ds1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
eladmin latest 6156c405297e 2 days ago 572MB
las_oa latest 6156c405297e 2 days ago 572MB
tomcat latest 51c25da77baf 7 days ago 474MB
mysql 8.0 4073e6a6f542 2 weeks ago 530MB
nginx latest 904b8cb13b93 3 weeks ago 142MB
redis latest f9c173b0f012 3 weeks ago 117MB
# 解释
REPOSITORY # 镜像的仓库源
TAG # 镜像的标签
IMAGE ID # 镜像的id
CREATED # 镜像的创建时间
SIZE # 镜像的大小
# 可选项 Options:
-a, --all # 列出所有的镜像
-q, --quiet # 只显示镜像的ID
[root@ds1 ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13962 [OK]
mariadb MariaDB Server is a high performing open sou… 5321 [OK]
# 可选项
--filter=STARS=3000 搜索出来的镜像就是STARS大于3000的
# 下载镜像 docker pull 镜像名[:tag] (tag是版本)
[root@ds1 /]# docker pull mysql
Using default tag: latest # 如果不写tag,默认就是latest最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载:docker image 的核心---联合文件系统
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.io/library/mysql:latest
# 指定版本下载
docker pull mysql:5.7
docker rmi -f 镜像id # 删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id # 删除指定的多个镜像
docker rmi -f $(docker images -aq) # 删除所有的镜像
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name = "Name" 容器名字 tomcat01,tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-p 随机指定端口
# 测试,启动并进入容器
docker 容器是由 docker 镜像创建的运行实例
[root@ds1 /]# docker run -it centos /bin/bash
[root@0e1dfb1233d0 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的!
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@0e1dfb1233d0 /]# exit
exit
[root@ds1 /]# ls
bin data etc lib media my opt root sbin sys usr www
boot dev home lib64 mnt mydata proc run srv tmp va
列出所有的运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器 + 带出历史运行过的容器
-n=? # 显示最近创建的容器 ? 代表个数
-q # 只显示容器的编号
exit # 直接退出容器停止并退出
Ctrl + P + Q # 容器不停止退出
docker rm 容器id # 删除指定容器,不能删除正在运行的容器,如果要强制删除 rm -f (f是force)
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -aq|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 # 自己编写一段shell脚本
[root@ds1/]# docker run -d centos /bin/sh -c "while true; do echo Hello World;sleep 1;done"
# 显示日志
docker logs
-t # 显示日志加时间
-f # 保留打印窗口,持续打印
--tail number # 要显示的最后的日志条数
[root@ds1/]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
babf34105183 centos "/bin/sh -c 'while t…" 20 seconds ago Up 19 seconds strange_blackburn
[root@ds1/]# docker logs -tf --tail 10 babf34105183
docker stop id
# 命令 docker top 容器id
[root@ds1 /]# docker top 94c79bffd32f
UID PID PPID C STIME TTY TIME CMD
root 15569 15550 0 16:08 pts/0 00:00:00 /bin/bash
# 命令 docker inspect 容器id
[root@ds1 /]# docker inspect 94c79bffd32f
[
{
"Id": "94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234",
"Created": "2023-03-24T08:08:14.35591371Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 15569,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-03-24T08:08:14.668825682Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234/hostname",
"HostsPath": "/var/lib/docker/containers/94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234/hosts",
"LogPath": "/var/lib/docker/containers/94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234/94c79bffd32ff039270b954b4d71ea7fb33c135291354b502577a1d5dc547234-json.log",
"Name": "/jolly_lewin",
"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,
"ConsoleSize": [
30,
89
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/e314b8bb0c8db0a85ddc0d7ee8515a425cb1b49558d18cf5eeb1d21f4a88b3eb-init/diff:/var/lib/docker/overlay2/6768ff10d66787a49db51b3c2ee4c4167388b4c86ac740520f97dd7f2d88a615/diff",
"MergedDir": "/var/lib/docker/overlay2/e314b8bb0c8db0a85ddc0d7ee8515a425cb1b49558d18cf5eeb1d21f4a88b3eb/merged",
"UpperDir": "/var/lib/docker/overlay2/e314b8bb0c8db0a85ddc0d7ee8515a425cb1b49558d18cf5eeb1d21f4a88b3eb/diff",
"WorkDir": "/var/lib/docker/overlay2/e314b8bb0c8db0a85ddc0d7ee8515a425cb1b49558d18cf5eeb1d21f4a88b3eb/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "94c79bffd32f",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"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": "3812cdfdaf8fd8b1b9494cc54c8a6b8132a6c12ccb3d7456e1736b09e9fbf6f0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3812cdfdaf8f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e1db613abc14887062c5fc9d094081ada397b7f7e1bd38ce6e0ec9a5b3a4e82e",
"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": "f7c9d5077b7c59fe4235f80d94461c8076a73479f4fe4966ca4cd1e3f059f1ac",
"EndpointID": "e1db613abc14887062c5fc9d094081ada397b7f7e1bd38ce6e0ec9a5b3a4e82e",
"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 baseShell
# 测试
[root@ds1 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94c79bffd32f centos "/bin/bash" 18 minutes ago Up 18 minutes jolly_lewin
[root@ds1 /]# docker exec -it 94c79bffd32f /bin/bash
[root@94c79bffd32f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@94c79bffd32f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:08 pts/0 00:00:00 /bin/bash
root 15 0 0 08:27 pts/1 00:00:00 /bin/bash
root 30 15 0 08:27 pts/1 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 测试
[root@ds1 /]# docker attach 94c79bffd32f
[root@94c79bffd32f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
# 命令
docker cp [r] 容器id : 容器内路径 目的主机路径
# 参数r: 递归拷贝
# 测试
# 查看当前主机目录下的文件
[root@ds1 home]# ls
data dockers hd las_oa las_oa_web mysql nginx www
# 创建并进入容器内部
[root@ds1 home]# docker run -it centos /bin/bash
[root@4631d6f2bbe4 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@4631d6f2bbe4 /]# cd home/
# 在容器内新建一个文件test.java
[root@4631d6f2bbe4 home]# touch test.java
[root@4631d6f2bbe4 home]# exit
exit
[root@ds1 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ds1 home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4631d6f2bbe4 centos "/bin/bash" 35 seconds ago Exited (0) 14 seconds ago nifty_goldstine
# 将这个文件拷贝到主机上
[root@ds1 home]# docker cp 4631d6f2bbe4:/home/test.java /home
Preparing to copy...
Successfully copied 1.536kB to /home
[root@ds1 home]# ls
data dockers hd las_oa las_oa_web mysql nginx test.java www
# 拷贝是一个手动过程,未来我们使用 -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