【Docker系列】基础与常用命令

docker简介

  • docker容器技术,与我们熟知的虚拟机都同属于虚拟化技术!
  • docker基于Go语言开发

docker相关术语

镜像(image):相当于一个模板,可以用此镜像运行很多实例
容器(container):docker运用容器技术创建的一个简易的虚拟机,通过镜像创建
仓库(repository):存放镜像的仓库,分为public和private

安装docker

1、如果有安装旧版本的docker,需要先移除旧版本的docker

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2、安装docker所需要的依赖工具与环境

yum install -y yum-utils

3、使用阿里云的镜像安装docker

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、安装最新版docker

yum install docker-ce docker-ce-cli containerd.io

5、启动docker

systemctl start docker

6、执行docker hello word

docker run hello-world

7、移除docker

yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

配置阿里云镜像加速

1、登陆阿里云官网,找到镜像服务
在这里插入图片描述

2、按照指示设置自己服务器上的docker

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://n22inun7.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

docker run的运行流程

在这里插入图片描述

docker是怎么工作的

在这里插入图片描述

docker常用命令

帮助命令
docker version #查看版本
docker info #查看docker系统信息
docker 命令 --help #帮助命令
镜像命令
  • docker images
docker images #当前本机上的镜像列表

docker images --help

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

#Options:
  -a, --all             Show all images (default hides intermediate images)
  -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 image IDs
  • docker search 搜索镜像
docker search mysql #搜索mysql镜像
#可选项
  -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 pull 下载镜像
docker pull mysql #下载mysql镜像,默认下载最新版本
#指定版本下载
docker pull mysql:5.7 #版本必须在仓库中
# 可选项
  -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
  • docker rmi 删除镜像
docker rmi -f $(docker images -aq) #全部删除
docker rmi -f ID ID1 ID2 #指定镜像ID删除指定镜像
容器命令
新建容器并启动
docker run [可选参数] image
#可选参数
--name="Name"	容器名字
-d				后台方式运行
-it				使用交互方式运行,进入容器查看内容
-p				指定容器的端口 -p 8080:8080
	-p IP:主机端口:容器端口
	-p 主机端口:容器端口(最常用)
	-p 容器端口
	容器端口
-P				随机指定端口
#使用容器运行centos镜像
docker pull centos #下载centos镜像,默认最新版本
docker run -it centos /bin/bash #使用容器运行centos镜像,并进入交互模式
ls #此时已经进入容器中的centos环境,查看目录
![在这里插入图片描述](https://img-blog.csdnimg.cn/11bdc5e024254ec0a7af13950e0b1d09.png)e
exit #从容器中退出,返回主机
查看当前正在运行的容器
docker ps

在这里插入图片描述
因为已经退出所以没有正在运行的docker容器
查看所有运行以及历史运行过的容器

docker ps -a

在这里插入图片描述

列出最近创建的n个容器
docker ps -a -n=?

在这里插入图片描述

容器退出
exit #直接从当前已经进入的容器中退出,并关闭容器
ctrl+p+q #快捷键,退出当前容器,保持容器运行状态
删除容器
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 run -d 镜像名 后台启动容器
docker run -d centos 
#常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
查看日志
docker logs
#可选项:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --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:37Z) or relative (e.g. 42m for 42 minutes)
查看容器内的进程信息
docker top 容器ID
查看镜像的元数据
docker inspect 容器ID
#可选项:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
[root@iZ2zein4retlu7npemaz4qZ ~]# 

进入当前运行的容器
docker exec -it 容器ID bashshell

docker attach  容器ID
#区别
docker exec #进入容器后开启一个新的终端,可以在里面操作
docker attach #进入容器当前正在执行的终端,不会启动新的终端
从容器内拷贝文件到主机上
docker cp 容器ID:容器内部路径  目标主机路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLLDa_&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值