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

目录

​编辑

1.创建容器

1.前台运行创建

2.后台运行创建

2.查看容器

1.查看所有的容器

2.查看容器的ID

3.管理容器

1.开启容器

2.关闭容器

3.强制关闭容器

4.重启容器

4.查看容器信息

1.查看所有信息

2.查看容器IP

3.查看PID

5.进入容器

1.进入当前的伪终端中

2.创建一个新的伪终端并进入

6.导出\导入容器

1.导出容器

2.导入容器

7.删除容器

1.删除单个容器

2.删除多个容器

3.删除全部容

1.创建容器

1.前台运行创建

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

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

2.查看容器

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

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

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

3.管理容器

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

4.查看容器信息

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

5.进入容器

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

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

6.导出\导入容器

1.导出容器

将当前容器导出到本地

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

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

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

7.删除容器

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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服bug

curry.30

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值