Docker基本命令

Docker的基础命令

注意,在docker的命令中,如果需要输入整个的英文单词,就是--,单独的一个字母就是-

Docker帮助命令

shell 复制代码
docker version  #显示docker的版本信息
docker info     #显示docker系统信息,包括镜像和容器的信息
docker command --help #帮助命令
		#例如:docker images --help
		[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker images --help
        Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]
        List 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   Pretty-print images using a Go template
              --help            Print usage
              --no-trunc        Don't truncate output
          -q, --quiet           Only show numeric IDs

帮助文档:docker | Docker Docs

Docker 镜像命令

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。

  • docker images 的命令
shell 复制代码
#例如:docker images --help
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker images --help
Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]
List 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   Pretty-print images using a Go template
--help            Print usage
--no-trunc        Don't truncate output
-q, --quiet           Only show numeric IDs
  • 查询全部镜像
shell 复制代码
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker images -a
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
docker.io/portainer/portainer   latest              5f11582196a4        15 months ago       287 MB

#REPOSITORY  镜像的仓库源
#TAG   		 标签【版本】
#IMAGE ID	 镜像的id 
#CREATED	 创建的时间
#SIZE		 大小
  • 搜素镜像
shell 复制代码
# docker search [镜像名]
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker search --help

Usage:	docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --help            Print usage
      --limit int       Max number of search results (default 25)
      --no-index        Don't truncate output
      --no-trunc        Don't truncate output

举例

shell 复制代码
docker search mysql  #相当于从docker hub上去搜索,与百度去docker相同

[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker search mysql
INDEX       NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                           MySQL is a widely used, open-source relati...   14899     [OK]       
docker.io   docker.io/mariadb                         MariaDB Server is a high performing open s...   5689      [OK]       
docker.io   docker.io/phpmyadmin                      phpMyAdmin - A web interface for MySQL and...   953       [OK]       
docker.io   docker.io/percona                         Percona Server is a fork of the MySQL rela...   625       [OK]
  • 拉取镜像
shell 复制代码
# docker pull [镜像名]
docker pull mysql # 拉取镜像【下载】,默认是最新版本[latest],也可以指定版本
  • 删除惊醒
shell 复制代码
docker rmi [镜像名|镜像id]

Docker 容器命令

  • 运行容器【以镜像名运行容器】
shell 复制代码
# docker run [可选参数] image


#参数说明
--name="Name"  容器的名字 例如tomcat01...
--d            后台的方式运行
-it            交互的方式运行
-p             指定容器的端口 -p 8080:8080
	-p 主机端口:容器端口---------------------------------常用
	-p 容器端口
-P			   随机指定端口



#举例
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker run -it centos /bin/bash   # 测试启动并进入容器
[root@e0ccea9f0acf /]# ls		
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@e0ccea9f0acf /]# exit 									    # 停止并退出容器
  • 列出正在运行的所有容器
shell 复制代码
# docker ps
[root@iZf8zhsqf64x47n1tpdy6oZ /]# docker ps --help

Usage:	docker ps [OPTIONS]

List containers

Options:
  -a, --all             #Show all containers (default shows just running)
  -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 numeric IDs
  -s, --size            Display total file sizes
  • exit退出
shell 复制代码
exit 					停止退出
快捷按键Ctrl + P + Q	 不停止退出
  • 删除容器
shell 复制代码
docker rm 容器id                    # 不能删除正在运行的,docker rm -f 强制删除
  • 启动停止容器
shell 复制代码
docker start 容器id 	 # 启动容器
docker restart 容器id  # 重启容器
docker stop 容器id 	 # 停止当前的容器
docker kill 容器id     # 强制停止当前的容器

常用其他的命令

  • 后台启动容器
shell 复制代码
# docker run -d 镜像名

# docker ps 发现centos停止了

# 常见的坑,docker容器使用后台运行,就必须要存在一个前台进程 ,如果docker 发现没有应用,就会自动停止

# 例如,如果只安装了一个nginx,发现自己没有提供服务,就会立刻停止,就是因为没有一个前台的程序
  • 查看日志命令
shell 复制代码
docker logs --help
docker logs -tf --tail [number] [容器id]

# tf显示日志
# --tail要显示日志的条数
  • 查看容器中进程信息ps
shell 复制代码
# docker top 容器id
  • 查看镜像的元数据
shell 复制代码
docker inspect 容器id
  • 进入当前正在运行的容器
shell 复制代码
#通过后台方式运行容器时,需要进入容器,修改配置
方式一:
docker exec -it 容器id  #进入容器开启一个新的终端
方式二:
docker attach 容器id    #进入容器正在执行的终端,不会启动新的进程
  • 从容器内拷贝文件到主机上
shell 复制代码
docker cp 容器id:容器内路径

# 测试
#查看当前正在运行的容器
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                        NAMES
bfb7cef7d1e4        centos                "/bin/bash"         10 minutes ago      Up 10 minutes                                                    naughty_meninsky
af3bc99f4a47        portainer/portainer   "/portainer"        2 days ago          Up 26 hours         8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp   portainer

#进入容器内
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker attach bfb7cef7d1e4
[root@bfb7cef7d1e4 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@bfb7cef7d1e4 /]# cd /home
[root@bfb7cef7d1e4 home]# ls

#创建一个文件
[root@bfb7cef7d1e4 home]# touch test.java
[root@bfb7cef7d1e4 home]# ls
test.java
[root@bfb7cef7d1e4 home]# exit
exit

#复制容器内的文件到主机上
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# docker cp bfb7cef7d1e4:/home/test.java /home
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# ls
[root@iZf8zhsqf64x47n1tpdy6oZ ~]# cd /home
[root@iZf8zhsqf64x47n1tpdy6oZ home]# ls
test.java
[root@iZf8zhsqf64x47n1tpdy6oZ home]#
相关推荐
XIAOHEZIcode15 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
武子康1 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn864 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化