docker笔记之安装

前言

在以往,当我们我们发布一个项目的话,需要交给运维人员,给他们很多文档,然后按照文档,安装程序,他们按到手的不是一个可以直接运行的项目,需要进行各种测试之前,需要把项目跑通。所以光是配置文档,就很繁琐,基于此,docker就很好的解决了这个问题。

一、docker是什么?

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

  1. docker可以做到让开发人员把镜像打包,测试,一键运行,直接的减轻运维人员的压力。
  2. 项目打包为一个一个的镜像之后,可以再次进行扩展,打包镜像,让更能更加的完善。
  3. 内核级别的虚拟化,使得一个物理机可以运行很多的容器实例,极大的减少了服务器的性能。

一些简单的概念

  • 容器(container):由镜像创建而来,用于运行一个或者多个应用,在容器内部可以运行一些基本的linux命令,容器本身就是物理机里面的一个小型linux系统。
  • 镜像(image):好比一个模板,利用模板创建容器服务,一个镜像可以创建多个容器
  • 仓库(repository):仓库就是存放镜像的地方,分为公有跟私有

二、docker快速入门

1.安装docker

#进行一些基本的环境查看
[root@iZbp10d5h2h0qbsabf055iZ /]# uname -r  #查看内核版本
3.10.0-957.21.3.el7.x86_64
[root@iZbp10d5h2h0qbsabf055iZ /]# cat /etc/os-release #查看系统版本
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[root@iZbp10d5h2h0qbsabf055iZ /]#
#删除旧的docker
[root@iZbp10d5h2h0qbsabf055iZ /]# yum remove docker \
> 
Loaded plugins: fastestmirror
No Match for argument: docker
No Packages marked for removal
[root@iZbp10d5h2h0qbsabf055iZ /]# 
#下载需要的安装包
[root@iZbp10d5h2h0qbsabf055iZ /]# yum install -y yum-utils  

在这里插入图片描述

#设置镜像的仓库
[root@iZbp10d5h2h0qbsabf055iZ /]# yum-config-manager  --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@iZbp10d5h2h0qbsabf055iZ /]# 
#更新yum软件包索引
[root@iZbp10d5h2h0qbsabf055iZ /]# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                     | 3.6 kB  00:00:00     
epel                                                                                     | 4.7 kB  00:00:00     
extras                                                                                   | 2.9 kB  00:00:00     
updates                                                                                  | 2.9 kB  00:00:00     
Metadata Cache Created
[root@iZbp10d5h2h0qbsabf055iZ /]#
#安装docker
yum -y install docker-ce
#检查docker是否安装成功
[root@iZbp10d5h2h0qbsabf055iZ /]# docker version
Client: Docker Engine - Community
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:34:14 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
#开启docker服务,并且运行helloworld程序
[root@iZbp10d5h2h0qbsabf055iZ /]# service docker start  #开启docker服务
Redirecting to /bin/systemctl start docker.service  
[root@iZbp10d5h2h0qbsabf055iZ /]# docker run hello-world  #运行hello-world程序
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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/

[root@iZbp10d5h2h0qbsabf055iZ /]# 

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

三、总结

在这里插入图片描述

最后再附上配置阿里云镜像加速器的流程
在这里插入图片描述
在这里插入图片描述

打开文件/etc/docker/daemon.json

[root@iZbp10d5h2h0qbsabf055iZ image]# cd /etc/docker/
[root@iZbp10d5h2h0qbsabf055iZ docker]# ls
daemon.json  key.json
[root@iZbp10d5h2h0qbsabf055iZ docker]# vim daemon.json
#把以下内容加到daemon.json当中即可。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    
    
  "registry-mirrors": ["https://ymlbq6ki.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

至此,docker的安装基本完成。

猜你喜欢

转载自blog.csdn.net/qq_41486775/article/details/113773129