首页 > 其他分享 >docker——容器的基本操作

docker——容器的基本操作

时间:2024-03-19 20:24:45浏览次数:21  
标签:ago 容器 root 0.0 基本操作 docker minutes 80

docker 容器的基本操作

run

格式

docker run [选项] 镜像 [命令] [参数...]

选项

选项 解释
-d 后台运行
-i 交互模式
-t 分配一个伪终端
-p 设置端口
--rm 运行完命令后,删除容器
--name 指定名称
--dns 指定dns(默认dns与主机一致)

实例

  • 1.启动容器后自动终止容器
[root@hmm ~]#  docker run --name deamo1 centos
[root@hmm ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS                          PORTS                               NAMES
8204e6b2710f   centos    "/bin/bash"          About a minute ago   Exited (0) About a minute ago                                       deamo1
8949ff144990   httpd     "httpd-foreground"   2 hours ago          Exited (255) 17 minutes ago     0.0.0.0:80->80/tcp, :::80->80/tcp   testweb
[root@hmm ~]#
  • 2.启动容器并允许用户交互
[root@hmm ~]# docker run -it --name demo2 centos /bin/bash
[root@921dd07c6b6c /]#       //此时进入了容器,@符号后面为容器id
  • 3.启动容器并让其以守护进程的方法在后台运行
[root@hmm ~]# docker run -d -p 80:80 --name test httpd
20bfaecb17b66d81a003f990d1b9aa961b160bbc573164d89e8a6769275ccfee
[root@hmm ~]# docker ps -a |grep test
20bfaecb17b6   httpd     "httpd-foreground"   17 seconds ago   Up 16 seconds                   0.0.0.0:80->80/tcp, :::80->80/tcp   test
  • 4.启动容器并运行指定命令后删除容器
[root@hmm ~]# docker run --rm  centos echo 'bye bye'
bye bye
[root@hmm ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS                        PORTS                               NAMES
20bfaecb17b6   httpd     "httpd-foreground"   About a minute ago   Up About a minute             0.0.0.0:80->80/tcp, :::80->80/tcp   test
921dd07c6b6c   centos    "/bin/bash"          6 minutes ago        Exited (1) 3 minutes ago                                          demo2
8204e6b2710f   centos    "/bin/bash"          8 minutes ago        Exited (0) 8 minutes ago                                          deamo1
8949ff144990   httpd     "httpd-foreground"   2 hours ago          Exited (255) 24 minutes ago   0.0.0.0:80->80/tcp, :::80->80/tcp   testweb         //无新生成的容器
[root@hmm ~]#

create

格式

docker create [选项] 镜像 [命令] [参数...]

选项

语法,基本与run相同。只不过create 只是创建,并不启动。

实例

[root@hmm ~]# docker create --name new ubuntu 
\ad3aca1015887c81d297b79bd0bc8ff091197423fa3c467240dc0c7bc0aeea9f
[root@hmm ~]# docker ps -a |grep new
ad3aca101588   ubuntu    "/bin/bash"          10 seconds ago   Created                                                           new
[root@hmm ~]#

start

格式

docker start [选项] 容器

实例

[root@hmm ~]# docker start ad3aca101588
ad3aca101588

stop

格式

docker stop [选项] 容器

实例

[root@hmm ~]# docker stop ad3aca101588
ad3aca101588
[root@hmm ~]#

-t:等多少秒后停止容器
下面案例中,执行命令后需等待3秒后输出容器id,完成容器停止。
[root@hmm ~]# docker stop -t 3 ad3aca101588
ad3aca101588

pause|unpause

格式

docker pause|unpause 容器]

实例

[root@hmm ~]# docker pause 20bfaecb17b6
20bfaecb17b6
[root@hmm ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                        PORTS                               NAMES
ad3aca101588   ubuntu    "/bin/bash"          6 minutes ago    Exited (0) 3 minutes ago                                          new
20bfaecb17b6   httpd     "httpd-foreground"   14 minutes ago   Up 14 minutes (Paused)  //此处为挂起状态      0.0.0.0:80->80/tcp, :::80->80/tcp   test

[root@hmm ~]# docker unpause 20bfaecb17b6
20bfaecb17b6
[root@hmm ~]# docker ps -a |grep httpd
20bfaecb17b6   httpd     "httpd-foreground"   17 minutes ago   Up 17 minutes                 0.0.0.0:80->80/tcp, :::80->80/tcp   test

kill

格式

docker pause|unpause 容器

实例

[root@hmm ~]# docker kill ad3aca101588
Error response from daemon: Cannot kill container: ad3aca101588: container ad3aca1015887c81d297b79bd0bc8ff091197423fa3c467240dc0c7bc0aeea9f is not running
[root@hmm ~]#    //强制关闭一个正在运行的容器

批量开启和批量停止容器

rename

格式

docker rename 容器 容器名称

实例

ps

格式

选项

选项 解释
a 显示全部容器
f 条件过滤容器
n 显示最近创建的n个容器
l 显示最新创建的容器
q 只显示id
s 显示总文件大小

实例

  1. -f 筛选
  • id
[root@hmm ~]# docker  ps -a  
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                           PORTS                               NAMES
ad3aca101588   ubuntu    "/bin/bash"          36 minutes ago   Exited (0) 32 minutes ago                                            new
20bfaecb17b6   httpd     "httpd-foreground"   44 minutes ago   Up 44 minutes                    0.0.0.0:80->80/tcp, :::80->80/tcp   test
921dd07c6b6c   centos    "/bin/bash"          48 minutes ago   Exited (1) 46 minutes ago                                            demo2
8204e6b2710f   centos    "/bin/bash"          51 minutes ago   Exited (0) 51 minutes ago                                            deamo1
8949ff144990   httpd     "httpd-foreground"   3 hours ago      Exited (255) About an hour ago   0.0.0.0:80->80/tcp, :::80->80/tcp   testweb
[root@hmm ~]# docker  ps -a  -f id=a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
ad3aca101588   ubuntu    "/bin/bash"   36 minutes ago   Exited (0) 33 minutes ago             new
[root@hmm ~]# docker  ps -a  -f id=8
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@hmm ~]# docker  ps -a  -f id=82
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
8204e6b2710f   centos    "/bin/bash"   52 minutes ago   Exited (0) 52 minutes ago             deamo1
[root@hmm ~]#
  • name
[root@hmm ~]# docker  ps -a  -f name=demo
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
921dd07c6b6c   centos    "/bin/bash"   52 minutes ago   Exited (1) 49 minutes ago             demo2
[root@hmm ~]# docker  ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                           PORTS                               NAMES
ad3aca101588   ubuntu    "/bin/bash"          40 minutes ago   Exited (0) 36 minutes ago                                            new
20bfaecb17b6   httpd     "httpd-foreground"   48 minutes ago   Up 48 minutes                    0.0.0.0:80->80/tcp, :::80->80/tcp   test
921dd07c6b6c   centos    "/bin/bash"          53 minutes ago   Exited (1) 50 minutes ago                                            demo2
8204e6b2710f   centos    "/bin/bash"          55 minutes ago   Exited (0) 55 minutes ago                                            deamo1
8949ff144990   httpd     "httpd-foreground"   3 hours ago      Exited (255) About an hour ago   0.0.0.0:80->80/tcp, :::80->80/tcp   testweb
[root@hmm ~]#
  • status
    可用状态:created,restarting,running,removing,pausd,exited,dead
[root@hmm ~]# docker  ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                           PORTS                               NAMES
ad3aca101588   ubuntu    "/bin/bash"          40 minutes ago   Exited (0) 36 minutes ago                                            new
20bfaecb17b6   httpd     "httpd-foreground"   48 minutes ago   Up 48 minutes                    0.0.0.0:80->80/tcp, :::80->80/tcp   test
921dd07c6b6c   centos    "/bin/bash"          53 minutes ago   Exited (1) 50 minutes ago                                            demo2
8204e6b2710f   centos    "/bin/bash"          55 minutes ago   Exited (0) 55 minutes ago                                            deamo1
8949ff144990   httpd     "httpd-foreground"   3 hours ago      Exited (255) About an hour ago   0.0.0.0:80->80/tcp, :::80->80/tcp   testweb
[root@hmm ~]# docker  ps -a  -f status=exited
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                           PORTS                               NAMES
ad3aca101588   ubuntu    "/bin/bash"          41 minutes ago   Exited (0) 38 minutes ago                                            new
921dd07c6b6c   centos    "/bin/bash"          54 minutes ago   Exited (1) 51 minutes ago                                            demo2
8204e6b2710f   centos    "/bin/bash"          57 minutes ago   Exited (0) 57 minutes ago                                            deamo1
8949ff144990   httpd     "httpd-foreground"   3 hours ago      Exited (255) About an hour ago   0.0.0.0:80->80/tcp, :::80->80/tcp   testweb
[root@hmm ~]# docker  ps -a  -f status=running
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                               NAMES
20bfaecb17b6   httpd     "httpd-foreground"   54 minutes ago   Up 54 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   test
[root@hmm ~]#
  • label
[root@hmm ~]# docker ps -a -f label=hbkj
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
9a5a08615a61   ubuntu    "/bin/bash"   41 seconds ago   Up 40 seconds             suspicious_darwin
[root@hmm ~]# docker ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED             STATUS                         PORTS                               NAMES
3160a3da6b8d   ubuntu    "/bin/bash"          32 seconds ago      Up 31 seconds                                                      vigilant_ramanujan
9a5a08615a61   ubuntu    "/bin/bash"          48 seconds ago      Up 47 seconds                                                      suspicious_darwin
ad3aca101588   ubuntu    "/bin/bash"          47 minutes ago      Exited (0) 4 minutes ago                                           new
20bfaecb17b6   httpd     "httpd-foreground"   56 minutes ago      Up 56 minutes                  0.0.0.0:80->80/tcp, :::80->80/tcp   test
921dd07c6b6c   centos    "/bin/bash"          About an hour ago   Exited (1) 57 minutes ago                                          demo2
8204e6b2710f   centos    "/bin/bash"          About an hour ago   Exited (0) About an hour ago                                       deamo1
[root@hmm ~]# docker ps -a -f label=hb
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@hmm ~]#     //标签必须完全匹配才能筛选出来

