首页 > 技术文章 > Docker常用命令

laoash 2021-02-23 23:41 原文

1、帮助命令

docker version	#查看docker版本信息
docker info		#查看docker系统信息,包括镜像和容器的数量
docker [命令] --help	#docker帮助命令

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

2、镜像命令

docker images	#查看主机上的所有镜像
docker search	#搜索镜像
docker pull		#下载镜像
docker rmi		#删除镜像

docker images 查看主机上的所有镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              7.0                 a65a903aae35        3 months ago        533MB

#REPOSITORY		镜像仓库源
#TAG			镜像版本号
#IMAGE ID		镜像ID
#CREATED		镜像创建时间
#SIZE			镜像的大小


#可选项
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
  

#常用命令
docker images -a	#列出所有镜像
docker images -q	#只显示镜像ID
docker images -aq	#显示所有镜像的ID

docker search 搜索镜像

#docker search 镜像名

#可选项
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
      
#常用命令
docker search 镜像名 -f stars=1000			#搜索星大于1000的镜像
docker search 镜像名 -f is-official=true	#搜索官方的镜像

docker pull 下载镜像

#docker pull 镜像名[:tag]

[root@localhost ~]# docker pull tomcat:7.0
7.0: Pulling from library/tomcat	#如果不写tag,默认就是latest
0ecb575e629c: Pull complete 		#分层下载: docker image 的核心 联合文件系统
7467d1831b69: Pull complete 
feab2c490a3c: Pull complete 
f15a0f46f8c3: Pull complete 
26cb1dfcbebb: Pull complete 
5b224ce6d4ea: Pull complete 
c932fe81bb40: Pull complete 
e9a52e18e79d: Pull complete 
f146b5645f68: Pull complete 
b396c3d5a249: Pull complete 
Digest: sha256:55bc11d882143354cf8b1351e007739a1d5eaf15995c9a59ee5c15893af4c9e9	# 签名 防伪
Status: Downloaded newer image for tomcat:7.0
docker.io/library/tomcat:7.0		#真实地址

#等价于
docker pull tomcat:7.0
docker pull docker.io/library/tomcat:7.0

docker rmi 删除镜像

docker rmi -f 镜像ID	#删除指定镜像
docker rmi -f 镜像ID 镜像ID 镜像ID	#删除指定镜像
docker rmi -f $(docker images -aq)	#删除全部镜像

3、容器命令

docker run 镜像ID			#新建容器并启动
docker ps				#列出所有运行的容器
docker rm 容器ID			#删除指定容器
docker start 容器ID		#启动容器
docker restart 容器ID		#重启容器
docker stop 容器ID		#停止当前正在运行的容器
docker kill 容器ID		#强制停止当前容器
[root@localhost ~]# docker container --help

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

新建容器并启动

docker run [可选参数] image | docker container run [可选参数] image

#可选参书说明
--name="Name"		#容器名字 tomcat01 tomcat02 用来区分容器
-d					#后台方式运行
-it					#交互方式运行,进入容器查看内容
-p					#指定容器的端口或宿主机端口与容器端口做映射
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(端口映射)	#例如:-p 80(宿主机端口):80(容器端口)
	-p 容器端口

-P					#随机映射一个49000~49900的端口到内部容器开放的网络端口
-v					#宿主机与容器文件映射

退出容器

exit		#容器直接退出
ctrl+P+Q	#容器不停止退出

删除容器

docker rm 容器ID					#删除指定的容器,不能删除正在运行的容器
docker rm -rf 容器ID				#删除指定的容器,强制删除
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 run -d 镜像名

#使用docker ps发现容器停止了
#docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止

#正确应该使用命令:docker run -itd 镜像名

查看日志

docker logs --help

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      
-tf					#显示日志信息(一直更新)
--tail number		#需要显示日志条数

docker logs -t --tail n			#查看n行日志
docker logs -tf 容器ID			#跟踪日志

查看容器中进程信息

docker top 容器ID

查看镜像的元数据

docker inspect 容器ID

进入当前正在运行的容器(exec、attach)

#方式一(推荐)
docker exec -it 容器ID bashshell

1、如果从容器退出,容器不会停止
2、进入当前容器后开启一个新的终端,可以在里面操作

#方式二
docker attach 容器ID

1、如果从容器退出,会导致容器停止
2、进入容器正在执行的终端

从容器内拷贝到主机

docker cp 容器ID:容器内路径 主机目标路径

继续更新....

推荐阅读