【云原生】一文秒会Docker容器企业化管理

简介: 【云原生】一文秒会Docker容器企业化管理


1.创建容器

1.前台运行创建

注意:下面是创建容器并进入容器,当exit退出容器后,容器就不在运行,需要使用-d选项,后台运行

1. [root@localhost ~]# docker run -i -t centos:7 /bin/bash
2. [root@4af91bbbc860 /]# ps -ef
3. UID         PID   PPID  C STIME TTY          TIME CMD
4. root          1      0  0 03:19 pts/0    00:00:00 /bin/bash
5. root         15      1  0 03:19 pts/0    00:00:00 ps -ef
6. [root@4af91bbbc860 /]# exit
7. exit
8. [root@localhost ~]# docker ps -a
9. CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS                     PORTS     NAMES
10. 4af91bbbc860   centos:7   "/bin/bash"   7 minutes ago   Exited (0) 8 seconds ago             naughty_meitner
11. 
12. 选项:
13. -name:指定容器名称,默认是随机名字
14. -i:创建一个交互式容器
15. -t:为容器分配一个伪终端,绑定到容器的标准输入上
16. -d:后台运行容器
2.后台运行创建
1. [root@localhost ~]# docker run -itd --name centos7-1 centos:7 /bin/bash
2. 08b6e56e54f2d645601cd71020a922c7b5f916725401b7408e358a782a965284
3. [root@localhost ~]# docker ps -a
4. CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS                          PORTS     NAMES
5. 08b6e56e54f2   centos:7   "/bin/bash"   2 seconds ago   Up 2 seconds                              centos7-1
6. 4af91bbbc860   centos:7   "/bin/bash"   8 minutes ago   Exited (0) About a minute ago       naughty_meitner
7. 
8. 选项:
9. -name:指定容器名称,默认是随机名字
10. -i:创建一个交互式容器
11. -t:为容器分配一个伪终端,绑定到容器的标准输入上
12. -d:后台运行容器

2.查看容器

1. [root@localhost ~]# docker ps --help
2. 
3. Usage:  docker ps [OPTIONS]
4. 
5. List containers
6. 
7. Aliases:
8.   docker container ls, docker container list, docker container ps, docker ps
9. 
10. Options:
11.   -a, --all             Show all containers (default shows just running)    #查看所有容器 默认之查看在运行的容器
12.   -f, --filter filter   Filter output based on conditions provided
13.       --format string   Format output using a custom template:
14.                        'table':            Print output in table format
15.                         with column headers (default)
16.                        'table TEMPLATE':   Print output in table format
17.                         using the given Go template
18.                        'json':             Print in JSON format
19.                        'TEMPLATE':         Print output using the given Go
20.                         template.
21.                         Refer to https://docs.docker.com/go/formatting/ for
22.                         more information about formatting output with templates
23.   -n, --last int        Show n last created containers (includes all            #查看多少行容器
24.                         states) (default -1)
25.   -l, --latest          Show the latest created container (includes all states)     #查看最近创建的容器
26.       --no-trunc        Don't truncate output       #显示完整ID号
27.   -q, --quiet           Only display container IDs      #只显示ID号
28.   -s, --size            Display total file sizes    #显示容器大小
1.查看所有的容器
1. [root@localhost ~]# docker ps -a
2. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
3. 08b6e56e54f2   centos:7   "/bin/bash"   27 minutes ago   Up 27 minutes                         centos7-1
4. 4af91bbbc860   centos:7   "/bin/bash"   35 minutes ago   Exited (0) 28 minutes ago             naughty_meitner
2.查看容器的ID

注:可以用于删除容器时,过滤

1. [root@localhost ~]# docker ps -q
2. 08b6e56e54f2

3.管理容器

1.开启容器
1. [root@localhost ~]# docker start 4af91bbbc860
2. 4af91bbbc860
3. [root@localhost ~]# docker ps -a
4. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
5. 08b6e56e54f2   centos:7   "/bin/bash"   30 minutes ago   Up 30 minutes             centos7-1
6. 4af91bbbc860   centos:7   "/bin/bash"   38 minutes ago   Up 1 second               naughty_meitner
2.关闭容器
1. [root@localhost ~]# docker stop 08b6e56e54f2
2. 08b6e56e54f2
3. [root@localhost ~]# docker ps -a
4. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
5. 08b6e56e54f2   centos:7   "/bin/bash"   30 minutes ago   Exited (137) 10 seconds ago             centos7-1
6. 4af91bbbc860   centos:7   "/bin/bash"   39 minutes ago   Up 53 seconds                           naughty_meitner
3.强制关闭容器
1. [root@localhost ~]# docker kill 4af91bbbc860
2. 4af91bbbc860
3. [root@localhost ~]# docker ps -a
4. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
5. 08b6e56e54f2   centos:7   "/bin/bash"   31 minutes ago   Exited (137) 51 seconds ago             centos7-1
6. 4af91bbbc860   centos:7   "/bin/bash"   39 minutes ago   Exited (137) 1 second ago               naughty_meitner
4.重启容器
1. [root@localhost ~]# docker restart 4af91bbbc860
2. 4af91bbbc860
3. [root@localhost ~]# docker ps -a
4. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                            PORTS     NAMES
5. 08b6e56e54f2   centos:7   "/bin/bash"   32 minutes ago   Exited (137) About a minute ago             centos7-1
6. 4af91bbbc860   centos:7   "/bin/bash"   40 minutes ago   Up 7 seconds                                naughty_meitner