inspect

格式

docker inspect [选项]

选项

实例

1.-s

[root@hmm ~]# docker inspect 20bfaecb17b6 >test1
[root@hmm ~]# docker inspect -s 20bfaecb17b6 >sizetest
[root@hmm ~]# diff test1  sizetest 
140a141,142
>         "SizeRw": 2,
>         "SizeRootFs": 167481474,

2.-f

[root@hmm ~]# docker container inspect 20bfaecb17b6 --format "{{.NetworkSettings}}"
{{ b44e5e255c9acf19f2236f113cc0cf74b449695205718ee2ab02bde9381e0f86 /var/run/docker/netns/b44e5e255c9a map[80/tcp:[{0.0.0.0 80} {:: 80}]] false  0 [] []} {3abde00f27a1324014ae1c9e48b74cba2a7db2f50ddb0f747f90d71c5d462d73 172.17.0.1  0 172.17.0.2 16  02:42:ac:11:00:02} map[bridge:0xc0003401c0]}
[root@hmm ~]# docker container inspect 20bfaecb17b6 --format "{{.NetworkSettings.Ports}}"
map[80/tcp:[{0.0.0.0 80} {:: 80}]]
[root@hmm ~]#

attach

格式

docker attach [选项] 容器

选项

选项 解释
-detach-keys string 覆盖后台运行容器的键顺序
--no-stdin 不连接标准输入
--sig-proxy 将接受的信息代理给(default true)

