一、镜像的分层结构

层次:Kernel --> bootfs引导层 --> Base Image -->image -->image (镜像打包好了,只能可读,镜像启动容器后,才可更改)

  • 容器共享宿主机的内核kernel
  • base镜像提供的是最小的Linux发行版
  • 同一docker主机支持运行多种linux发行版,所有linux内核是一样的
  • 采用分层结构的最大好处:共享资源,做叠代只需在原始镜像上附加一层,镜像分层后视图将从上至下查看,以最新梗概的生效,没有更改的共享;同样的镜像曾只用下载一份,降低了网络带宽和节约磁盘空间
  • 镜像启动成容器,用户才能访问,用户只能更改容器第一层为新生成的可写层使用copy-on-write技术(复制写实技术),可写容器层以下的所有镜像层都是只读的
  • 容器层保存镜像变化的部分,并不会对镜像本身进行任何修改

二、搭建docker及阿里云yum源

1、搭建docker镜像yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vi docker.repo 
[docker]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7.9/x86_64/stable/
gpgcheck=0
enabled=1

2、搭建阿里云yum源

解释:阿里云centos源可解决docker安装时所需依赖等问题

#根据实际的系统版本下载对应的yum源,这里是centos7版本
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

三、docker的安装

1、安装docker环境
#列出yum源离docker-ce所有的版本
[root@localhost yum.repos.d]# yum list docker-ce   --showduplicates

#安装docker
[root@localhost yum.repos.d]# yum install -y  docker-ce-19.03.15-3.el7 docker-ce-cli-19.03.15-3.el7
#docker-ce-cli为docker引擎的命令行界面,docker-ce完成所有管理工作
[root@localhost yum.repos.d]# rpm -aq |grep docker
docker-ce-cli-19.03.15-3.el7.x86_64  
docker-ce-19.03.15-3.el7.x86_64
#containerd.io为OS API接口的守护进程,将docker鱼OS分离
[root@localhost yum.repos.d]# rpm -qa |grep container
container-selinux-2.119.2-1.911c772.el7_8.noarch
containerd.io-1.6.6-3.1.el7.x86_64

#开启docker并设置开机自启
[root@localhost ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
 ..........
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled  #docker是走桥接,转发使用的防火墙
WARNING: bridge-nf-call-ip6tables is disabled
#解决两个WARNING
[root@localhost ~]# sysctl -a |grep bridge-nf-call-iptables
net.bridge.bridge-nf-call-iptables = 0
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.docker0.stable_secret"
sysctl: reading key "net.ipv6.conf.ens192.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
#修改系统内核参数
[root@localhost ~]# vi /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1  
net.bridge.bridge-nf-call-ip6tables = 1
#使修改参数生效
[root@localhost ~]# sysctl --system 
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
2、镜像的使用
#在镜像仓库中搜索yakexi007相关镜像
[root@localhost ~]# docker search yakexi007
NAME                          DESCRIPTION         STARS               OFFICIAL            AUTOMATED
yakexi007/game2048                                0                                       

#拉取镜像到本地  
[root@localhost ~]# docker pull yakexi007/game2048
Using default tag: latest
latest: Pulling from yakexi007/game2048
534e72e7cedc: Pull complete 
f62e2f6dfeef: Pull complete 
fe7db6293242: Pull complete 
3f120f6a2bf8: Pull complete 
4ba4e6930ea5: Pull complete 
Digest: sha256:8a34fb9cb168c420604b6e5d32ca6d412cb0d533a826b313b190535c03fe9390
Status: Downloaded newer image for yakexi007/game2048:latest
docker.io/yakexi007/game2048:latest

#查看镜像
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
yakexi007/game2048   latest              19299002fdbe        5 years ago         55.5MB

#查看镜像封装历史记录
[root@localhost ~]# docker  history yakexi007/game2048
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
19299002fdbe        5 years ago         /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "sed …   0B                  
<missing>           5 years ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B                  
<missing>           5 years ago         /bin/sh -c #(nop) COPY dir:cb74e9c037a3d501c…   600kB               
<missing>           5 years ago         /bin/sh -c #(nop)  MAINTAINER Golfen Guo <go…   0B                  
<missing>           5 years ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           5 years ago         /bin/sh -c #(nop)  EXPOSE 443/tcp 80/tcp        0B                  
<missing>           5 years ago         /bin/sh -c #(nop) COPY file:d15ceb73c6ea776c…   1.1kB               
<missing>           5 years ago         /bin/sh -c #(nop) COPY file:af94db45bb7e4b8f…   643B                
<missing>           5 years ago         /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178…   50.1MB              
<missing>           5 years ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.11.7     0B                  
<missing>           5 years ago         /bin/sh -c #(nop)  MAINTAINER NGINX Docker M…   0B                  
<missing>           5 years ago         /bin/sh -c #(nop) ADD file:7afbc23fda8b0b387…   4.8MB  

#创建并运行一个新容器 
 [root@localhost ~]# docker run -d --name demo -p 80:80 yakexi007/game2048  
 #查看正在运行的容器
 [root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                         NAMES
b1498afff141        yakexi007/game2048   "/bin/sh -c 'sed -i …"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 443/tcp   demo

浏览器输入本地IP地址,即能获得一个2048小游戏
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