一起学docker(二)| docker常用命令

Docker 常用命令

官网地址

一起学docker(二)| docker常用命令_第1张图片

常用命令
# 版本号
docker -v

# 版本信息
docker version

# 详细信息,包括镜像和容器数量
docker info

# 查看日志
docker logs [可选参数] 容器id
# -tf 格式化输出日志
# --tail number  指定输出行数

# 查看容器中进程信息
docker top 容器id

# 查看容器信息
docker inspect 容器id

# 进入容器  
# 方式一  进入容器后开启一个新的终端 常用 03351a0e8a6a为容器id
docker exec -it 03351a0e8a6a /bin/bash
# 方式二  进入容器正在执行的终端,不会启动新的进程
docker attach 容器id

# 从容器内拷贝文件到主机
docker cp 容器id:容器内路径 目的主机路径

# 查看CPU消耗
docker stats

# 帮助命令
docker 命令 --help
Docker进程相关命令
# 启动Docker服务
systemctl start docker

# 停止docker服务
systemctl stop docker

# 重启docker服务
systemctl restart docker

# 查看docker服务状态
systemctl status docker

# 设置开机启动docker服务
systemctl enable docker
Docker镜像相关命令
  • docker images -a 等同于 docker images 查看本地所有镜像

    [root@VM-4-9-centos ~]# docker images -a
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    hello-world   latest    9c7a54a9a43c   2 months ago   13.3kB
    [root@VM-4-9-centos ~]# docker images
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    hello-world   latest    9c7a54a9a43c   2 months ago   13.3kB
    
    # REPOSITORY 镜像的仓库源
    # TAG  镜像的标签
    # IMAGE ID  镜像的ID
    # CREATED  镜像的创建时间
    # SIZE 镜像的大小
    
  • docker images -q 查看所用镜像的ID

    [root@VM-4-9-centos ~]# docker images -q
    9c7a54a9a43c
    
  • docker search 镜像名称 搜索镜像(可选项 docker search 镜像名称 --filter=stars=1000 搜索stars大于1000的)

    [root@VM-4-9-centos ~]# docker search mysql
    NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mysql                           MySQL is a widely used, open-source relation…   14334     [OK]       
    mariadb                         MariaDB Server is a high performing open sou…   5472      [OK]       
    percona                         Percona Server is a fork of the MySQL relati…   617       [OK]       
    phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   838       [OK]
    
    [root@VM-4-9-centos ~]# docker search mysql --filter=stars=1000
    NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mysql     MySQL is a widely used, open-source relation…   14334     [OK]       
    mariadb   MariaDB Server is a high performing open sou…   5472      [OK] 
    
  • docker pull 镜像名称 从仓库拉取镜像到本地(可选项docker pull 镜像名称:版本号,下载指定版本镜像)

    [root@VM-4-9-centos ~]# docker pull mysql
    Using default tag: latest  # 不指定tag,默认下载最新的
    latest: Pulling from library/mysql
    49bb46380f8c: Pull complete   # 分层下载
    aab3066bbf8f: Pull complete 
    d6eef8c26cf9: Pull complete 
    0e908b1dcba2: Pull complete 
    480c3912a2fd: Pull complete 
    89a648ecb3cf: Pull complete 
    6313eed00780: Pull complete 
    668fe2d98404: Pull complete 
    d3f8a843b813: Pull complete 
    c80ab9fc8db5: Pull complete 
    1b8b6b073273: Pull complete 
    Digest: sha256:ea68e51ffe9b96fef6076f1218af11301aeaf13c6201e0ec9aaef5791d5ddc5d  # 签名
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest  # 真实地址
    
    [root@VM-4-9-centos ~]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    70e9ff4420fb: Pull complete 
    7ca4383b183f: Pull complete 
    3e282e7651b1: Pull complete 
    1ffa0e0ca707: Pull complete 
    6eb790cf6382: Pull complete 
    2b7ffc37d8e9: Pull complete 
    4393c12228b9: Pull complete 
    389d2c130d52: Pull complete 
    e5df3caef94c: Pull complete 
    5c6aa409290d: Pull complete 
    faa350980ea9: Pull complete 
    Digest: sha256:bd873931ef20f30a5a9bf71498ce4e02c88cf48b2e8b782c337076d814deebde
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7
    
  • docker rmi 镜像id 删除指定本地镜像(docker rmi docker images -q,删除本地所有镜像,docker images -q需使用反引号)

    [root@VM-4-9-centos ~]# docker rmi f6360852d654
    Untagged: mysql:latest
    Untagged: mysql@sha256:ea68e51ffe9b96fef6076f1218af11301aeaf13c6201e0ec9aaef5791d5ddc5d
    Deleted: sha256:f6360852d65467a8252391f24826648079893ea03e52d4d8ee5f07fcdeb6d25e
    Deleted: sha256:ad3dab4ebb00f07aab01d00494ca339ef1212031a4ceef20140a0fb72cd49091
    Deleted: sha256:eba8b08c5578a559a4e23bf2ecb33156cb6ce2760e23bd4e0392f845e3169991
    Deleted: sha256:a43949b645daf661a96f364ed1905652d5cef6accfd0cbebc65f31c7f90e1434
    Deleted: sha256:22c0afbd3e9b365a5341c82cb5992abbd3f8e6f3ec854a7b5e6bb6e327df725c
    Deleted: sha256:a377b511bb5e08a6157afd8a017efd62b58352e84014f143bcac27f825bc9ffd
    Deleted: sha256:955462aa9eb18d4be371268533cd06a08909a43fa9e8383171fd81df8e092223
    Deleted: sha256:7b935957c6b8cb4cfecb1109ffceacbf3b100d48c2c842bffbe667ada0d7bf51
    Deleted: sha256:798dc96d1cbbb087d228d7ee1542e81181d99c5feef7d646061fb1be60f0a600
    Deleted: sha256:ac5998110c5937efa17d9ab7a09324d73ed1b8626c623d4f079d73d92bb573a6
    Deleted: sha256:55cba8d2bd703353ff4e0958d6cf47d334cf2b7af665d18e7524d5e062d36a14
    Deleted: sha256:32f7f5f86853e4f33f21f8c84cb800dcad0b678f3571e5ef342cfbb0a369b103
    [root@VM-4-9-centos ~]# docker images
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    mysql         5.7       2be84dd575ee   5 weeks ago    569MB
    hello-world   latest    9c7a54a9a43c   2 months ago   13.3kB
    
    [root@VM-4-9-centos ~]# docker rmi `docker images -q`
    Untagged: mysql:5.7
    Untagged: mysql@sha256:bd873931ef20f30a5a9bf71498ce4e02c88cf48b2e8b782c337076d814deebde
    Deleted: sha256:2be84dd575ee2ecdb186dc43a9cd951890a764d2cefbd31a72cdf4410c43a2d0
    Deleted: sha256:6e73b9eb85de983b8f09dc8343a2f857446e3b8d6dfe524ef7229fa3a43429e9
    Deleted: sha256:0598435d4168d38c687dc6eddb5c400a4d61c7473ae0075199149d2a267aa343
    Deleted: sha256:c515cbd5c65ac47b8db1f823a1dc8b7dc6a2e48f71ce34afa982823a45638000
    Deleted: sha256:6fca8af01449e3945162148d6779eff3d6ad5a2c3b03a2047189a2a80fd515a6
    Deleted: sha256:e609b760ab615fc06e0f146d70bddd2237ef77492a09c70034d99dc418aedd78
    Deleted: sha256:fb81140926f6e013a31b6fbd4642f70b81a2f164bd7922eb4f25d85bfb379323
    Deleted: sha256:422c4721f357ead3754ea1b3a630d5334a93db6a5d3d41642778e50194e5e9b4
    Deleted: sha256:3356622c341e34d779294e5c450424a43bd6f55538a1dbbbdd30489f33315764
    Deleted: sha256:10213d78b510219f6cf8e143c99d29db6ba8dc6bea318ae9c812ea5948805b39
    Deleted: sha256:82afa988bdb62c2f462d0d88e709afaf7b529cc944ea5197633c875e2a70636f
    Deleted: sha256:616461b0543d6905f05f2b384bc403d268886c8845a6de09629a2b022388c830
    Error response from daemon: conflict: unable to delete 9c7a54a9a43c (must be forced) - image is being used by stopped container d50e9e447b71
    [root@VM-4-9-centos ~]# docker images
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    hello-world   latest    9c7a54a9a43c   2 months ago   13.3kB
    
