Docker架构图

202305171816.png.jpg

仓库、镜像、容器概念
Docker本身是一个容器运行载体或称之为管理引擎。我们把应用程序和配置依赖打包好形成一个可交付的运行环境,这个打包好的运行环境就制作images镜像文件。只能通过这个镜像文件才能生成docker容器。imgaes文件可以看作是容器的模板。docker根据images文件生成容器的实例,同一个images文件,可以生成多个同时运行的容器实例。
镜像:
images文件生成的容器实例,本身也是一个文件,称为镜像文件
容器:
一个容器运行一种服务,当我们需要的时候,就可以通过docker客户端创建一个对应的运行实例,也就是我们的容器
仓库:
至于仓库,就是放了一堆镜像的地方,我们可以把镜像发布到仓库中,需要的时候从仓库中拉下来就可以了。

Docker安装环境说明

本次实验使用Centos7.6 1810版本,Docker运行在Centos7上,要求系统为64位,系统内核版本为3.10以上。
查看自己的内核

uname -a命令用于打印当前系统相关信息(内核版本号、硬件架构、主机名和操作系统类型等)

Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

查看版本信息

[root@localhost ~]# 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="

Docker安装步骤

yum安装gcc相关环境(需要确保虚拟机可以上网)

yum -y install gcc gcc-c++
Updated:
  gcc.x86_64 0:4.8.5-44.el7   gcc-c++.x86_64 0:4.8.5-44.el7                                                 

Dependency Updated:
  cpp.x86_64 0:4.8.5-44.el7               gcc-gfortran.x86_64 0:4.8.5-44.el7   libgcc.x86_64 0:4.8.5-44.el7              libgfortran.x86_64 0:4.8.5-44.el7  
  libgomp.x86_64 0:4.8.5-44.el7           libquadmath.x86_64 0:4.8.5-44.el7    libquadmath-devel.x86_64 0:4.8.5-44.el7   libstdc++.x86_64 0:4.8.5-44.el7    
  libstdc++-devel.x86_64 0:4.8.5-44.el7  

Complete!

安装需要的软件包

yum install -y yum-utils

Running transaction
  Installing : python-chardet-2.2.1-3.el7.noarch                                                                                     1/3 
  Installing : python-kitchen-1.1.1-5.el7.noarch                                                                                     2/3 
  Installing : yum-utils-1.1.31-54.el7_8.noarch                                                                                      3/3 
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                                     1/3 
  Verifying  : yum-utils-1.1.31-54.el7_8.noarch                                                                                      2/3 
  Verifying  : python-chardet-2.2.1-3.el7.noarch                                                                                     3/3 

Installed:
  yum-utils.noarch 0:1.1.31-54.el7_8                                                                                                     

Dependency Installed:
  python-chardet.noarch 0:2.2.1-3.el7                                 python-kitchen.noarch 0:1.1.1-5.el7                                

Complete!

设置镜像仓库

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum软件包索引

yum makecache fast

安装Docker CE

yum install docker-ce docker-ce-cli containerd.io
Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                                 checkpolicy.x86_64 0:2.5-8.el7                                 
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8                     docker-buildx-plugin.x86_64 0:0.10.4-1.el7                     
  docker-ce-rootless-extras.x86_64 0:24.0.0-1.el7                        docker-compose-plugin.x86_64 0:2.17.3-1.el7                    
  fuse-overlayfs.x86_64 0:0.7.2-6.el7_8                                  fuse3-libs.x86_64 0:3.6.1-4.el7                                
  libcgroup.x86_64 0:0.41-21.el7                                         libsemanage-python.x86_64 0:2.5-14.el7                         
  policycoreutils-python.x86_64 0:2.5-34.el7                             python-IPy.noarch 0:0.75-6.el7                                 
  setools-libs.x86_64 0:3.3.8-4.el7                                      slirp4netns.x86_64 0:0.4.3-4.el7_8                             

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7              audit-libs.x86_64 0:2.8.5-4.el7              policycoreutils.x86_64 0:2.5-34.el7             

Complete!

启动、检查docker服务

启动docker服务:systemctl start docker
查看docker服务:systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-05-18 03:41:31 EDT; 48s ago
     Docs: https://docs.docker.com
 Main PID: 6607 (dockerd)
    Tasks: 10
   Memory: 33.4M
   CGroup: /system.slice/docker.service
           └─6607 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

May 18 03:41:26 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
May 18 03:41:26 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:26.816176083-04:00" level=info msg="Starting up"
May 18 03:41:27 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:27.054695325-04:00" level=info msg="Loading containe...tart."
May 18 03:41:30 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:30.710254204-04:00" level=info msg="Firewalld: inter...rning"
May 18 03:41:31 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:31.366400631-04:00" level=info msg="Loading containe...done."
May 18 03:41:31 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:31.438580784-04:00" level=info msg="Docker daemon" c...24.0.0
May 18 03:41:31 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:31.439788782-04:00" level=info msg="Daemon has compl...ation"
May 18 03:41:31 localhost.localdomain dockerd[6607]: time="2023-05-18T03:41:31.605198923-04:00" level=info msg="API listen on /r....sock"
May 18 03:41:31 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

测试Dokcer命令

查看docker版本:docker version
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           24.0.0
 API version:       1.43
 Go version:        go1.20.4
 Git commit:        98fdcd7
 Built:             Mon May 15 18:51:37 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.0
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.4
  Git commit:       1331b8c
  Built:            Mon May 15 18:50:39 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@localhost ~]# 

运行hello-world容器

 docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

查看镜像

docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    9c7a54a9a43c   13 days ago   13.3kB

Docker卸载

systemctl stop docker    
    
yum -y remove docker-ce    docker-ce-cli
    
rm -rf /var/lib/docker    

阿里云镜像加速

  • 介绍:https://www.aliyun.com/product/acr
  • 注册一个属于自己的阿里云账户(可复用淘宝账号)
  • 进入管理控制台设置密码,开通
  • 查看镜像加速器自己的

2023-05-19_114840.jpg
配置镜像加速器

[root@localhost docker]# mkdir -p /etc/docker
[root@localhost docker]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://h5vhdgsa.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://h5vhdgsa.mirror.aliyuncs.com"]
}
[root@localhost docker]# 

配置加载、重启容器

[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker
[root@localhost docker]# 

测试HellWord

[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
最后修改:2023 年 05 月 23 日
如果觉得我的文章对你有用,请随意赞赏