目 录CONTENT

文章目录

Docker常用命令

Rain
2020-12-23 / 0 评论 / 0 点赞 / 540 阅读 / 1,094 字 / 正在检测是否收录...

帮助命令

docker version    #显示docker版本信息
docker info       #显示docker系统信息
docker  --help    #帮助命令

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

镜像命令

查看本机镜像docker images

[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   11 months ago   13.3kB

#解释
REPOSITORY    镜像的仓库源
TAG           镜像的标签
IMAGE ID      镜像的id
CREATED       创建时间
SIZE          镜像大小
[root@localhost /]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10300     [OK]       
mariadb                           MariaDB is a community-developed fork of MyS…   3809      [OK]      
 
#可选项,通过收藏过滤
--filter=STARS=3000  搜索出来starts大于3000的

下载镜像docker pull

#下载镜像 docker pull 镜像名

[root@localhost /]# docker pull mysql
Using default tag: latest     #如果不写tag,默认就是latest
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete    #分层下载,docker images的核心 
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  #真实地址

image.png

指定版本镜像下载

[root@localhost /]# 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

删除镜像docker rmi

[root@localhost /]# docker rmi -f 镜像id  #删除指定id的镜像

[root@localhost /]# docker rmi -f 镜像id 镜像id 镜像id #删除多个镜像


[root@localhost /]# docker rmi -f $(docker images -aq) #删除全部

容器命令

下载一个centos镜像来测试

下载容器pull centos

新建容器并启动

docker run [可选参数]

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

 
# 启动并进入容器
[root@localhost /]# docker run -it centos /bin/bash
[root@430f83ef38e5 /]# 
#退出命令,从容器退回主机
[root@430f83ef38e5 /]# exit

**列出所有的运行的容器**
**docker ps 命令**
-a		#列出当前正在运行的容器和历史运行的容器
-n=?		#显示最近创建的容器
-q		#只显示容器的编号

退出容器exit

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

删除容器docker rm

docker rm 容器id		删除指定的容器,不能删除正在运行的容器,如果强制删除 rm -rf
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 run -d 镜像名
[root@localhost /]# docker run -d centos

#问题docker ps 发现 centos停了
#常见坑,docker 容器使用后台运行,就必须要有前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供任何服务,就会立即停止,就是没有程序了

查看日志 logs



docker logs -f -t --tail 10 容器id
#显示日志
-tf	     #显示日志
-tail number #要显示的日志条数

查看容器中的进程信息 ps

#docker top 容器id

[root@localhost /]# docker top e6f6896ab12a
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                16507               16487               0                   16:10               pts/0               00:00:00            /bin/bash

查看镜像源数据 inspect

#docker inspect 容器id

进入当前正在运行的容器

# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

# 命令
docker exec -it 容器id bashShell

#方式二
docker attach 容器id


docker exec  #进入容器后开启一个新的终端,可以在里面操作

docker attach #进入容器正在执行的终端

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径 目的的主机路径

#拷贝是一个手动过程、未来我们使用 -v 卷的技术,可以实现

0

评论区