4.查看容器信息

1.查看所有信息
[root@localhost ~]# docker inspect 4af91bbbc860
2.查看容器IP
1. [root@localhost ~]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 4af91bbbc860
2. 172.17.0.2
3.查看PID
1. [root@localhost ~]# docker inspect -f '{{.State.Pid}}' 4af91bbbc860
2. 72633

5.进入容器

注意:进入当前的伪终端,如果exit退出之后,当前的容器也会随之关闭,因为当前只存在一个终端。

1.进入当前的伪终端中
1. [root@localhost ~]# docker attach 4af91bbbc860
2. [root@4af91bbbc860 /]# exit
3. exit
4. [root@localhost ~]# docker ps -a
5. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                             PORTS     NAMES
6. 08b6e56e54f2   centos:7   "/bin/bash"   42 minutes ago   Exited (137) 11 minutes ag     o             centos7-1
7. 4af91bbbc860   centos:7   "/bin/bash"   50 minutes ago   Exited (0) 5 seconds ago                     naughty_meitner
2.创建一个新的伪终端并进入
1. [root@localhost ~]# docker exec -it 4af91bbbc860 /bin/bash
2. [root@4af91bbbc860 /]# exit
3. exit
4. [root@localhost ~]# docker ps -a
5. CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
6. 08b6e56e54f2   centos:7   "/bin/bash"   43 minutes ago   Exited (137) 13 minutes ago             centos7-1
7. 4af91bbbc860   centos:7   "/bin/bash"   51 minutes ago   Up 28 seconds                           naughty_meitner

6.导出\导入容器

1.导出容器

将当前容器导出到本地

1. [root@localhost ~]# docker export 4af91bbbc860 > centos7.tar
2. [root@localhost ~]# ls
3. anaconda-ks.cfg  centos7.tar  original-ks.cfg
2.导入容器

将当前容器导入为竟镜像,然后再基于这个镜像创建容器

1. [root@localhost ~]# cat centos7.tar | docker import - centos7:test
2. sha256:d0373e6a70dc1413fa992d11c40b9260fe742db22196429835d33b9cc3eb2135
3. [root@localhost ~]# docker images
4. REPOSITORY                                          TAG       IMAGE ID       CREATED          SIZE
5. centos7                                             test      d0373e6a70dc   11 seconds ago   204MB

7.删除容器

1.删除单个容器
1. [root@localhost ~]# docker rm -f 4af91bbbc860
2. 
3. 选项:
4. -f:强制删除
2.删除多个容器
[root@localhost ~]# docker rm -f 4af91bbbc860  08b6e56e54f2
3.删除全部容器
[root@localhost ~]# docker rm -f  $(docker ps -aq)


目录
相关文章
|
8天前
|
Prometheus 监控 Cloud Native
如何在Docker容器中监控和管理应用程序的性能?
如何在Docker容器中监控和管理应用程序的性能?
326 182
|
8天前
|
Prometheus Kubernetes 监控
如何在Kubernetes环境下使用第三方监控系统监控Docker容器性能?
如何在Kubernetes环境下使用第三方监控系统监控Docker容器性能?
309 145
|
6天前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
100 5
|
7天前
|
监控 Linux 调度
【赵渝强老师】Docker容器的资源管理机制
本文介绍了Linux CGroup技术及其在Docker资源管理中的应用。通过实例演示了如何利用CGroup限制应用程序的CPU、内存和I/O带宽使用,实现系统资源的精细化控制,帮助理解Docker底层资源限制机制。
|
存储 Cloud Native 数据处理
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
本文整理自阿里云资深技术专家、Apache Flink PMC 成员梅源在 Flink Forward Asia 新加坡 2025上的分享,深入解析 Flink 状态管理系统的发展历程,从核心设计到 Flink 2.0 存算分离架构,并展望未来基于流批一体的通用增量计算方向。
163 0
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
|
2月前
|
运维 监控 Cloud Native
从本土到全球,云原生架构护航灵犀互娱游戏出海
本文内容整理自「 2025 中企出海大会·游戏与互娱出海分论坛」,灵犀互娱基础架构负责人朱晓靖的演讲内容,从技术层面分享云原生架构护航灵犀互娱游戏出海经验。
302 15
|
2月前
|
运维 监控 Cloud Native
从本土到全球,云原生架构护航灵犀互娱游戏出海
内容整理自「 2025 中企出海大会·游戏与互娱出海分论坛」,灵犀互娱基础架构负责人朱晓靖的演讲内容,从技术层面分享云原生架构护航灵犀互娱游戏出海经验。
|
6天前
|
人工智能 Kubernetes Cloud Native
Higress(云原生AI网关) 架构学习指南
Higress 架构学习指南 🚀写在前面: 嘿,欢迎你来到 Higress 的学习之旅!
94 0
|
6月前
|
运维 Cloud Native 测试技术
极氪汽车云原生架构落地实践
随着极氪数字业务的飞速发展,背后的 IT 技术也在不断更新迭代。极氪极为重视客户对服务的体验,并将系统稳定性、业务功能的迭代效率、问题的快速定位和解决视为构建核心竞争力的基石。