Docker容器相关命令

前提是我们要有镜像

  • docker ps 查看正在运行的镜像

    • -a 查看所有容器
    • -n=? 显示最近创建的容器
    • -q 只显示容器编号
  • docker run [可选参数] images 创建并启动容器

    • -i:保持容器运行。通常与 -t 同时使用。加入it这两个参数后,容器创建后自动进入容器中,退出容器后,容器自动关闭

    • -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用

    • -d:以守护(后台)模式运行容器。创建一个容器在后台运行,需要使用docker exec 进入容器。退出后,容器不会关闭

    • -it:创建的容器一般称为交互式容器,-id 创建的容器一般称为守护式容器

      [root@VM-4-9-centos ~]# docker run --name=centos -it centos
      [root@03351a0e8a6a /]# ls
      bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
      [root@03351a0e8a6a /]# [root@VM-4-9-centos ~]# docker ps
      CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
      03351a0e8a6a   centos    "/bin/bash"   About a minute ago   Up About a minute             centos
      
    • –name:为创建的容器命名

    • -p:指定容器端口

      • -p ip:主机端口:容器端口
      • -p 主机端口:容器端口(常用)
      • -p 容器端口
    • -P:随机指定端口

  • 退出容器

    • exit 可能会导致容器停止
    • Ctrl+P+Q 不停止退出
  • docker rm 容器id 删除指定容器,正在运行的会删除失败,可以使用rm -f强制删除(docker rm docker ps -a,删除所有容器,docker ps -a需用反引号,等价于$(docker ps -a));也可以使用docker ps -a -q|xargs docker rm来删除所有的容器

    [root@VM-4-9-centos ~]# docker ps -aq
    d3022c1a8168
    4ac6fe74ce41
    f743e587dbc3
    e52492624ba1
    5e33977ca543
    3efb6728daaf
    ac4709df8a9b
    d50e9e447b71
    [root@VM-4-9-centos ~]# docker rm d3022c1a8168
    d3022c1a8168
    [root@VM-4-9-centos ~]# docker ps -aq
    4ac6fe74ce41
    f743e587dbc3
    e52492624ba1
    5e33977ca543
    3efb6728daaf
    ac4709df8a9b
    d50e9e447b71
    [root@VM-4-9-centos ~]# docker rm `docker ps -aq`
    4ac6fe74ce41
    f743e587dbc3
    e52492624ba1
    5e33977ca543
    3efb6728daaf
    ac4709df8a9b
    d50e9e447b71
    [root@VM-4-9-centos ~]# docker ps -aq
    [root@VM-4-9-centos ~]# 
    
  • docker start 容器iddocker restart 容器id 启动容器

  • docker stop 容器id 停止正在运行的指定容器

  • docker kill 容器id 强制停止指定容器

  • docker export 容器id > 文件名.tar 导出容器

    [root@VM-4-9-centos ~]# docker export eb92a5483eb2 > tomcat-test.tar
    [root@VM-4-9-centos ~]# ls
    cosfs.sh  dnspod.sh  tomcat-test.tar  txcdn.sh
    
  • cat 文件名.tar | docker import - 镜像用户/镜像名:版本号 导入容器

    [root@VM-4-9-centos ~]# docker images
    REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
    tomcat-j              1.0       f1349e14e065   6 hours ago     479MB
    mysql                 latest    f6360852d654   3 days ago      565MB
    redis                 latest    7e89539dd8bd   2 weeks ago     130MB
    tomcat                latest    95f422fe411b   2 weeks ago     475MB
    nginx                 latest    021283c8eb95   2 weeks ago     187MB
    hello-world           latest    9c7a54a9a43c   2 months ago    13.3kB
    portainer/portainer   latest    5f11582196a4   8 months ago    287MB
    centos                latest    5d0da3dc9764   22 months ago   231MB
    [root@VM-4-9-centos ~]# cat tomcat-test.tar  | docker import - jiang/tom:v2.0
    sha256:92c249ed74a48b5130d3296add051c5b029e18abe7c44c75942fbf599986e0df
    [root@VM-4-9-centos ~]# docker images
    REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
    jiang/tom             v2.0      92c249ed74a4   9 seconds ago   477MB
    tomcat-j              1.0       f1349e14e065   6 hours ago     479MB
    mysql                 latest    f6360852d654   3 days ago      565MB
    redis                 latest    7e89539dd8bd   2 weeks ago     130MB
    tomcat                latest    95f422fe411b   2 weeks ago     475MB
    nginx                 latest    021283c8eb95   2 weeks ago     187MB
    hello-world           latest    9c7a54a9a43c   2 months ago    13.3kB
    portainer/portainer   latest    5f11582196a4   8 months ago    287MB
    centos                latest    5d0da3dc9764   22 months ago   231MB
    [root@VM-4-9-centos ~]# 
    
