码迷,mamicode.com
首页 > 其他好文 > 详细

Docker.容器管理

时间:2020-09-18 02:25:18      阅读:38      评论:0      收藏:0      [点我收藏+]

标签:out   mon   需要   sha   tom   直接   ted   tail   详细   

容器管理

0.前言

<镜花缘>
咱们世界也许也是在一个容器中...

0.环境

[root@VM_0_12_centos docker]# docker -v
Docker version 19.03.11, build 42x35exxfx

1.启动

1.0.新建并启动(基于镜像)

docker run -it  --rm -d -p 8888:8080 tomcat:8.0
  -i	:	交互式操作
  -t	:	终端
  -rm	:	容器退出后随之将其删除,可以避免浪费空间
  -p 	:	端口映射
  -d 	:	容器在后台运行
  -name	: 	容器名称
[root@VM_0_12_centos ~]# docker run -it hello-world /bin/bash
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown.

经验+1...
//这种情况,虽然报错了,但是容器创建成功了,
//但是STATUS:::Created,已经创建,但是未启动,
//这个演示镜像可能没有这么多不支持shell交互&bash交互...?

1.该容器使用的不支持/bin/bash,只有/bin/sh ,或者压根都没有提供这种shell接口!

#没有添加/bash/bash就可以了
[root@VM_0_12_centos ~]# docker run -d --name Hw_01 hello-world
378e29d0eeb9e150c113cb87e83777f984112c282b8e0d19e3e11ec81232a774
#"docker ps -a"查看这个容器{378e29d0eeb9e},发现COMMAND:::"/hello"
#再次run,将"/bin/bash"改为"/hello"成功.
[root@VM_0_12_centos ~]# docker run -it --name Hw_02 hello-world /hello

Hello from Docker!
This message shows that ...
...
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
...

//不同用途的镜像的启动区别?


1.1.启动

#docker start {容器Id}
[root@VM_0_12_centos ~]# docker start 378e29d0eeb9
378e29d0eeb9

1.2.启动.重启

#docker restart {容器Id}
[root@VM_0_12_centos ~]# docker restart 378e29d0eeb9
378e29d0eeb9

2.查看容器列表(不同状态)

2.0.命令介绍

docker ps [OPTIONS]
[OPTIONS]说明:
*	-a 			:显示所有的容器,包括未运行的。
* 	-f 			:根据条件过滤显示的内容。
* 	--format 	        :指定返回值的模板文件。
* 	-l 			:显示最近创建的容器。
* 	-n 			:列出最近创建的n个容器。
* 	--no-trunc 	:不截断输出。
* 	-q 			:静默模式,只显示容器编号。
* 	-s 			:显示总的文件大小。


2.1.所有的容器列表

#所有
[root@VM_0_12_centos ~]# docker ps -a
#格式化
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | "
NAMES | IMAGE |
Hw_02 | hello-world |
Hw_01 | hello-world |
...
#非缩写形式
[root@VM_0_12_centos ~]# docker ps --all

经验+1
"--option"为选项的全称?"-o"为选项的缩写?连"--"都缩写为了"-"...


2.2.筛选filter

#明明有这个名称{Hw_01}的容器,但是没有显示出来...
[root@VM_0_12_centos ~]# docker ps --filter "name=Hw_01"
#名称过滤=>{明明}有,但是不是所有...
[root@VM_0_12_centos ~]# docker ps -a --filter "name=Hw_01" --format "table {{.Names}} | {{.Image}} | "
NAMES | IMAGE |
Hw_01 | hello-world |
#标签过滤=>(打标签...docker tag...)
[root@VM_0_12_centos ~]# docker ps -a -f "label=color"
#状态过滤=>
[root@VM_0_12_centos ~]# docker ps -a -f status=running
[root@VM_0_12_centos ~]# docker ps -a -f "status=running"
[root@VM_0_12_centos ~]# docker ps -a -f exited=0

