Docker&Kubernetes ❀ Docker镜像与容器相关操作基础命令详解
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Docker镜像与容器相关操作基础命令详解相关的知识,希望对你有一定的参考价值。
文章目录
1、Docker信息查询
1.1 版本信息
[root@localhost ~]# docker version
Client: Docker Engine - Community #客户端版本信息
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:17:04 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community #服务端版本信息
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:15:27 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0
1.2 详细信息
[root@localhost ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 2 #容器数量;
Running: 0
Paused: 0
Stopped: 2
Images: 1 #镜像数量;
Server Version: 20.10.5
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18.0-193.el8.x86_64 #宿主机内核版本;
Operating System: CentOS Linux 8 (Core) #宿主机操作系统;
OSType: linux #宿主机操作系统类型;
Architecture: x86_64 #宿主机体系结构;
CPUs: 4 #宿主机核数;
Total Memory: 3.664GiB #宿主机内存;
Name: localhost.localdomain #宿主机主机名称;
ID: APEZ:LIL2:ZACW:QKAG:6JXY:IGVV:ZKML:KJKP:J7T6:USVO:D2G3:536H
Docker Root Dir: /var/lib/docker #Docker根目录路径;
Debug Mode: false #Debug模式;
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors: #注册表镜使用为阿里源;
https://sta7qavr.mirror.aliyuncs.com/
Live Restore Enabled: false
1.3 帮助命令
[root@localhost ~]# docker --help
Usage: docker [OPTIONS] COMMAND
[root@localhost ~]# man docker
2、镜像命令
2.1 本地镜像查看
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
镜像仓库源 镜像标签 镜像ID 创建时间 镜像大小
hello-world latest bf756fb1ae65 14 months ago 13.3kB
- -a:列出本地所有镜像,包含中间映像层;
- -q:仅显示镜像ID;
- –digests:显示镜像摘要信息;
- –no-trunc:显示完整的镜像信息;
2.2 镜像搜索
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
名称 描述信息 点赞数量 官方推荐 是否自动安装
centos The official build of CentOS. 6439 [OK]
镜像搜索命令等同于前往https://hub.docker.com/网页内搜索镜像名称,其返回信息与Linux内执行命令结果一致,选择需要的镜像点击即可获取到对应的详细信息;
点击具体的centos镜像,网页内置了镜像爬取命令,复制到Linux系统执行即可完成镜像爬取,如果需要下载特定的centos版本,此时需要查看当前网页支持的特定centos版本具体为哪些;
网页下拉即可寻找到对应的版本,然后进行下载爬取即可,默认不选择版本即下载为最新版本(latest);
2.3 镜像拉取
[root@localhost ~]# docker pull centos #爬取最新版本的centos镜像;
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker pull centos:7.9.2009 #爬取下载centos 7.9.2009版本的centos镜像
#需要查看网页内特定的版本是否存在才可以下载,否则下载无效;
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 2 months ago 209MB
centos 7.9.2009 8652b9f0cb4c 3 months ago 204MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
2.4 镜像删除
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 2 months ago 209MB
centos 7.9.2009 8652b9f0cb4c 3 months ago 204MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
[root@localhost ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:7e02330c713f93b1d3e4c5003350d0dbe215ca269dd1d84a4abc577908344b30
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
3、容器命令
由于虚拟机存在部署慢等缺点,Linux开发了一种新的虚拟化技术:Linux 容器技术(Linux Containers),Linux容器部署模拟一个完整的操作系统,而是对进程进行隔离,有了容器就可以将软件运行所需的资源打包到一个隔离的容器中,容器与虚拟机不同,不需要捆绑一套完整的操作系统,只需要对软件工作所需的库资源进行设置即可,系统因此而变得高效与轻量;
3.1 新建并启动容器
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- –name:指定容器名称;
- -d:后台运行容器,并返回容器ID;
- -i:以交互模式运行容器;
- -t:为容器分配一个伪输入终端;
- -P:随机端口映射;
- -p:指定端口映射;
端口映射有四种格式:- IP:HOST PORT:CONTAINER PORT
- IP::CONTAINER PORT
- HOST PORT:CONTAINER PORT
- CONTAINER PORT
- –restart=always:容器自动开启,当docker服务down或restart时,容器自动完成启动;
- -h:设置容器主机名;
- –dns 114.114.114.114:指定容器的DNS服务器;
- –dns-search:DNS搜索设置;
- –add-host [hostname:IP]:注入主机名称与IP地址的映射关系,此信息写入/etc/hosts文件内;
- –rm:容器停止后自动删除;
[root@Redhat8 ~]# docker run -it --name centos-1 -d centos
abb0127a2e48f5db4c6f2ac591daa2c808f74d9cc6020c51b59357fdd74fe87f
[root@Redhat8 ~]# docker exec -it centos-1 /bin/bash
[root@abb0127a2e48 /]# exit
exit
3.2 列出所有运行的容器
[root@localhost ~]# docker run -it 300e315adb2f
[root@6b0a01d8c5e0 /]# #进入容器后,命令前缀回显容器ID;
[root@localhost ~]# docker ps #显示当前运行状态的容器;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
容器ID 镜像 命令 创建时间 运行状态 端口 名称
6b0a01d8c5e0 300e315adb2f "/bin/bash" About a minute ago Up About a minute pensive_brattain
- -a:列出当前所有运行过的容器,包含正在运行的容器;
- -l:列出最近创建的容器;
- -n:列出最近n个创建的容器;
- -q:只显示容器ID;
- –no-trunc:显示完整信息;
3.3 退出容器
[root@localhost ~]# docker run -it 300e315adb2f
[root@6b0a01d8c5e0 /]# exit #容器停止退出;
exit
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
364722e51f07 hello-world "/hello" Less than a second ago Exited (0) Less than a second ago silly_shockley
Ctrl+P+Q #容器不停止退出;
[root@localhost ~]# docker run -it --name test_zxc centos
[root@7283bd6f0645 /]# (此处按下键盘Ctrl+P+Q)[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7283bd6f0645 centos "/bin/bash" 30 seconds ago Up 29 seconds test_zxc
3.4 启动/重启/停止/杀死/删除/重新登录 容器
[root@Redhat8 ~]# docker
attach commit context diff export image info load logs node port push rm save service stats system trust version
build config cp events help images inspect login manifest pause ps rename rmi search stack stop tag unpause volume
builder container create exec history import kill logout network plugin pull restart run secret start swarm top update wait
[root@localhost ~]# docker start/restart/stop/kill/rm/attach 364722e51f07 #多个容器ID使用空格间隔;
364722e51f07
[root@localhost ~]# docker inspect 364722e51f07 #查看容器基本信息
[root@localhost ~]# docker logs 364722e51f07 #查看容器日志;
[root@localhost ~]# docker stats 364722e51f07 #查看容器所占系统资源;
[root@localhost ~]# docker exec 364722e51f07 #容器执行命令
[root@localhost ~]# docker exec -it 364722e51f07 /bin/bash #登入容器的bash;
以上是关于Docker&Kubernetes ❀ Docker镜像与容器相关操作基础命令详解的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法