docker
通用命令 | 作用 |
---|---|
docker version | 显示docker的版本信息 |
docker info | 显示docker的系统信息,包括镜像和容器的数量 |
docker 命令 --help | 帮助命令 |
docker帮助文档
Reference documentation | Docker Documentation
镜像命令
镜像命令 | 信息 |
---|---|
docker images | 显示所有镜像 |
docker search 镜像名称 | 搜索镜像 |
docker search mysql | 搜索mysql镜像 |
docker pull 镜像 | 下载镜像默认最新版 |
docker pull mysql | 下载mysql镜像 |
docker pull 镜像名[:tag] | 指定版本下载镜像 |
docker rmi -f 镜像 | 删除镜像 |
docker rmi -f $(docker images -aq) | 删除全部镜像 |
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 2 months ago 13.3kB
#含义
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all 列出所有镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provi
-q, --quiet 显示镜像id
[root@localhost ~]# docker search mysql搜索镜像
docker search 搜索镜像
容器命令
容器命令 | 信息 |
---|---|
docker pull centos | 下载centos镜像 |
docker run [可选参数] image | 新建容器并启动 |
exit | 退出容器 |
docker ps | 查询当前正在运行中的容器 |
docker ps -a | 当前正在运行的和查曾经运行的容器 |
docker rm 容器id | 删除指定容器,不能删除正在运行中的容器,如果要强制删除 rm -f |
docker rm -f $(docker ps -aq) | 删除所有的容器 |
docker ps -a -qlxargs docker rm | 删除所有容器 |
docker start 容器id | 启动容器 |
docker restart 容器id | 重启容器 |
docker stop 容器id | 停止当前正在运行中的容器 |
docker kill 容器id | 强制停止当前容器 |
docker run [可选参数] image
参考说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行没进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-p 随机指定端口
测试:
启动并且进入容器
[root@iZp5qw8rqo26yzZ /]# docker run -it centos /bin/bash
[root@b0dca34bddcf /]#
查询当前正在运行中的容器
[root@iZp5qw8rqo26yzZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-a 当前正在运行的和查曾经运行的容器
-n=1? 显示最近创建的容器
-q 显示容器的编号
退出容器
eixt #容器停止退出
Ctrl+P+Q#容器不停退出
删除容器
docker ps -a -qlxargs docker rm 删除所有容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行中的容器
docker kill 容器id #强制停止当前c
常用的其他命令
常用命令 | 信息 |
---|---|
docker run -d centos(镜像名) | 通过镜像centos启动容器(后台启动容器),docker发现没有应用启动就会自动停止 |
docker logs -f -t --tail 5 容器id | 查看5条容器日志 |
docker logs -f -t --tail 10 45afdcf27544 | 查看45afdcf27544容器的10条日志,不停止日志会一直打印 |
docker top 45afdcf27544 | 查看45afdcf27544容器内部进程信息 |
docker inspect 45afdcf27544 | 查看容器45afdcf27544的元数据 |
后台启动容器
[root@iZp5qw8rqo26yzZ /]# docker run -d centos
但是docker ps 时却发现,centos没有启动,是停止状态的
#是因为,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就会没有程序了
查看日志
docker logs -f -t --tail num 容器id
容器如果开始没有日志
我们可以编写一个shell脚本
"while true;do echo kuangshen;sleep 1;done"
不停止
[root@iZp5qw8rqo26yzZ ~]# docker run -d centos /bin/sh -c "while true;do echo xccvt;sleep 1;done"
45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6
[root@iZp5qw8rqo26yzZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45afdcf27544 centos "/bin/sh -c 'while t…" 21 seconds ago Up 20 seconds priceless_solomon
查看十条日志,打印后,还会不停的打印
[root@iZp5qw8rqo26yzZ ~]# docker logs -tf --tail 10 45afdcf27544
-tf #查看日志
-tail number #要显示日志条数
[root@iZp5qw8rqo26yzZ ~]# docker logs -f -t --tail 10 45afdcf27544
2023-07-18T11:05:12.713724760Z xccvt
2023-07-18T11:05:13.716966735Z xccvt
2023-07-18T11:05:14.720340305Z xccvt
2023-07-18T11:05:15.723548214Z xccvt
2023-07-18T11:05:16.728819584Z xccvt
2023-07-18T11:05:17.732418738Z xccvt
2023-07-18T11:05:18.736425785Z xccvt
查看进程信息
查看容器内部进程信息
[root@iZp5qw8rqo26yzZ ~]# docker top 45afdcf27544
UID PID PPID C STIME TTY
root 15213 15192 0 19:04 ?
root 16778 15213 0 19:13 ?
查看镜像元数据
[root@iZp5qw8rqo26yzZ ~]# docker inspect 45afdcf27544
[
{
"Id": "45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6",
"Created": "2023-07-18T11:04:16.225202192Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo xccvt;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 15213,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-07-18T11:04:16.505865592Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6/hostname",
"HostsPath": "/var/lib/docker/containers/45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6/hosts",
"LogPath": "/var/lib/docker/containers/45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6/45afdcf2754474f6f62d7f9c80bc2c445b208ae95d3fe9e6a0f673e50b79b5d6-json.log",
"Name": "/priceless_solomon",
"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": [
42,
207
],
"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/80fe6074d48f2016db2f78cc22d349fbfb3abf729e9e056ff1f0a7866115a45a-init/diff:/var/lib/docker/overlay2/5a4f279cb0fe35b062fda2e9d54de204544884de7c721a6dba1cd5054bb72fbe/diff",
"MergedDir": "/var/lib/docker/overlay2/80fe6074d48f2016db2f78cc22d349fbfb3abf729e9e056ff1f0a7866115a45a/merged",
"UpperDir": "/var/lib/docker/overlay2/80fe6074d48f2016db2f78cc22d349fbfb3abf729e9e056ff1f0a7866115a45a/diff",
"WorkDir": "/var/lib/docker/overlay2/80fe6074d48f2016db2f78cc22d349fbfb3abf729e9e056ff1f0a7866115a45a/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "45afdcf27544",
"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 xccvt;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": "ea020a0b3757b584201676999736eb2f7856dc2fdbc14cbd6197d3c44799948c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ea020a0b3757",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "70631f1b2bafbb28f1e3eb396fc77d99f5a5149d84109312c2d924eeabf8a419",
"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": "b49bd2a740c3222f1a93b80c5551f3816aab179f707e3e54987eb034b03e71f7",
"EndpointID": "70631f1b2bafbb28f1e3eb396fc77d99f5a5149d84109312c2d924eeabf8a419",
"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 45afdcf27544 /bin/bash | 进入容器45afdcf27544 (开启一个新的终端) |
docker attach 45afdcf27544 | 进入容器45afdcf27544,进入的是正在运行的终端,不会启动新的进程 |
docker cp 83d1f434728f:/home/test.java /home | 复制容器83d1f434728f的test.java文件到home路径下 |
#通常容器都是使用后台的方式运行的,但是有时候我们需要进入容器,修改一些配置
方式一
#命令
docker exec -it 容器id bashShell
[root@iZp5qw8rqo26yzZ ~]# docker exec -it 45afdcf27544 /bin/bash
[root@45afdcf27544 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
方式二
docker attach 容器id
#测试
[root@iZp5qw8rqo26yzZ ~]# docker attach 45afdcf27544
xccvt #正在执行的代码
xccvt
从容器内拷贝文件到主机上
docker sp 容器id:容器内路径
[root@iZp5qw8rqo26yzZ ~]# docker cp 83d1f434728f:/home/test.java /home
Successfully copied 1.54kB to /home
部署ES+Kibana
命令 | 信息 |
---|---|
docker stats | 查看docker状态 |
安装运行elasticsearch
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
增加限制,
docker run -d --name elasticsearch02 --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xmx64m -Xmx512m" elasticsearch:7.6.2
可视化
portainer
docker 的图形化管理工具docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
标签:容器,--,命令,t03,镜像,docker,null,root From: https://www.cnblogs.com/2580p/p/18066933