Docker学习笔记(一) -- 安装与概述

141 阅读2分钟

docker是基于Go语言开发的开源项目

Docker在开发和运维中,能够更快的进行部署,更高效的利用资源。也能更轻松的迁移、拓展和管理。

与传统的虚拟方式相比,它更轻量化。启动和停止能够在秒级实现。且对系统资源需求量较少。

docker是如何工作的?

1630504933545.png

从图中可以看到,docker有着比虚拟机更少的抽象层。且docker主要使用宿主机的内核。所以在新建一个容器时,不需要重新加载一个新的系统内核。

在虚拟机的系统中,有独立的文件系统,进程系统,内存系统,等等系统,所以为了让容器接近虚拟机,也需要有这些系统。docker采用隔离的方式来将容器的运行环境相对宿主机隔离开来。这样就能享受到这些系统资源的同时,又不用运行独立的内核。

安装docker

官网地址

建议直接看官网的安装方法,点击对应的系统,就会跳转到对应的安装页面。

1630505438842.png 这里介绍CentOS的安装步骤。需要准备一台CentOS7以上的虚拟机或者云。

#查看系统内核
# uname -r
# cat /etc/redhat-release

#1.卸载原来的docker

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

#2.需要的安装包

yum install -y yum-utils

#3.设置镜像仓库(默认国外的,一般不适用这个,太慢了)

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
#使用阿里镜像
 yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#4.安装docker相关内容 docker-ce社区版 ee 企业版

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

#5. 启动

systemctl start docker

查看docker版本

docker version
​
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:55:49 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:54:13 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
​

跑一个镜像进行测试

docker run hello-world
​
#打印下面这些说明成功了
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/

卸载Docker

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