小结
attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像

build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像

commit    Create a new image from a container changes   # 提交当前容器为新的镜像

cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中

create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器

diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化

events    Get real time events from the server          # 从 docker 服务获取容器实时事件

exec      Run a command in an existing container        # 在已存在的容器上运行命令

export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history   Show the history of an image                  # 展示一个镜像形成历史

images    List images                                   # 列出系统当前镜像

import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]

info      Display system-wide information               # 显示系统相关信息

inspect   Return low-level information on a container   # 查看容器详细信息

kill      Kill a running container                      # kill 指定 docker 容器

load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]

login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器

logout    Log out from a Docker registry server          # 从当前 Docker registry 退出

logs      Fetch the logs of a container                 # 输出当前容器日志信息

port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口

pause     Pause all processes within a container        # 暂停容器

ps        List containers                               # 列出容器列表

pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像

push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器

restart   Restart a running container                   # 重启运行的容器

rm        Remove one or more containers                 # 移除一个或者多个容器

rmi       Remove one or more images       # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]

run       Run a command in a new container              # 创建一个新的容器并运行一个命令

save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]

search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像

start     Start a stopped containers                    # 启动容器

stop      Stop a running containers                     # 停止容器

tag       Tag an image into a repository                # 给源中镜像打标签

top       Lookup the running processes of a container   # 查看容器中运行的进程信息

unpause   Unpause a paused container                    # 取消暂停容器

version   Show the docker version information           # 查看 docker 版本号

wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值

你可能感兴趣的:(docker,docker,容器,运维)