当前位置:首页 » 《关注互联网》 » 正文

Docker基础学习笔记一:Docker概述和安装_温暖@的博客

16 人参与  2022年01月21日 10:01  分类 : 《关注互联网》  评论

点击全文阅读


Docker基础学习笔记一:Docker概述和安装

    • 容器的起源
    • 什么是容器
    • Docker是什么
    • Docker容器为什么这么火
    • Docker能做什么?
    • Docker的安装(centos8中)
    • 卸载

容器的起源

假设你们公司正在秘密研发下一个“今日头条”APP,我们姑且称为明日头条,程序员自己从头到尾搭建了一套环境开始写代码,写完代码后程序员要把代码交给测试同学测试,这时测试同学开始从头到尾搭建这套环境,测试过程中出现问题程序员也不用担心,大可以一脸无辜的撒娇,“明明在人家的环境上可以运行的”。

测试同学测完后终于可以上线了,这时运维同学又要重新从头到尾搭建这套环境,费了九牛二虎之力搭建好环境开始上线,糟糕,上线系统就崩溃了,这时心理素质好的程序员又可以施展演技了,“明明在人家的环境上可以运行的”。

从整个过程可以看到,不但我们重复搭建了三套环境还要迫使程序员转行演员浪费表演才华,典型的浪费时间和效率,聪明的程序员是永远不会满足现状的,因此又到了程序员改变世界的时候了,容器技术应运而生。

什么是容器

容器技术起源于Linux开源平台,并且经历了比较长时间的发展阶段,其中最重要的成果是Linux容器(Linux Containers,LXC)技术

IBM DeveloperWorks网站关于容器技术的描述是这样的:“容器有效地将由单个操作系统管理的资源划分到孤立的组中,以更好地在孤立的组之间平衡有冲突的资源使用需求。与虚拟化相比,这样既不需要指令级模拟,也不需要即时编译。容器可以在核心CPU本地运行指令,而不需要任何专门的解释机制。此外,也避免了准虚拟化(paravirtualization)和系统调用替换中的复杂性。”

直白的翻译过来就是这样的:如果认为虚拟机是模拟运行的一整套操作系统(包括内核、应用运行态环境和其他系统环境)和跑在上面的应用,那么容器就是独立运行的一个(或一组)应用,以及它们必需的运行环境

容器中运行的就是一个或者多个应用程序,以及应用运行所需要的环境。容器直接运行在操作系统内核之上的用户空间。容器技术可以让多个独立的用户空间运行在同一台宿主机上。容器既可以运行在物理机也可以运行在虚拟机上,当然也可以运行在公有云主机上。

Docker是什么

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

Docker基于Go语言开发的!开源项目!

一个完整的Docker有以下几个部分组成:

  1. DockerClient客户端
  2. Docker Daemon守护进程
  3. Docker Image镜像
  4. DockerContainer容器

Docker容器为什么这么火

在容器技术出来之前,我们都是使用虚拟机技术!

虚拟机:在window中装一个VMware,通过这个软件我们可以虚拟出来一台或者多台电脑!笨重!

虚拟机也属于虚拟化技术,Docker容器技术,也是一种虚拟化技术!

Docker容器技术:可以独立运行的一个(或一组)应用,以及它们必需的运行环境

Docker能做什么?

比较Docker和虚拟机技术的不同:

  • 传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
  • 容器内的应用直接运行在宿主机的内容,容器是没有自己的内核的,也没有虚拟我们的硬件,所以就轻便了
  • 每个容器间是互相隔离,每个容器内都有一个属于自己的文件系统,互不影响

Docker的安装(centos8中)

docker官网:https://www.docker.com/
官方安装文档url:https://docs.docker.com/engine/install/centos/

注意:此操作都是在root用户下操作,如果权限不够在centos操作命令时输入sudo;

环境准备:

Linux要求内核3.0以上

[root@localhost ~]uname -r  #查看内核版本
4.18.0-305.19.1.el8_4.x86_64
[root@localhost ~]cat /etc/os-release #查看系统版本
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"

安装

#使用yum下载需要的安装包
[root@localhost ~]yum install -y yum-utils 
Last metadata expiration check: 11:30:39 ago on Sun 26 Sep 2021 02:28:03 AM CST.
Package yum-utils-4.0.18-4.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
 #设置镜像的仓库,官方文档默认的是国外的,这里用的是国内的
[root@localhost ~]yum-config-manager \
>     --add-repo \
>     https://download.docker.com/linux/centos/docker-ce.repo
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
#更新系统,为了下载最新的docker
[root@localhost ~] yum -y update
Docker CE Stable - x86_64                                                         7.4 kB/s |  15 kB     00:02    
Dependencies resolved.
Nothing to do.
Complete!
#安装docker相关的 docker-ce 社区版 而ee是企业版 containerd.io是容器,因为我已经安装过了
[root@localhost ~]yum install docker-ce docker-ce-cli containerd.io
Last metadata expiration check: 0:01:37 ago on Sun 26 Sep 2021 02:13:27 PM CST.
Package docker-ce-3:20.10.8-3.el8.x86_64 is already installed.
Package docker-ce-cli-1:20.10.8-3.el8.x86_64 is already installed.
Package containerd.io-1.4.9-3.1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
#启动docker
[root@localhost ~]# systemctl start docker
# 使用docker version查看是否安装成功
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:53:39 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:00 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

测试hello-word

[root@localhost ~] docker run hello-world
Unable to find image 'hello-world:latest' locally #没有这个镜像,需要从外网下载
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0
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/ #下载的真实地址
 
#查看一下下载的这个hello-world镜像
[root@localhost ~]docker images
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    feb5d9fea6a5   2 days ago   13.3kB

卸载

#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!

参考文章url:https://www.kuangstudy.com/bbs/1439163376210096129#Docker
参考Docker视频url:https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0


点击全文阅读


本文链接:http://zhangshiyu.com/post/33645.html

容器  运行  环境  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1