【云原生】一文秒会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)


目录
相关文章
|
1天前
|
Cloud Native Linux 开发者
【Docker】Docker:解析容器化技术的利器与在Linux中的关键作用
【Docker】Docker:解析容器化技术的利器与在Linux中的关键作用
|
3天前
|
存储 Kubernetes Docker
Kubernetes(K8S)集群管理Docker容器(概念篇)
Kubernetes(K8S)集群管理Docker容器(概念篇)
|
4天前
|
存储 Ubuntu 安全
Docker容器常用命令
Docker容器常用命令
17 1
|
9天前
|
存储 运维 监控
构建高效稳定的Docker容器监控体系
【4月更文挑战第18天】 在现代微服务架构中,Docker容器已成为部署和运行应用的标准环境。随之而来的挑战是如何有效监控这些容器的性能与健康状况,确保系统的稳定性和可靠性。本文将探讨构建一个高效稳定的Docker容器监控体系的关键技术和方法,包括日志管理、性能指标收集以及异常检测机制,旨在为运维人员提供实用的指导和建议。
|
18天前
|
运维 Cloud Native 持续交付
构建未来:云原生架构在现代企业中的应用与挑战
【4月更文挑战第10天】 随着数字化转型的不断深入,企业对信息技术基础设施的要求日益提高。云原生架构作为一种新兴的设计理念和技术集合,以其灵活性、可扩展性和容错性,正在成为推动企业技术革新的关键力量。本文将探讨云原生技术的核心组件、实施策略以及面临的主要挑战,并分析如何通过采纳云原生架构来优化业务流程和提升服务效率。
|
18天前
|
Linux Docker 容器
docker 容器常用命令
docker 容器常用命令
13 0
|
18天前
|
Linux Shell 虚拟化
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
28 0
|
18天前
|
存储 Linux Shell
centos 部署docker容器 安装 、基本使用方法(一)
centos 部署docker容器 安装 、基本使用方法(一)
32 0
|
1月前
|
Java Go 开发者
Docker容器技术简介及其与Go语言的结合点
【2月更文挑战第23天】本文首先概述了Docker容器技术的核心概念和优势,接着探讨了Go语言与Docker容器技术的结合点。通过阐述Docker的轻量级、可移植性和版本控制等特性,以及Go语言在容器化应用中的优势,本文旨在说明两者结合能够实现更高效、灵活的应用开发和部署。
|
1月前
|
Oracle 关系型数据库 数据库