docker入门教程(二)

1.docker核心概念

1.1 docker镜像

docker镜像也叫image,类似于虚拟机镜像,可以将镜像理解为一个面线docker引擎的只读模板,也包含文件系统。
镜像是创建容器的基础,通过版本管理和增量文件系统,镜像与容器的关系可以比作我们面向对象语言中的类与对象,镜像就是我们的写的类文件,然后容器就是new 出来的对象。

1.2 docker容器

docker容器(container)类似于一个轻量级的沙箱,docker利用容器来运行和隔离应用。容器是从镜像创建的应用运行实例,可以将其启动,暂停,删除,停止,而这些容器都是相互隔离,互不可见的。镜像自身是只读的,容器从镜像启动的时候,docker会在镜像的最上层创建一个可写层,镜像本身保持不变。

1.3 docker仓库

docker仓库就跟代码仓库差不多,只不过我们代码仓库是放代码的,然后我们docker仓库是放docker镜像的,docker仓库可以分为公开仓库于私有仓库,我们最大的docker仓库就是docker hub,链接
当用户创建了自己的镜像之后就可以使用push命令将它上传到指定的仓库,这样用户下次在另一台电脑使用该镜像时,可以直接pull下来。

1.4 镜像,容器与仓库的关系

在这里插入图片描述
1.镜像与仓库之间的关系是push与pull的关系,这个还是很好理解的。
2.镜像与容器的关系:镜像run可以启动一个容器,然后容器也可commit成一个镜像。

2. docker安装

咱们这里安装主要是针对linux系统来说,对于mac os 与windows系统的哥们可以直接取docker官网下载对应的安装包,然后都是可视化安装。

2.1内核版本

要使用好容器技术,linux内核版本需要3.8以上
可以通过uname -a命令来查看内核版本

Linux hadoop101 3.10.0-957.27.2.el7.x86_64 #1 SMP Mon Jul 29 17:46:05 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2.2 设置yum源

curl -o /etc/yum.repos.d/CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

这个时候可以使用命令查看可以安装的docker-ce包

 yum list docker-ce --show-duplicates

在这里插入图片描述
如果要删除系统中老版本的docker 可以使用命令

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

2.3安装

yum install docker-ce

在这里插入图片描述
安装完后使用docker version 查看版本

docker version

在这里插入图片描述

2.4设置

设置开机自启动

扫描二维码关注公众号,回复: 11293242 查看本文章
systemctl enable docker

启动docker

systemctl start docker

添加阿里云镜像源

vi /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://bsoedszv.mirror.aliyuncs.com"
  ]
}

重启docker

systemctl restart docker

3. 运行第一个容器

docker run hello-world

输出结果:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
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/

Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
首先docker会在本地仓库中查找‘hello-world:latest’镜像,然后没有找到,就去hub中pull这个镜像。
我们从输出结果中可以看到有4步:
1.The Docker client contacted the Docker daemon. 就是docker客户端连接docker daemon ,可以看出docker是个c/s架构
2.The Docker daemon pulled the “hello-world” image from the Docker Hub.
docker从docker hub中拉取这个镜像。
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
docker daemon使用这个镜像创建一个容器,然后运行你的可以执行文件。
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
docker daemon将运行结果返回给docker 客户端,发到你的终端上,然后就在terminal看到了它这堆输出。

猜你喜欢

转载自blog.csdn.net/yuanshangshenghuo/article/details/106559123