设为首页 收藏本站
查看: 710|回复: 0

[经验分享] Docker day2 Docker基本命令

[复制链接]

尚未签到

发表于 2018-5-26 13:28:11 | 显示全部楼层 |阅读模式
Docker基本命令

镜像常用命令

· 命令列表
– docker images    //查看镜像列表
– docker history   //查看镜像制作历史
– docker inspect   //查看镜像底层信息
– docker pull      //下载镜像
– docker push      //上传镜像
– docker rmi       //删除本地镜像
– docker save      //镜像另存为tar包
– docker load      //使用tar包导入镜像
– docker search    //搜索镜像
– docker tag       //修改镜像名称和标签
docker images

· 查看镜像列表
– REPOSITORY  //镜像仓库名称
– TAG         //镜像标签
– IMAGE ID    //镜像ID
– CREATED     //创建时间
– SIZE        //大小
  · 镜像ID: Docker镜像通过镜像ID进行识别。镜像ID是一个64字符的十六进制的字符串。但是当我们运行镜像时,通常我们不会使用镜像ID来引用镜像,而是使用镜像名来引用。

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB
docker history

· 查看镜像历史
– 了解镜像制作过程
– 详细参考后面的dockerfile内容
[root@localhost ~]# docker history centos
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
358bf47a7a64        3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
fb2a47779ef8        3 weeks ago         /bin/sh -c #(nop)  LABEL name=CentOS Base Ima   0 B                 
99d067612410        3 weeks ago         /bin/sh -c #(nop) ADD file:7441d818786942af84   203.5 MB            
docker inspect

