Docker的常用命令

目录

命令查询地址

1.帮助命令

2.镜像命令

        (1)查看所有本地主机上的镜像

        (2)搜索镜像

        (3)下载镜像

        (4)删除镜像

3.容器命令

        (1)新建容器并启动

        (2)查看当前正在运行的容器

        (3)退出容器

        (4)删除容器

        (5)启动和停止容器

4.常用的其他命令

5.常用可选集


命令查询地址

https://docs.docker.com/engine/reference/commandline/docker/

1.帮助命令

#显示docker的详细信息
docker version
#显示docker的系统信息,包括镜像和容器的数量
docker info
#帮助命令
docker 命令 --help

2.镜像命令

        (1)查看所有本地主机上的镜像

                docker images

#查看使用方法
[root@VM-8-5-centos ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs


[root@VM-8-5-centos ~]# docker images
REPOSITORY               TAG                IMAGE ID       CREATED         SIZE
#REPOSITORY        镜像的仓库源
#TAG               镜像的标签
#IMAGE ID          镜像的ID
#CREATED           镜像的创建时间
#SIZE              镜像的大小

        (2)搜索镜像

            docker search

#可选项
[root@VM-8-5-centos ~]# docker search --help

Usage:  docker search [OPTIONS] TERM

Search Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results
      --no-trunc        Don't truncate output

#基本使用
#搜索mysql
docker search mysql
#可选项的使用
--filter=STARS=300 搜索结果为STARS大于三百

        (3)下载镜像

                 docker pull

#可选项
[root@VM-8-5-centos ~]# docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Download an image from a registry

Aliases:
  docker image pull, docker pull

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output

#下载mysql
[root@VM-8-5-centos ~]# docker pull mysql   #与docker pull docker.io/library/mysql:latest 作用结果一样
Using default tag: latest             #不写tag默认下载最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete           #分层下载,docker image的核心,联合文件系统
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest        #真实地址


#指定版本下载,必须是存在的版本
docker pull mysql:5.7

        (4)删除镜像

                 docker rmi

#可选项
[root@VM-8-5-centos ~]# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Aliases:
  docker image rm, docker image remove, docker rmi

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents


#根据ID删除
docker rmi 镜像对应的IMAGEID
#根据镜像名删除
docker rmi -f 镜像名
#删除指定版本
docker rmi -f 镜像名:版本
#删除所有镜像
docker rmi -f $(docker images -aq)
#删除多个镜像
docker rmi -f 镜像ID 镜像ID 镜像ID 镜像ID ...

3.容器命令

        (1)新建容器并启动

                 docker run

#可选项
docker run --help
......

#参数说明
    --name="Name"       容器名字,用来区分容器
    -d                  后台方式运行
    -it                 使用交互方式运行
    -p                  指定容器的端口
        -p IP:主机端口:容器端口
        -p 主机端口:容器端口
        -p 容器端口
        容器端口
    -P                  随机指定端口


#示例
docker run -it centos /bin/sh      #以sh的方式启动并进入容器
docker run -it centos /bin/bash    #以bash的方式启动并进入容器,exit退出

        (2)查看当前正在运行的容器

                 docker ps 

#可选集
[root@VM-8-5-centos ~]# docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Aliases:
  docker container ls, docker container list, docker container ps, docker ps

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes


#查看当前正在运行的容器
docker ps
#查看曾经运行过的容器
docker ps -a
#查看最近创建的n个容器(N为数字)
docker ps -n=N
#只显示当前运行容器的编号
docker ps -q
#显示所有容器的编号
docker ps -aq

        (3)退出容器

exit            #关闭并退出
Ctrl+P+Q        #不关闭退出(bash启动)

        (4)删除容器

            docker rm 容器ID

#可选集
[root@VM-8-5-centos ~]# docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Aliases:
  docker container rm, docker container remove, docker rm

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container

#删除指定容器
docker rm 容器ID
docker rm -f 容器ID  #强制删除
#递归删除所有容器
docker rm -f $(docker ps -aq)
docker ps -aq|xargs docker rm       #删除所有容器,运行中的除外
docker ps -aq|xargs docker rm -f    #强制删除所有容器

        (5)启动和停止容器

docker start   容器ID        #启动容器
docker stop    容器ID        #停止当前正在运行的容器
docker restart 容器ID        #重启容器
docker kill    容器ID        #强制停止当前正在运行的容器

4.常用的其他命令

#后台启动,当没有提供服务的时候就立即停止。
docker run -d centos
#查看日志
docker logs
#查看最近十条日志
docker logs -f -t --tail 10 容器ID
    #参数说明
        -f      #动态显示日志
        -t      #显示的日志带有时间戳
        --tail N #显示N条日志

#查看容器中的进程信息
docker top 容器ID
#查看容器的详细信息
docker inspect 容器ID

#进入当前正在运行的容器
 docker exec -it  3e89dda75d52 /bin/bash  #进入容器后开启一个新的终端,可以执行操作
 docker attach 3e89dda75d52               #进入正在执行的终端,不会启动新的进程

#从容器内拷贝文件到主机
 docker cp 3e89dda75d52:/home/test.java /home

5.常用可选集


attach        Attach to a running container        #当前 she11 下 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          Ki11 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退出
1ogs          Fetch the 1ogs of a container            #输出当前容器日志信息



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值