2022-05-25
运维
00
请注意,本文编写于 836 天前,最后修改于 110 天前,其中某些信息可能已经过时。

目录

基础命令
镜像命令
容器命令
常用的其他命令

Docker常用命令

基础命令

docker version #查看docker的版本信息 docker info #查看docker的系统信息,包括镜像和容器的数量 docker 命令 --help #帮助命令(可查看可选的参数) docker COMMAND --help

命令的帮助文档地址:https://docs.docker.com/engine/reference/commandline/docker/

镜像命令

1.docker images 查看本地主机的所有镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB #解释: 1.REPOSITORY 镜像的仓库源 2.TAG 镜像的标签 3.IMAGE ID 镜像的id 4.CREATED 镜像的创建时间 5.SIZE 镜像的大小 # 可选参数 -a/--all 列出所有镜像 -q/--quiet 只显示镜像的id

2.docker search 搜索镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10308 [OK] mariadb MariaDB is a community-developed fork of MyS… 3819 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 754 [OK] percona Percona Server is a fork of the MySQL relati… 517 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 86 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79 centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK] #可选参数 Search the 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 (default 25) --no-trunc Don't truncate output #搜索收藏数大于3000的镜像 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10308 [OK] mariadb MariaDB is a community-developed fordockerk of MyS… 3819 [OK]

3.docker pull 镜像名[

] 下载镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql Using default tag: latest #如果不写tag默认就是latest latest: Pulling from library/mysql 6ec7b7d162b2: Pull complete #分层下载,docker image的核心-联合文件系统 fedd960d3481: Pull complete 7ab947313861: Pull complete 64f92f19e638: Pull complete 3e80b17bff96: Pull complete 014e976799f9: Pull complete 59ae84fee1b3: Pull complete ffe10de703ea: Pull complete 657af6d90c83: Pull complete 98bfb480322c: Pull complete 6aa3859c4789: Pull complete 1ed875d851ef: Pull complete Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 #签名 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #下载来源的真实地址 #docker pull mysql等价于docker pull docker.io/library/mysql:latest

指定版本下载

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 6ec7b7d162b2: Already exists fedd960d3481: Already exists 7ab947313861: Already exists 64f92f19e638: Already exists 3e80b17bff96: Already exists 014e976799f9: Already exists 59ae84fee1b3: Already exists 7d1da2a18e2e: Pull complete 301a28b700b9: Pull complete 529dc8dbeaf3: Pull complete bc9d021dc13f: Pull complete Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7

4.docker rmi 删除镜像

#1.删除指定的镜像id [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id #2.删除多个镜像id [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id 镜像id 镜像id #3.删除全部的镜像id [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f $(docker images -aq)

容器命令

如拉取一个centos镜像

docker pull centos 运行容器的命令说明: docker run [可选参数] image #参数说明 --name="名字" 指定容器名字 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容 -p 指定容器的端口 ( -p ip:主机端口:容器端口 配置主机端口映射到容器端口 -p 主机端口:容器端口 -p 容器端口 ) -P 随机指定端口(大写的P)

运行并进入容器centos

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it centos /bin/bash [root@bd1b8900c547 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

退出容器命令:

#exit 停止并退出容器(后台方式运行则仅退出) #Ctrl+P+Q 不停止容器退出 #docker attach [container_name]进入容器 再次进入容器。 container_id 也可以 [root@bd1b8900c547 /]# exit exit [root@iZwz99sm8v95sckz8bd2c4Z ~]#

列出运行过的容器命令:

#docker ps # 列出当前正在运行的容器 -a # 列出所有容器的运行记录 -n=? # 显示最近创建的n个容器 -q # 只显示容器的编号 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bca129320bb5 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago optimistic_shtern bd1b8900c547 centos "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago cool_tesla cf6adbf1b506 bf756fb1ae65 "/hello" 5 hours ago Exited (0) 5 hours ago optimistic_darwin

删除容器命令:

docker rm 容器id #删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f docker rm -f $(docker ps -aq) #删除所有的容器 docker ps -a -q|xargs docker rm #删除所有的容器

启动和停止容器命令:

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

常用的其他命令

日志的查看

docker logs -tf 容器id docker logs --tail number 容器id #num为要显示的日志条数

查看容器中进程信息

docker top c703b5b1911f

查看容器的元数据

docker inspect 容器id

进入当前正在运行的容器

1、docker exec -it 容器id /bin/bash 2、docker attach 容器id 区别: docker exec 进入容器后开启一个新的终端,可以在里面操作 docker attach 进入容器正在执行的终端,不会启动新的进程

拷贝操作

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

其他命令

pasted-3.png

attach Attach to a running container #当前shell下attach连接指定运行镜像 build Build an image from a Dockerfile #通过Dockerfile定制镜像 commit Create a new image from a container's changes #提交当前容器为新的镜像 cp Copy files/folders from a container to a HOSTDIR or to STDOUT #从容器中拷贝指定文件或者目录到宿主机中 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 a running container#在已存在的容器上运行命令 export Export a container's filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import) history Show the history of an image #展示一个镜像形成历史 images List images #列出系统当前镜像 import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export) info Display system-wide information #显示系统相关信息 inspect Return low-level information on a container or image #查看容器详细信息 kill Kill a running container #kill指定docker容器 load Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save) login Register or log in to a Docker registry#注册或者登陆一个docker源服务器 logout Log out from a Docker registry #从当前Docker registry退出 logs Fetch the logs of a container #输出当前容器日志信息 pause Pause all processes within a container#暂停容器 port List port mappings or a specific mapping for the CONTAINER #查看映射端口对应的容器内部源端口 ps List containers #列出容器列表 pull Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像 push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器 rename Rename a container #重命名容器 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(s) to a tar archive#保存一个镜像为一个tar包(对应load) search Search the Docker Hub for images #在docker hub中搜索镜像 start Start one or more stopped containers#启动容器 stats Display a live stream of container(s) resource usage statistics #统计容器使用资源 stop Stop a running container #停止容器 tag Tag an image into a repository #给源中镜像打标签 top Display the running processes of a container #查看容器中运行的进程信息 unpause Unpause all processes within a container #取消暂停容器 version Show the Docker version information#查看容器版本号 wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值

本文作者:酷少少

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!