· 查看镜像底层信息
– 了解镜像环境变量、存储卷、标签等信息
[root@localhost ~]# docker inspect centos
… …
{
"Id": "sha256:980e0e4c79ec933406e467a296ce3b86685e6b42eed2f873745e6a91d718e37a",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2016-09-06T21:10:20.397787682Z",
"Container": "37446a1771cbec3e85b76d9159fd6a5a92743655cb92a65661e8a174bad81c7e“
… …
docker rmi

· 删除本地镜像
– 注意:启动容器时删除镜像会提示错误
[root@localhost ~]# docker rmi centos
Error response from daemon: conflict: unable to remove repository reference
"centos" (must force) - container 3daba799135c is using its referenced image
980e0e4c79ec
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB
busybox             latest              789355058656        7 weeks ago         1.129 MB
mysql               latest              82960a1161e0        14 months ago       383.4 MB
nginx               latest              affde4c9c317        14 months ago       181.4 MB
[root@localhost ~]# docker rmi busybox
Untagged: busybox:latest
Deleted: 789355058656ec2ca0a856283e2597852ccd444548c9ef4fcbf8c9c24a73e169
Deleted: 97d69bba9a9d8406055d0fdc38a7f00162823ee5ca3751fc71531cb210204ff9
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB
mysql               latest              82960a1161e0        14 months ago       383.4 MB
nginx               latest              affde4c9c317        14 months ago       181.4 MB

docker save|load

· 保存本地镜像另存为tar文件
– 方便其他人使用tar包导入镜像
[root@localhost ~]# docker save centos > centos.tar
· 使用tar包文件导入镜像
[root@localhost ~]# docker load < centos.tar
docker search busybox
  – 从官方源搜索镜像
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布

[root@localhost ~]# docker search busybox
NAME                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
busybox                     Busybox base image.                             1168      [OK]      
progrium/busybox                                                            66                   [OK]
hypriot/rpi-busybox-httpd   Raspberry Pi compatible Docker Image with ...   39                  
radial/busyboxplus          Full-chain, Internet enabled, busybox made...   17                   [OK]
hypriot/armhf-busybox       Busybox base image for ARM.                     8                    
armhf/busybox               Busybox base image.                             4                    
arm32v7/busybox             Busybox base image.                             3                    
prom/busybox                Prometheus Busybox Docker base images           2                    [OK]
armel/busybox               Busybox base image.                             2                    
s390x/busybox               Busybox base image.                             2                    
onsi/grace-busybox                                                          2                    
p7ppc64/busybox             Busybox base image for ppc64.                   2                    
aarch64/busybox             Busybox base image.                             2                    
arm32v6/busybox             Busybox base image.                             1                    
spotify/busybox             Spotify fork of https://hub.docker.com/_/b...   1                    
ppc64le/busybox             Busybox base image.                             1                    
i386/busybox                Busybox base image.                             1                    
concourse/busyboxplus                                                       0                    
cfgarden/garden-busybox                                                     0                    
trollin/busybox                                                             0                    
yauritux/busybox-curl       Busybox with CURL                               0                    
ggtools/busybox-ubuntu      Busybox ubuntu version with extra goodies       0                    [OK]
amd64/busybox               Busybox base image.                             0                    
ddn0/busybox                fork of official busybox                        0                    [OK]
arm64v8/busybox             Busybox base image.                             0  
docker tag

· 重命名镜像名称(复制)
docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB
[root@localhost ~]# docker tag centos v1
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
v1                  latest              358bf47a7a64        3 weeks ago         203.5 MB
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB
[root@localhost ~]# docker tag centos:latest v2:test
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
v1                  latest              358bf47a7a64        3 weeks ago         203.5 MB
v2                  test                358bf47a7a64        3 weeks ago         203.5 MB
centos              latest              358bf47a7a64        3 weeks ago         203.5 MB

容器常用命令

· 命令列表
– docker run         //运行容器
– docker ps          //查看容器列表
– docker stop        //关闭容器
– docker start       //启动容器
– docker restart     //重启容器
– docker attach|exec //进入容器
– docker inspect     //查看容器底层信息
– docker top         //查看容器进程列表
– docker rm          //删除容器
docker run

· 使用镜像启动容器
[root@localhost ~]# docker run -it nginx bash
root@065d2c789646:/# exit
· 使用镜像启动容器放在后台
[root@localhost ~]# docker run -itd centos bash
b8f218f2341c12655e6092d7d2e7fd5229824fdefce84075fb3a9569ebf82079
[root@localhost ~]#
docker ps

· 列出容器列表
– docker ps 查看正在运行的容器
– docker ps -a 查看所有容器列表
– docker ps -aq 仅显示容器id
  · 容器ID: 64字符的十六进制的字符串来定义容器ID,它是容器的唯一标识符。容器之间的交互是依靠容器ID识别的,由于容器ID的字符太长,我们通常只需键入容器ID的前4个字符即可。

[root@localhost ~]# docker  ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef58c24c92ef        centos              "bash"              15 seconds ago      Up 14 seconds                           insane_torvalds     
[root@localhost ~]# docker  ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
ef58c24c92ef        centos              "bash"              22 seconds ago      Up 21 seconds                                     insane_torvalds     
a4e1a0a525dc        centos              "bash"              4 minutes ago       Exited (137) 3 minutes ago                        tender_ptolemy      
065d2c789646        nginx               "bash"              16 minutes ago      Exited (0) 12 minutes ago                         dreamy_bardeen      
56e8898c730c        centos              "bash"              30 minutes ago      Exited (137) 26 minutes ago                       serene_brattain     
cd7551f2f4ad        nginx               "bash"              15 hours ago        Exited (127) 25 minutes ago                       cranky_hoover      
[root@localhost ~]# docker  ps -aq
ef58c24c92ef
a4e1a0a525dc
065d2c789646
56e8898c730c
cd7551f2f4ad

docker stop|start|restart

· 管理容器
– docker stop 关闭容器
– docker start 开启容器
– docker restart 重启容器
[root@localhost ~]# docker  ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef58c24c92ef        centos              "bash"              15 seconds ago      Up 14 seconds                           insane_torvalds     
[root@server0 ~]# docker stop ef5
docker attach|exec

· 进入容器
– docker attach 进入容器,exit会导致容器关闭
– docker exec 进入容器,退出时不会关闭容器
[root@localhost ~]# docker  ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef58c24c92ef        centos              "bash"              9 minutes ago       Up 40 seconds                           insane_torvalds     
065d2c789646        nginx               "bash"              25 minutes ago      Up 3 seconds        80/tcp, 443/tcp     dreamy_bardeen  
[root@localhost ~]# docker attach ef5
[root@ef58c24c92ef /]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
065d2c789646        nginx               "bash"              28 minutes ago      Up 3 minutes        80/tcp, 443/tcp     dreamy_bardeen      
[root@localhost ~]# docker exec -it 065d bash
root@065d2c789646:/# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
065d2c789646        nginx               "bash"              29 minutes ago      Up 4 minutes        80/tcp, 443/tcp     dreamy_bardeen      
[root@localhost ~]#
docker inspect

· 查看容器底层信息
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
065d2c789646        nginx               "bash"              31 minutes ago      Up 6 minutes        80/tcp, 443/tcp     dreamy_bardeen    ntic_bard
[root@localhost ~]# docker inspect 065d
Id": "065d2c789646eb6c564f21afdace2303c04f123ed64a8e71fcbcd9c014544389",
"Created": "2017-12-26T01:52:15.761726948Z",
"Path": "bash",
"Args": [],
"State": {
........
docker top

· 查看容器进程列表
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
065d2c789646        nginx               "bash"              31 minutes ago      Up 6 minutes        80/tcp, 443/tcp     dreamy_bardeen    ntic_bard
[root@localhost ~]# docker  top 065d
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5493                13925               0                   10:17               pts/4               00:00:00            bash
docker rm

· 删除容器
– 注意,删除正在运行的容器时会提示错误,先关闭容器,才能删除。
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
ef58c24c92ef        centos              "bash"              24 minutes ago      Exited (0) 12 minutes ago                         insane_torvalds     
065d2c789646        nginx               "bash"              40 minutes ago      Up 15 minutes                 80/tcp, 443/tcp     dreamy_bardeen      
56e8898c730c        centos              "bash"              54 minutes ago      Exited (137) 50 minutes ago                       serene_brattain     
cd7551f2f4ad        nginx               "bash"              16 hours ago        Exited (127) 50 minutes ago                       cranky_hoover      
[root@localhost ~]# docker  rm 065d
Error response from daemon: Cannot destroy container 065d: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error: failed to remove containers: [065d]
[root@localhost ~]# docker rm 56e8
56e8
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
ef58c24c92ef        centos              "bash"              25 minutes ago      Exited (0) 13 minutes ago                         insane_torvalds     
065d2c789646        nginx               "bash"              42 minutes ago      Up 16 minutes                 80/tcp, 443/tcp     dreamy_bardeen      
cd7551f2f4ad        nginx               "bash"              16 hours ago        Exited (127) 51 minutes ago                       cranky_hoover      
[root@localhost ~]#

DSC0000.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-481402-1-1.html 上篇帖子: CentOS7(1708)制作docker镜像之后,开启sshd服务失败的处理办法 下篇帖子: 【系列6】使用Dockerfile创建带LAMP的Centos Docker镜像
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表