jonvi 发表于 2018-5-26 13:28:11

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来引用镜像,而是使用镜像名来引用。

# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            latest            358bf47a7a64      3 weeks ago         203.5 MB
docker history

· 查看镜像历史
– 了解镜像制作过程
– 详细参考后面的dockerfile内容
# 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

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

· 删除本地镜像
– 注意:启动容器时删除镜像会提示错误
# docker rmi centos
Error response from daemon: conflict: unable to remove repository reference
"centos" (must force) - container 3daba799135c is using its referenced image
980e0e4c79ec
# 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
# docker rmi busybox
Untagged: busybox:latest
Deleted: 789355058656ec2ca0a856283e2597852ccd444548c9ef4fcbf8c9c24a73e169
Deleted: 97d69bba9a9d8406055d0fdc38a7f00162823ee5ca3751fc71531cb210204ff9
# 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包导入镜像
# docker save centos > centos.tar
· 使用tar包文件导入镜像
# docker load < centos.tar
docker search busybox
  – 从官方源搜索镜像
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布

# docker search busybox
NAME                        DESCRIPTION                                     STARS   OFFICIAL   AUTOMATED
busybox                     Busybox base image.                           1168            
progrium/busybox                                                            66                  
hypriot/rpi-busybox-httpd   Raspberry Pi compatible Docker Image with ...   39                  
radial/busyboxplus          Full-chain, Internet enabled, busybox made...   17                  
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                  
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                  
amd64/busybox               Busybox base image.                           0                  
ddn0/busybox                fork of official busybox                        0                  
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
# docker tag centos v1
# 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
# docker tag centos:latest v2:test
# 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

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

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

# dockerps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
ef58c24c92ef      centos            "bash"            15 seconds ago      Up 14 seconds                           insane_torvalds   
# dockerps -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      
# dockerps -aq
ef58c24c92ef
a4e1a0a525dc
065d2c789646
56e8898c730c
cd7551f2f4ad

docker stop|start|restart

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

· 进入容器
– docker attach 进入容器,exit会导致容器关闭
– docker exec 进入容器,退出时不会关闭容器
# dockerps
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
# docker attach ef5
# exit
exit
# 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      
# docker exec -it 065d bash
root@065d2c789646:/# exit
exit
# 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      
#
docker inspect

· 查看容器底层信息
# 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
# docker inspect 065d
Id": "065d2c789646eb6c564f21afdace2303c04f123ed64a8e71fcbcd9c014544389",
"Created": "2017-12-26T01:52:15.761726948Z",
"Path": "bash",
"Args": [],
"State": {
........
docker top

· 查看容器进程列表
# 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
# dockertop 065d
UID               PID               PPID                C                   STIME               TTY               TIME                CMD
root                5493                13925               0                   10:17               pts/4               00:00:00            bash
docker rm

· 删除容器
– 注意,删除正在运行的容器时会提示错误,先关闭容器,才能删除。
# 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      
# dockerrm 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:
# docker rm 56e8
56e8
# 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      
#

页: [1]
查看完整版本: Docker day2 Docker基本命令