#在docker里面,下面的3条命令,是等价
#1.=>双引号{"}和{‘}
#2.一个空格{ }和多个空格{          }
[root@VM_0_12_centos ~]# docker ps -a --filter "name=Hw_01" 
[root@VM_0_12_centos ~]# docker ps -a --filter ‘name=Hw_01‘
[root@VM_0_12_centos ~]# docker       ps       -a          --filter ‘name=Hw_01‘

#固定的格式,需要遵循,比如"-f"
#错误示范↓
[root@VM_0_12_centos ~]# docker ps -a -f status = running
invalid argument "status" for "-f, --filter" flag: bad format of filter (expected name=value)
#正确格式↓
[root@VM_0_12_centos ~]# docker ps -a -f status=running

经验+2...


2.2.容器列表.正在运行

[root@VM_0_12_centos ~]# docker ps


2.3.容器列表.最近创建

#列出最近创建的5个容器
[root@VM_0_12_centos ~]# docker ps -a -n 5

3.开机运行容器

3.0.创建容器阶段.设置开机运行

#
[root@VM_0_12_centos ~]# docker run -d -it --name Hw_03 --restart=always hello-world
448fffc49921f7d89368f74dd79058fd3d430cb4a4284a4f21d8dc7f026c2421
#一直在尝试重启,哪怕真的起不来..."Restarting (0) 31 seconds ago"...它不知道它永远起不来,一下搞得好励志...
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | {{.Status}}|"
NAMES | IMAGE | STATUS|
Hw_03 | hello-world | Restarting (0) 31 seconds ago|
Hw_02 | hello-world | Exited (0) About an hour ago|
Hw_01 | hello-world | Exited (0) About an hour ago|
...
#想让已经Exited的{Hw_02}也励志一下...
[root@VM_0_12_centos ~]# docker update --restart=always Hw_02
Hw_02
#发现{Hw_02}励志已经晚了...说明励志要趁早...等你Exit的时候再励志就晚了...
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | {{.Status}}|"
NAMES | IMAGE | STATUS|
Hw_03 | hello-world | Restarting (0) 56 seconds ago|
Hw_02 | hello-world | Exited (0) About an hour ago|
Hw_01 | hello-world | Exited (0) About an hour ago|

励志+1...
经验+1...
//docker服务设置开机自动启动...


3.1.更改一个已经创建的容器

#试图停止{Hw_03}的无效励志...失败...
[root@VM_0_12_centos ~]# docker update --restart=no Hw_03
Hw_03
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | {{.Status}}|"
NAMES | IMAGE | STATUS|
Hw_03 | hello-world | Restarting (0) 5 seconds ago|
Hw_02 | hello-world | Exited (0) About an hour ago|
Hw_01 | hello-world | Exited (0) About an hour ago|
...

//搜到好几个都是"docker update --restart=no 容器名称",但是确实没有起作用...使用方式不对?
//又搜到一个....

#刚才搜到的docker命令没有起作用...又重新找到了一个...
[root@VM_0_12_centos ~]# docker container update --restart=no Hw_03
Hw_03
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | {{.Status}}|"
NAMES | IMAGE | STATUS|
Hw_03 | hello-world | Exited (0) 14 seconds ago|
Hw_02 | hello-world | Exited (0) About an hour ago|
Hw_01 | hello-world | Exited (0) About an hour ago|
...

拯救迷途少年{Hw_03} +1...
//故事不会告诉你们,拯救方式简单粗暴:Exited...


4.容器后台运行

4.1.创建阶段.后台运行

[root@VM_0_12_centos ~]# docker run -d --name Hw_04 hello-world
d2e22adac77f1e6399c890ed2a6d8416b3494a6405fc00afd80d188bf7affe6f

4.2.已经运行容器,并且正在与容器进行交互...

