Docker的常用命令
帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker --help #万能命令
帮助文档的地址:(https://docs.docker.com/reference/)
镜像命令
docker images 查看所有本地的主机上的镜像
[root@localhost lpeng]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
Options:
-a, --all #列出所有的镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet #只显示镜像的ID
docker search 搜索镜像
[root@localhost lpeng]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13654 [OK]
mariadb MariaDB Server is a high performing open sou… 5217 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 717 [OK]
# 可选项,通过收藏来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于等于3000
[root@localhost lpeng]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13654 [OK]
mariadb MariaDB Server is a high performing open sou… 5217 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@localhost lpeng]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
0ed027b72ddc: Pull complete #分层下载,docker images的核心 联合文件系统
0296159747f1: Pull complete
3d2f9b664bd3: Pull complete
df6519f81c26: Pull complete
36bb5e56d458: Pull complete
054e8fde88d0: Pull complete
f2b494c50c7f: Pull complete
132bc0d471b8: Pull complete
135ec7033a05: Pull complete
5961f0272472: Pull complete
75b5f7a3d3a4: Pull complete
Digest: sha256:3d7ae561cf6095f6aca8eb7830e1d14734227b1fb4748092f2be2cfbccf7d614 #签名
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
docker rmi 删除镜像
[root@localhost lpeng]# docker rmi -f 镜像id #删除指定的镜像
[root@localhost lpeng]# docker rmi -f 镜像id 镜像id 镜像id 镜像id #删除多个镜像
[root@localhost lpeng]# docker rmi -f $(docker images -aq) #删除全部的镜像
容器命令
拉取镜像
说明:有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-i 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
-P 随机指定端口
# 测试,启动并进入容器
[root@localhost lpeng]# docker run -it centos /bin/bash
[root@33f491868bad /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回到主机
[root@33f491868bad /]# exit
exit
[root@localhost lpeng]# ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[root@localhost lpeng]# cd /
[root@localhost /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
列出所有的运行的容器
# docker ps 命令
-a # 列出当前正在运行的容器+带出历史运行的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33f491868bad centos "/bin/bash" 4 minutes ago Exited (130) About a minute ago suspicious_pare
c52deeb8b004 feb5d9fea6a5 "/hello" 39 minutes ago Exited (0) 39 minutes ago hungry_johnson
8142522de3bc feb5d9fea6a5 "/hello" 39 minutes ago Exited (0) 39 minutes ago wonderful_kare
退出容器
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 /]# docker run -d centos
# 问题docker ps,发现centos停止了
# 常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器,没有日志
# 自己编写一段shell脚本
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo lpeng;sleep 1;done"
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
246bf0614536 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds loving_keldysh
# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数
[root@localhost /]# docker logs -tf --tail 10 246bf0614536
查看容器中的进程信息
# 命令 docker top 容器id
[root@localhost /]# docker top 246bf0614536
UID PID PPID C STIME
root 7070 7052 0 17:31
root 7405 7070 0 17:35
查看镜像的元数据
#命令
docker inspect 容器id
# 测试
[root@localhost /]# docker inspect 246bf0614536
[
{
"Id": "246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f",
"Created": "2023-01-05T09:31:21.883691281Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo lpeng;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 7070,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-01-05T09:31:22.171994494Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f/hostname",
"HostsPath": "/var/lib/docker/containers/246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f/hosts",
"LogPath": "/var/lib/docker/containers/246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f/246bf0614536200a7f4534505ff33efa6a8d80cf4b46bd1aaa24f7951029038f-json.log",
"Name": "/loving_keldysh",
"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/bdd46a18639eab141ac67188f71dd8b7f6bbb305a9ea4a42c08eaaa74b0508f4-init/diff:/var/lib/docker/overlay2/18772c2f40d453362e92bad992100698c51ec1b41d047ac10cbc2c89d4d5747b/diff",
"MergedDir": "/var/lib/docker/overlay2/bdd46a18639eab141ac67188f71dd8b7f6bbb305a9ea4a42c08eaaa74b0508f4/merged",
"UpperDir": "/var/lib/docker/overlay2/bdd46a18639eab141ac67188f71dd8b7f6bbb305a9ea4a42c08eaaa74b0508f4/diff",
"WorkDir": "/var/lib/docker/overlay2/bdd46a18639eab141ac67188f71dd8b7f6bbb305a9ea4a42c08eaaa74b0508f4/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "246bf0614536",
"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 lpeng;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": "9ed94c844f071b0771f48860261e41d9fe9262f2c66561ae218aca3f6d4cee85",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/9ed94c844f07",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a31b2a27f68716a60a8c6e1556a8714cebec88e388086c754235973b61a36b72",
"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": "3094662f4abae25daf0ec99a5fdd6372882157b6f817e1ac0133771f4cdaf8b7",
"EndpointID": "a31b2a27f68716a60a8c6e1556a8714cebec88e388086c754235973b61a36b72",
"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
}
}
}
}
]
[root@localhost /]#
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
docker exec -it 容器id bashShell
# 测试
[root@localhost /]# docker exec -it 246bf0614536 /bin/bash
[root@246bf0614536 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@246bf0614536 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:31 ? 00:00:00 /bin/sh -c while true;do echo lpeng;sleep 1;done
root 774 0 0 09:44 pts/0 00:00:00 /bin/bash
root 861 1 0 09:45 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 862 774 0 09:45 pts/0 00:00:00 ps -ef
# 方式二
docker attach 容器id
# 测试
[root@localhost lpeng]# docker attach 246bf0614536
lpeng
lpeng
lpeng
lpeng
正在执行当前的代码...
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不回启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
# 查看当前主机目录下
[root@localhost home]# ls
lpeng
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5922b259c65 centos "/bin/bash" 53 seconds ago Up 52 seconds vigorous_raman
# 进入docker容器内部
[root@localhost home]# docker attach b5922b259c65
[root@b5922b259c65 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 在容器内部新建一个文件
[root@b5922b259c65 /]# touch lpeng.java
[root@b5922b259c65 /]# ls
bin dev etc home lib lib64 lost+found lpeng.java media mnt opt proc root run sbin srv sys tmp usr var
[root@b5922b259c65 /]# exit
exit
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#将这个文件拷贝到主机上
[root@localhost home]# docker cp b5922b259c65:/lpeng.java /home
[root@localhost home]# ls
lpeng lpeng.java
[root@localhost home]#
# 拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现自动同步docker data
小结
attach Attach local standard input, output, and error streams to a running container标签:容器,常用命令,镜像,Docker,null,root,docker,localhost From: https://www.cnblogs.com/lp214/p/17029459.html
#当前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