实例

//注意 使用attach进入容器后,exit退出时会强制停止所有服务,关闭容器
1.连接到运行的容器中,并退出

[root@hmm ~]# docker run -d --name topdemo ubuntu /usr/bin/top -b
fa1894bcd4e50457cfc24ef3cab083fd3bc7ce3ad608f6760532272d4ef3e530
[root@hmm ~]# docker attach topemo
Error response from daemon: No such container: topemo
[root@hmm ~]# docker attach topdemo


top - 12:10:04 up  1:43,  0 users,  load average: 0.00, 0.01, 0.05
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1982.6 total,   1198.0 free,    350.1 used,    434.5 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   1480.8 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     1 root      20   0    7168   1712   1260 R   0.0   0.1   0:00.07 top

top - 12:10:07 up  1:43,  0 users,  load average: 0.00, 0.01, 0.05
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1982.6 total,   1197.9 free,    350.1 used,    434.5 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   1480.8 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     1 root      20   0    7168   1712   1260 R   0.0   0.1   0:00.07 top^C
[root@hmm ~]# docker ps -a|grep topdemo
fa1894bcd4e5   ubuntu    "/usr/bin/top -b"    47 seconds ago      Exited (0) 11 seconds ago                                          topdemo
[root@hmm ~]#

exec