#先进去一个容器...
[root@VM_0_12_centos ~]# docker attach coredocker5c
#按下组合键:Ctrl+P+Q
read escape sequence
#要将这个组合键{Ctrl+P+Q}区别于{exit}
#查看刚才的交互...↓
[root@VM_0_12_centos ~]# docker logs -tf coredocker5c
...
2020-09-15T15:16:12.864678231Z dotner^Ht^H^H^H^H^H^Z
2020-09-15T15:16:16.101312196Z dotnet
...
#"dotner^Ht^H^H^H^H^H^Z","dotnet"都是我输入的...都让logs记录下来了...
#注意:组合键{Ctrl+C} = {exit}
[root@VM_0_12_centos ~]# docker attach coredocker5c
^Cinfo: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...

5.端口映射

#尝试将宿主机的12000端口,随机映射到容器一个端口...失败...
[root@VM_0_12_centos ~]# docker run -it -d -p 127.0.0.1::12000 --name core091501 coredocker3
3ae7086ccebf8bfb12337447fb8101c832b93ca65c29a4cfe76c70ce5842d5e1

经验+1...

#HOSTPORT:CONTAINERPORT
[root@VM_0_12_centos ~]# docker run -it -d -p 11105:11001 --name core091505 coredocker3
b1a61ae6a7707d5193ae325c9cc97bd5e5659a4be741489a5ca0d2b70165ff53
#查看某一个容器的端口映射
[root@VM_0_12_centos ~]# docker port core091505
11001/tcp -> 0.0.0.0:11105

//回过头来看下"docker ps -a".PORTS这一列...

6.删除容器

6.0.删除所有未使用的容器(不是处于运行状态)

[root@VM_0_12_centos ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
990f0eb42c97a4155d9dbc8dbf297726
...

6.1.删除.指定容器

#docker  rm  <容器ID/容器名字>
#试图删除一个正在运行的容器...失败...
[root@VM_0_12_centos ~]# docker rm core091505
Error response from daemon: You cannot remove a running container b1a61ae6a7707d5193ae325c9cc97bd5e5659a4be741489a5ca0d2b70165ff53. Stop the container before attempting removal or force remove
#强制删除一个正在运行的容器...成功...
[root@VM_0_12_centos ~]# docker rm -f core091505
core091505

7.查看容器详细信息

docker exec

#exec一个dotnet core容器...
[root@VM_0_12_centos ~]# docker exec -it 57eb5000e286 /bin/bash
root@57eb5000e286:/app# dotnet --list-sdks
root@57eb5000e286:/app# dotnet --list-runtimes
Microsoft.AspNetCore.App 3.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
...
#组合键{Ctrl+P+Q} =>
root@57eb5000e286:/app# read escape sequence
#不进入交互模式,直接获取一个信息...
[root@VM_0_12_centos ~]# docker exec coredocker7c dotnet --list-runtimes
Microsoft.AspNetCore.App 3.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

docker inspect

[root@VM_0_12_centos ~]# docker inspect coredocker7c       

//返回一个json格式的信息...↓

[
    {
        "Id": "a3ef00508650f8fd5236596da8356ac930bb81dc94cd74644e2740bd7293a20c",
        "Created": "2020-06-27T14:23:47.389045773Z",
        "Path": "dotnet",
        "Args": [
            "DockerCoreSample.dll"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6387,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-06-27T14:23:47.654927608Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        }
        ...
] 

8.设定容器名称

#不设定容器名称,docker使用的默认名字比较随意...比如{interesting_jennings}...
[root@VM_0_12_centos ~]# docker ps -a --format "table {{.Names}} | {{.Image}} | {{.Status}}|"
NAMES | IMAGE | STATUS|
interesting_jennings | coredocker3 | Up 3 minutes|
coredocker7c | coredocker1i | Up 2 months|
[root@VM_0_12_centos ~]# docker run --name Hw_01 --rm  hello-world

Hello from Docker!
...
#并且由于因为选项--rm,执行{docker ps -a}是看不到{Hw_01}这个容器的...
[root@VM_0_12_centos ~]# docker ps -a

摘抄文档

Docker.容器管理

标签:out   mon   需要   sha   tom   直接   ted   tail   详细   

原文地址:https://www.cnblogs.com/love-zf/p/13676556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!