Docker day2 Docker基本命令

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來引用鏡像,而是使用鏡像名來引用。mysql

[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官方發佈nginx

[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個字符便可。sql

[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 ~]#

Docker day2 Docker基本命令

相關文章
相關標籤/搜索