文章目录
1. 搜索镜像
方式1: 命令行
docker search ${keyword}
# eg:
docker search mysql
方式2: 网页搜索
registry.hub.docker.com
搜索
2. 查看镜像版本
registry.hub.docker.com
(目前已知的方法只有这个,也可以用curl方法,太麻烦了)
3. 下载镜像
docker pull ${image_name}[:${version}] # version不写和写 ':latest' 效果是一样的
# eg:
docker pull mysql
docker pull mysql:latest
docker pull mysql:5.7
4. 查看本地镜像列表
docker images
5. 安装镜像
docker run [OPTIONS] IMAGE_NAME [COMMAND] [ARG...]
# -it 以交互的方式运行容器(启动容器后进入容器)
# -d 后台运行容器(最常用的方式,会返回容器id)
# -p host_port:container_port 端口映射,将host_port(主机端口)和container_port(容器端口)映射
# -v host_dir:container_dir 文件映射,将host_dir(主机)和container_dir(容器)映射,文件必须存在,否则会被认为是目录,也可以直接就是目录
# --net=bridge 指定容器的网络连接类型,支持 bridge/host/none 3种类型(一般用不到)
docker network ls 即可查看容器支持的网络类型
# -w dir 指定容器的工作目录
# --name container_name 指定容器名称为 container_name
# --restart=always 容器自启动
# --restart=always # 表示容器退出时,docker会总是自动重启这个容器
# --restart=on-failure:3 # 表示容器的退出状态码非0(非正常退出),自动重启容器,3是自动重启的次数。超过3此则不重启
# --restart=no # 默认值,容器退出时,docker不自动重启容器
# --restart=unless-stopped # 表示容器退出时总是重启,但是不考虑docker守护进程运行时就已经停止的容器
# -e XXX="YYY" 指定环境变量
# eg:
docker run -d -p 3306:3306 -v ~/db/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql
6. 容器启动和自启动
# --restart=always 容器自启动
# --restart=always 表示容器退出时,docker会总是自动重启这个容器
# --restart=on-failure:3 表示容器的退出状态码非0(非正常退出),自动重启容器,3是自动重启的次数。超过3此则不重启
# --restart=no 默认值,容器退出时,docker不自动重启容器
# --restart=unless-stopped 表示容器退出时总是重启,但是不考虑docker守护进程运行时就已经停止的容器
6.1 启动关闭的容器
# 查看被关闭的容器
docker container ls -a
# 启动容器
docker container start ${container_name} # container_name: 容器名称
6.2 已经创建的容器,设置自启动
docker update --restart=always ${container_name} # container_name: 容器名称
6.3 查看运行中的容器的详细信息
docker inspect ${container_id}
eg:
docker inspect 23d249926910
上面的命令会得到一份json格式的内容
[
{
"Id": "23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b",
"Created": "2023-01-02T00:36:53.835208361Z",
"Path": "/bin/tini",
"Args": [
"--",
"/usr/local/bin/kibana-docker"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23111,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-01-02T00:36:54.291323242Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:1215bb66460ce6ba56571c497bd313ffc1e324a16ed748f2902ed5eea7c176ee",
"ResolvConfPath": "/var/snap/docker/common/var-lib-docker/containers/23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b/resolv.conf",
"HostnamePath": "/var/snap/docker/common/var-lib-docker/containers/23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b/hostname",
"HostsPath": "/var/snap/docker/common/var-lib-docker/containers/23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b/hosts",
"LogPath": "/var/snap/docker/common/var-lib-docker/containers/23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b/23d24992691025c29d534b80e7aaac359510681f3e71f64893bb8b93584a5d6b-json.log",
"Name": "/kibana",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "unconfined",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/home/adc/db/kibana/data:/usr/share/kibana/data",
"/home/adc/db/kibana/plugins:/usr/share/kibana/plugins"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "es-net",
"PortBindings": {
"5601/tcp": [
{
"HostIp": "",
"HostPort": "5601"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": true,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": [
"label=disable"
],
"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": null,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": null,
"ReadonlyPaths": null
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/snap/docker/common/var-lib-docker/overlay2/6dddac2b543b49f22a80e657f5408b2bd7f376080b290ddd1046533811ecb3a3-init/diff:/var/snap/docker/common/var-lib-docker/overlay2/503a10607f8a43c8dbb79b3a0bdb03ea4bd4f553e2ab697a720817ee652f3fb5/diff:/var/snap/docker/common/var-lib-docker/overlay2/dd3f6f3de8c31a3fe094f754f6ce36a0677aaff8d3d8ac7f42d2a9a0fb52c7fe/diff:/var/snap/docker/common/var-lib-docker/overlay2/ff5f57f9cdc254a755756f3344715b581640f5eaab317f33ad6b09603732663f/diff:/var/snap/docker/common/var-lib-docker/overlay2/d6516f92d3307f3169f2c3955291e6e7d4d7f010dcc784c9baf0590c89e2def7/diff:/var/snap/docker/common/var-lib-docker/overlay2/059d6511d747e6b8ce42bf309b2c77647dd3d72cbb83a9b9e3850514bbda461d/diff:/var/snap/docker/common/var-lib-docker/overlay2/f9375e7ba3c571bf0ed1ed9271552cf7c90b6e238986a6b8015359c8fbb37641/diff:/var/snap/docker/common/var-lib-docker/overlay2/fb8a34b45ec8715c7d23143b45ecdb0f9e261f62b5a464ad08ecfe715b454d06/diff:/var/snap/docker/common/var-lib-docker/overlay2/3e4a3b244e2499b9b69ca94a8ca2a7bd47419df6c4f0d5332556b62692b9bed3/diff:/var/snap/docker/common/var-lib-docker/overlay2/c9c2ad303f0d42900293e8da0f93ab9df9183f6b495347ebf4fe51863c9714e8/diff:/var/snap/docker/common/var-lib-docker/overlay2/885647d1389f65fd58e84a817a5bb3d00b6b2087bcf52b62f67a40b9db4bbd80/diff:/var/snap/docker/common/var-lib-docker/overlay2/a8c68b3967cb28a9cf709de80cef0389bf0f65905a8e3f253befb3025a01d939/diff:/var/snap/docker/common/var-lib-docker/overlay2/91cca123e147bc305e8ed75a4a0aa9571a1013b9d16d127c4e713291f35d8cb3/diff:/var/snap/docker/common/var-lib-docker/overlay2/6645778248de386df6394d0f76c1d0a962209e0d569beb70468f33890d947602/diff:/var/snap/docker/common/var-lib-docker/overlay2/7b6b43d0dc7ec008d44a96dbb26eef01c33daa38457bd0a951b60bad8b89a4fd/diff:/var/snap/docker/common/var-lib-docker/overlay2/0062dd7d1954159a35cc66db18184efc9f341525a3a5be9e98fa053458f11d1f/diff",
"MergedDir": "/var/snap/docker/common/var-lib-docker/overlay2/6dddac2b543b49f22a80e657f5408b2bd7f376080b290ddd1046533811ecb3a3/merged",
"UpperDir": "/var/snap/docker/common/var-lib-docker/overlay2/6dddac2b543b49f22a80e657f5408b2bd7f376080b290ddd1046533811ecb3a3/diff",
"WorkDir": "/var/snap/docker/common/var-lib-docker/overlay2/6dddac2b543b49f22a80e657f5408b2bd7f376080b290ddd1046533811ecb3a3/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/home/adc/db/kibana/data",
"Destination": "/usr/share/kibana/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/home/adc/db/kibana/plugins",
"Destination": "/usr/share/kibana/plugins",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "23d249926910",
"Domainname": "",
"User": "kibana",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5601/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"ELASTICSEARCH_URL=https://elasticsearch:9200",
"ELASTICSEARCH_HOSTS=https://elasticsearch:9200",
"ELASTICSEARCH_USERNAME=es_admin",
"ELASTICSEARCH_PASSWORD=HDLJD1lzsx--",
"PATH=/usr/share/kibana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ELASTIC_CONTAINER=true"
],
"Cmd": [
"/usr/local/bin/kibana-docker"
],
"Image": "kibana:8.5.3",
"Volumes": null,
"WorkingDir": "/usr/share/kibana",
"Entrypoint": [
"/bin/tini",
"--"
],
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "2022-12-05T12:09:50.062Z",
"org.label-schema.license": "Elastic License",
"org.label-schema.name": "Kibana",
"org.label-schema.schema-version": "1.0",
"org.label-schema.url": "https://www.elastic.co/products/kibana",
"org.label-schema.usage": "https://www.elastic.co/guide/en/kibana/reference/index.html",
"org.label-schema.vcs-ref": "93852c98d9e9902fe166302fae10bc8c5f3502fb",
"org.label-schema.vcs-url": "https://github.com/elastic/kibana",
"org.label-schema.vendor": "Elastic",
"org.label-schema.version": "8.5.3",
"org.opencontainers.image.created": "2022-12-05T12:09:50.062Z",
"org.opencontainers.image.documentation": "https://www.elastic.co/guide/en/kibana/reference/index.html",
"org.opencontainers.image.licenses": "Elastic License",
"org.opencontainers.image.revision": "93852c98d9e9902fe166302fae10bc8c5f3502fb",
"org.opencontainers.image.source": "https://github.com/elastic/kibana",
"org.opencontainers.image.title": "Kibana",
"org.opencontainers.image.url": "https://www.elastic.co/products/kibana",
"org.opencontainers.image.vendor": "Elastic",
"org.opencontainers.image.version": "8.5.3"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "fc40f95d899ba4778665c5de06af261df4d7a40a0903e2cca8eec64fb0ea5a78",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5601/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5601"
},
{
"HostIp": "::",
"HostPort": "5601"
}
]
},
"SandboxKey": "/run/snap.docker/netns/fc40f95d899b",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"es-net": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"23d249926910"
],
"NetworkID": "bc3bad8cb2d1659639cb877e56193896e500c524bd6d10f09a55aed46b211c4e",
"EndpointID": "d628132cf816715079c18a1626018f0a75301532c08c0c044dbb51f6ceef77d8",
"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
}
}
}
}
]
7. 进入容器
docker exec -it =${container_id} /bin/bash
# eg:
docker exec -it 463d8daef5a3 /bin/bash
8. 停止容器
docker stop ${container_id}
docker stop ${container_name}
# eg:
docker stop redis
9. 删除容器
docker rm ${container_id}
docker rm ${container_name}
# eg:
docker rm redis