“ 很久之前就想写相关知识,刚好之前在公司内部技术分享会的时候分享过一次,这次利用空闲时间又重新精心整理补充了一下,内容都是我平时学习及工作中实践总结而来。整体内容比较多,为避免篇幅过长、看起来枯燥乏味,特地拆分成了多篇来写。欢迎收藏、关注!”

系列文章将包含(打钩的为本次章节内容):

01

【 简介】

一、什么是

官方地址:

是一个基于go语言开发的开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

删除dockerimages_docker删除image_docker启动image

二、核心概念

三大核心概念:镜像 Image、容器 、仓库

面向对象

容器

对象

镜像

docker启动image_docker删除image_删除dockerimages

三、架构及原理

docker启动image_docker删除image_删除dockerimages

概念

说明

镜像()

镜像是用于创建 容器的模板。基于镜像可以创建容器,同一个镜像可以创建多个容器;

容器()

容器是独立运行的一个或一组应用,是镜像运行时的实体。

客户端()

客户端通过命令行或者其他工具使用 SDK () 与 的守护进程通信。

主机(Host)

一个物理或者虚拟的机器用于执行 守护进程和容器。

仓库用来保存镜像,可以理解为代码控制中的代码仓库。

镜像仓库分为公共镜像仓库 Hub和一些私有化部署的仓库比如:(这两种类似于和)

Hub() 提供了庞大的镜像集合供使用。

一个 中可以包含多个仓库();每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。

通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 : 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 作为默认标签。

是一个简化安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装,比如、 Ocean、 Azure。

四、轻量级的实现原理

容器本质上是宿主机的进程,通过实现了资源隔离,通过实现了资源限制,通过写时复制机制(copy-on-write)实现了高效的文件操作。

资源隔离

linux内核提拱了6种隔离的系统调用:

系统调用参数

隔离内容

UTS

主机名或域名 (since Linux 2.6.19)

IPC

信号量、消息队列和共享内存(since Linux 2.6.19)

PID

进程编号(since Linux 2.6.24)

网络设备、栈、端口等(since Linux 2.6.24)

Mount

挂载点(文件系统)(since Linux 2.6.24)

User

用户组和用户组( in Linux 2.6.23 and in Linux 3.8)

五、的优缺点1.优点2.缺点

02

【 安装与启动】

一、安装与启动1.安装的几种方式1)安装最新版本

① 先卸载旧版本的

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

② 指定下载源(可选,适用于首次安装)

yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

③ 安装(默认安装最新版本)

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

docker删除image_删除dockerimages_docker启动image

④ 验证是否安装成功

docker version

删除dockerimages_docker启动image_docker删除image

2)安装指定版本的

yum list docker-ce --showduplicates | sort -r  # 查看所有可用版本
yum install docker-ce- docker-ce-cli- containerd.io  # 安装指定版本

docker启动image_删除dockerimages_docker删除image

3)通过脚本一键安装

脚本内容如下:

#!/bin/bash
echo "set default docker install repo"
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
echo "install docker ..."
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl status docker

2.启动

systemctl start docker  # 启动服务
systemctl status docker  # 查看状态
systemctl stop docker  # 停止服务
systemctl restart docker  # 重启服务

二、创建第一个容器1.创建容器

按照国际惯例,先运行一个hello-world的容器

docker run hello-world
# 如果网络等一切正常的话,会出现如下提示,表示容器已经创建成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:18a657d0cc1c7d0678a3fbea8b7eb4918bba25968d3e1b0adebfa71caddbc346
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2.查看容器

docker ps -a  # 查看所有容器
# 创建成功,容器列表中就会有hello-world的容器,但名字不是hello-world,因为我们在运行容器时并未指定名称
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
a07f1a8ea1a4   hello-world   "/hello"   21 seconds ago   Exited (0) 20 seconds ago             adoring_chatterjee

三、卸载.常规方式卸载

① 停止服务

systemctl stop docker

② 搜索已经安装的安装包

yum list installed | grep docker
rpm -qa | grep docker
yum -y remove docker-ce.x86_64 
yum -y remove docker-ce-cli.x86_64 
yum -y remove containerd.io.x86_64

③ 移除所有相关安装包

yum -y remove contained.io.x86_64

④ 删除镜像及相关文件夹

rm -rf /var/lib/docker

2.脚本卸载

所谓的使用脚本安装和卸载,通俗理解就是把上述多个操作步骤的命令放在一个脚本中批量执行,内容如下:

#!/bin/bash
systemctl stop docker
yum -y remove docker-ce.x86_64
yum -y remove docker-ce-cli.x86_64
yum -y remove containerd.io.x86_64
rm -rf /var/lib/docker
rm -rf /etc/docker/daemon.json

———END———
限 时 特 惠: 本站每日持续更新海量各大内部创业教程,永久会员只需109元,全站资源免费下载 点击查看详情
站 长 微 信: nanadh666