标签:ago,容器,root,0.0,基本操作,docker,minutes,80
From: https://www.cnblogs.com/humlogs/p/18083770

相关文章

  • 视频转换容器格式
    容器格式视频容器格式是一种封装格式,用于存储在单一文件中的多种类型的数据,这通常包括视频和音频轨道、元数据(比如标题、作者等信息)、字幕和其他可能的数据流。容器格式定义了如何封装这些数据,但它不定义这些数据的编码方式。编码由视频和音频编解码器决定,而容器格式负责存储编码......
  • Docker
    Docker简介Docker是一种轻量级的容器化平台,用于将应用程序和它们的依赖项打包到可移植的容器中,以便在任何地方都可以轻松部署和运行。Docker技术提供了一种在容器中封装应用程序的方法,使得应用程序可以在任何环境中以相同的方式运行,而无需担心环境差异和依赖项的问题。Docke......
  • mysql 主从配置(docker安装)
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/c4524269de0049249b27d88015f0cd2b.png想要获取pdf文件可以关注:点击百度文库->mysql主从......
  • Docker实时同步备份数据至阿里云盘
    随着互联网的快速发展网站数据的安全性变得越来越重要,一旦网站数据丢失或被损坏将对企业个人的运营产生重大影响。因此做好网站数据的备份工作至关重要,阿里云盘作为阿里巴巴旗下的云存储服务,具有安全高效便捷等特点是备份网站数据的理想选择,但是各大LinuxWindows服务器系统......
  • 安装docker-compose
    道客的安装路径已经不能用了!使用官方安装脚本自动安装安装命令如下:curl-fsSLhttps://get.docker.com|bash-sdocker--mirrorAliyun也可以使用国内daocloud一键安装命令:curl-sSLhttps://get.daocloud.io/docker|shUbuntuDocker安装DockerEngine-Community......
  • 如何保证mysql的高可用性?我们用docker来做个实践。
    前言前段时间写了一篇关于mysql双主复制的文章,在这个的基础上我们来做个高可用的实践。前期准备,需要安装好docker、docker-compose的运行环境。mysql如何实现双主复制?我们用docker来个做实践。-CSDN博客一、安装haproxy1、创建haproxy目录,执行如下命令。mkdir-p/docke......
  • mysql存储过程查询结果循环遍历 判断 赋值 游标等基本操作
    时间:2018-03-2617:58:45一、首先说下本篇博客所实现功能的背景和功能是怎样的:   背景:因为公司项目开始迁移新平台项目,所以以前的平台老数据以及订单信息需要拆分表,而且需要业务逻辑来分析以前的订单表,来拆分成另外的几个新表,包括增加新的流水分析,以及更新其他用户或者......
  • 在 Docker 容器中运行 ASP.NET Core 应用
    创建Docker支持生成Dockerfile文件在解决方案asp.netcore项目下右键添加Dockerfile支持、选择目标OS为Linux,生成Dockerfile文件#Seehttps://aka.ms/customizecontainertolearnhowtocustomizeyourdebugcontainerandhowVisualStudiousesthisDockerfiletobuild......
  • Ubuntu安装指定版本的docker
    Ubuntu安装指定版本的docker1、删除docker相关组件sudoapt-getautoremovedockerdocker-cedocker-enginedocker.iocontainerdrunc2、更新apt-getsudoapt-getupdate3、安装apt依赖包,用于通过HTTPS来获取仓库sudoapt-getinstallapt-transport-httpsca-cert......
  • 轻量化部署工具Docker:参数化启动容器与数据卷挂载的综合应用
    dockerrun中的常见参数以mysql安装为例子dockerrun-d\--namemysql\-p3306:3306\-eTZ=Asia/Shanghai\-eMYSQL_ROOT_PASSWORD=123456\mysql-d表示后台执行–name容器的名字-p宿主机和容器映射的端口-e环境变量的设置此处设置了时区和密码......