云计算实训37——Dockerfile的应用+私有仓库的创建与管理

一、文件联合系统

文件联合系统(Overlay Filesystem):

是一种允许多个文件系统层叠在一起的文件系统。它通常用于将只读文件系统(如基础镜像)与可写文件系统(如用户的修改)结合,形成一个单一的文件系统视图。这种技术在容器化环境(如 Docker)和虚拟化中得到了广泛应用。

Overlay 文件系统的主要特点包括:

  1. 多层次结构:文件系统可以分为多个层,每一层可以是只读或可写。用户的修改会在最上层进行,而基础层保持不变。

  2. 高效性:Overlay 文件系统只在必要时加载数据,这样可以节省存储空间和提高性能。

  3. 文件共享:多个容器或用户可以共享相同的只读层,减少数据冗余。

  4. 快照和版本控制:由于其层叠结构,可以轻松创建快照并管理不同版本的文件系统。

在实际应用中,OverlayFS 是 Linux 系统中常用的实现,它提供了高效的文件系统组合能力,使得在开发和运行应用时更加灵活。

二、dockerfile应用

通过dockerfile创建⼀个在启动容器时,就可以启动httpd服务的镜像

步骤 :

1.创建⼀个⽬录,⽤于存储Docker file所使⽤的⽂件

2.在此⽬录中创建Docker file⽂件,以及镜像制作所使⽤的⽂件

3.使⽤docker build创建镜像

4.使⽤创建的镜像启动容器(验证)

1.使用Dockerfile创建httpd镜像

# 停止docker服务
[root@docker ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket

# 删除docker文件
[root@docker ~]# rm -rf /var/lib/docker/*

# 启动docker服务
[root@docker ~]# systemctl start docker

# 拉取centos镜像
[root@docker ~]#  docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

# 创建001目录
[root@docker ~]# mkdir 001

# 进到001目录下
[root@docker ~]# cd 001/

# 编辑启动脚本
[root@docker 001]# vim abc.sh

# !/bin/bash

rm -rf /run/httpd/*

exec /sbin/httpd -D FOREGROUND

# 查看创建的脚本
[root@docker 001]# ls
abc.sh

# 编辑index.html文件
[root@docker 001]# echo "httpd server is running" > index.html
[root@docker 001]# ls
abc.sh  index.html

# 编辑Dockerfile文件
[root@docker 001]# vim Dockerfile
FROM    centos:latest

MAINTAINER      "centos httpd server"

RUN     rm -rf /etc/yum.repos.d/*  

RUN     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

RUN     yum clean all && yum makecache

RUN     yum -y install epel-release

RUN     yum -y install httpd

ADD     abc.sh /abc.sh

ADD     index.html /var/www/html/index.html

RUN     chmod -v +x /abc.sh

CMD     ["/bin/bash","/abc.sh"]

# 初看当前的路径
[root@docker 001]# pwd
/root/httpd0

# 查看创建的文件
[root@docker 001]# ls
abc.sh  Dockerfile  index.html

# 创建镜像
[root@docker 001]# docker build -t centos:httpd .
[+] Building 64.6s (14/14) FINISHED                                    docker:default
 => [internal] load build definition from Dockerfile                             0.0s
 => => transferring dockerfile: 546B                                             0.0s
 => [internal] load metadata for docker.io/library/centos:latest                 0.0s
 => [internal] load .dockerignore                                                0.0s
 => => transferring context: 2B                                                  0.0s
 => [1/9] FROM docker.io/library/centos:latest                                   0.0s
 => [internal] load build context                                                0.0s
 => => transferring context: 282B                                                0.0s
 => [2/9] RUN     rm -rf /etc/yum.repos.d/*                                      0.2s
 => [3/9] RUN     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.ali  0.5s
 => [4/9] RUN     yum clean all && yum makecache                                37.5s
 => [5/9] RUN     yum -y install epel-release                                    1.5s 
 => [6/9] RUN     yum -y install httpd                                          24.4s 
 => [7/9] ADD     abc.sh /abc.sh                                                 0.0s 
 => [8/9] ADD     index.html /var/www/html/index.html                            0.0s 
 => [9/9] RUN     chmod -v +x /abc.sh                                            0.1s 
 => exporting to image                                                           0.3s 
 => => exporting layers                                                          0.3s 
 => => writing image sha256:75b16235c09a77040511b510a9c1e9b64a81b779f611e9baf80  0.0s 
 => => naming to docker.io/library/centos:httpd                                  0.0s

# 查看镜像
[root@docker 001]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       httpd     75b16235c09a   2 minutes ago   338MB
centos       latest    5d0da3dc9764   2 years ago     231MB

# 查看docker文件的变化
[root@docker 001]# ls /var/lib/docker/overlay2/
2e14mkmhsyqml8yz8xx6gkkfn
3gptwrcuxsr2z3ud201n5gg9j
backingFsBlockDev
e0dfg5mgrat7u2jzgxvgfj6mx
fc6fcbe50f7cd8967fba6b54c30675443571463587d0d0bc735cd28a4214dbd4
gwiapvpmctfmwsjpj4fhwpwfk
i1no1l3564opovspusav7juge
iafohrok69hr2srb4sgehmeq1
kh7i34l3wsr17f41sv1mpbnqr
l
nghnnma9u2ktpsgeb9kxxzjjm
oor91hz24u4ux6v2h0u2ayx6v
qta19zq5kinbictlivuvkr06r
tyce637p8xvl9en1bx2lg086s

# 创建容器测试
[root@docker 001]# docker run -d --name c0 centos:httpd 
d398df1d2b235ad09068fa3b84b9ad2e0c31c03815982a92882609055a04d9ae

# 查看容器
[root@docker 001]# docker ps --all
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS         PORTS     NAMES
d398df1d2b23   centos:httpd   "/bin/bash /abc.sh"   10 seconds ago   Up 9 seconds             c0

# 访问测试
[root@docker 001]# curl 172.17.0.2
httpd server is running

# 容器挂载
[root@docker 001]# docker run -d -v /opt/:/var/www/html/ -p80 --name c1 centos:httpd
cb8c1626ce9a6ea8986c40f0efa4f657192ff7babf9aecd4acbfcc115f7e5d83
[root@docker 001]# ls /opt/
assets  containerd  favicon.ico  index.html

# 写测试内容
[root@docker 001]# echo "adsfadfafwqrdfqdsafdgdfsgda" > /opt/index.html

# 访问测试
[root@docker 001]# curl 172.17.0.2
adsfadfafwqrdfqdsafdgdfsgda

# 查看容器
[root@docker 001]# docker ps -all
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS          PORTS                                     NAMES
cb8c1626ce9a   centos:httpd   "/bin/bash /abc.sh"   22 seconds ago   Up 22 seconds   0.0.0.0:32768->80/tcp, :::32768->80/tcp   c1

浏览器访问:192.168.2.30:32768

2.使用Dockerfile创建yum镜像

# 修改Dockerfile文件
[root@docker 001]# vim Dockerfile 
FROM    centos:latest

MAINTAINER      "centos httpd server"

RUN     rm -rf /etc/yum.repos.d/*

RUN     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

RUN     yum clean all && yum makecache

RUN     yum -y install epel-release

# 运行文件创建镜像
[root@docker 001]# docker build -t centos:yum .
[+] Building 0.0s (9/9) FINISHED                                       docker:default
 => [internal] load build definition from Dockerfile                             0.0s
 => => transferring dockerfile: 385B                                             0.0s
 => [internal] load metadata for docker.io/library/centos:latest                 0.0s
 => [internal] load .dockerignore                                                0.0s
 => => transferring context: 2B                                                  0.0s
 => [1/5] FROM docker.io/library/centos:latest                                   0.0s
 => CACHED [2/5] RUN     rm -rf /etc/yum.repos.d/*                               0.0s
 => CACHED [3/5] RUN     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirr  0.0s
 => CACHED [4/5] RUN     yum clean all && yum makecache                          0.0s
 => CACHED [5/5] RUN     yum -y install epel-release                             0.0s
 => exporting to image                                                           0.0s
 => => exporting layers                                                          0.0s
 => => writing image sha256:ee9375ac855c3a1f8ec3eb03cee9317c3127e4be47cb3d6f519  0.0s
 => => naming to docker.io/library/centos:yum                                    0.0s
 
# 查看镜像
[root@docker 001]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       httpd     ebbf109944f1   5 minutes ago   338MB
centos       yum       ee9375ac855c   5 minutes ago   272MB
centos       latest    5d0da3dc9764   2 years ago     231MB

# 查看docker磁盘使用情况
[root@docker 002]# du -sh /var/lib/docker/overlay2/
1.1G	/var/lib/docker/overlay2/

3.使用Dockerfile创建nginx镜像

# 创建目录
[root@docker ~]# mkdir 002

# 进到002目录下
[root@docker ~]# cd 002/

# 编辑Dockerfile文件
[root@docker 002]# vim Dockerfile

# 这里使用之前创建的yum镜像
FROM    centos:yum 

MAINTAINER      维护世界和平

RUN     yum -y install nginx

RUN     yum -y install epel-release

RUN     echo "daemon off;" >> /etc/nginx/nginx.conf

EXPOSE  80

CMD     /usr/sbin/nginx

# 运行Dockerfile文件-创建nginx镜像
[root@docker 002]# docker build -t centos:nginx .
[+] Building 80.1s (8/8) FINISHED                                      docker:default
 => [internal] load build definition from Dockerfile                             0.0s
 => => transferring dockerfile: 280B                                             0.0s
 => [internal] load metadata for docker.io/library/centos:yum                    0.0s
 => [internal] load .dockerignore                                                0.0s
 => => transferring context: 2B                                                  0.0s
 => [1/4] FROM docker.io/library/centos:yum                                      0.0s
 => [2/4] RUN YUM -y install nginx                                              73.0s
 => [3/4] RUN YUM -y install epel-release                                        6.5s
 => [4/4] RUN ECHO "daemon" > /etc/nginx/nginx.conf                              0.1s 
 => exporting to image                                                           0.4s 
 => => exporting layers                                                          0.4s 
 => => writing image sha256:c7c536aca7946e111b2af5f1d2806bdac9f1e9a59d8bd05ccac  0.0s 
 => => naming to docker.io/library/centos:nginx                                  0.0s 
 
# 查看镜像
[root@docker 002]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       nginx     c7c536aca794   58 seconds ago   422MB
centos       httpd     ebbf109944f1   29 minutes ago   338MB
centos       yum       ee9375ac855c   29 minutes ago   272MB
centos       latest    5d0da3dc9764   2 years ago      231MB

# 使用nginx镜像创建容器c1
[root@docker 002]# docker run -d --name nc centos:nginx 
34bf6e6f1ecfcb0e1f717385a3553fe058bab65caa4603280951d98976efebe6

# 查看创建的容器
[root@docker 002]# docker ps --all
CONTAINER ID   IMAGE          COMMAND                   CREATED             STATUS             PORTS                                     NAMES
34bf6e6f1ecf   centos:nginx   "/bin/sh -c /usr/sbi..."   8 seconds ago       Up 7 seconds       80/tcp                                    nc
4c437591541d   centos:nginx   "/bin/sh -c /usr/sbi..."   4 minutes ago       Up 4 minutes       80/tcp                                    ng
cb8c1626ce9a   centos:httpd   "/bin/bash /abc.sh"       About an hour ago   Up About an hour   0.0.0.0:32768->80/tcp, :::32768->80/tcp   c1
d398df1d2b23   centos:httpd   "/bin/bash /abc.sh"       About an hour ago   Up About an hour                                             c0

三、私有仓库

1.安装registry

# 查看安装的镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
centos       nginx     4a14f7d33da9   8 minutes ago       422MB
centos       httpd     ebbf109944f1   About an hour ago   338MB
centos       yum       ee9375ac855c   About an hour ago   272MB
registry     latest    cfb4d9904335   10 months ago       25.4MB
centos       latest    5d0da3dc9764   2 years ago         231MB

2.创建容器,映射端⼝,挂载⽂件

# 创建挂载目录
[root@docker ~]# mkdir /regist

# 查看容器--全部删掉
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 创建容器,映射端⼝,挂载⽂件
[root@docker ~]# docker run -d --name r1 -v /regist/:/var/lib/registry -p5000:5000 registry:latest
4cd2b4d2c49fb4bdc6cf64aacea6c72a38f2c0eb3beccc6ed6845447b081a06f

# 查看创建的容器
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE             COMMAND                   CREATED         STATUS         PORTS                                       NAMES
4cd2b4d2c49f   registry:latest   "/entrypoint.sh /etc..."   3 seconds ago   Up 2 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   r1

# 访问目录页
[root@docker ~]# curl localhost:5000/v2/_catalog
{"repositories":[]}

3.浏览器访问

192.168.2.30:5000/v2/_catalog

4.配置pull和push,修改daemon.json文件

[root@docker ~]# vim /etc/docker/daemon.json

{
    "registry-mirrors": [
        "https://do.nark.eu.org",
        "https://dc.j8.work",
        "https://docker.m.daocloud.io",
        "https://dockerproxy.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://docker.nju.edu.cn"
    ],
        "hosts":  [
                  "tcp://0.0.0.0:2375",
                  "unix:///var/run/docker.sock"
        ],
        "insecure-registries":[
                "http://192.168.2.30:5000"

        ]
}

5.修改了daemon.json文件,重启服务

[root@docker ~]# systemctl restart docker.service 

6.启动registry容器

# 查看容器
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE             COMMAND                   CREATED          STATUS                     PORTS     NAMES
4cd2b4d2c49f   registry:latest   "/entrypoint.sh /etc..."   10 minutes ago   Exited (2) 2 minutes ago             r1

# 启动r1容器
[root@docker ~]# docker start r1
r1

# 访问⽬录⻚
[root@docker ~]# curl localhost:5000/v2/_catalog
{"repositories":[]}

7.给yum镜像打标签

# 为要上传的镜像添加标记
[root@docker ~]# docker tag centos:yum 192.168.2.30:5000/centos:yum

# 查看打标签的镜像
[root@docker ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
centos                     nginx     4a14f7d33da9   33 minutes ago   422MB
centos                     httpd     ebbf109944f1   2 hours ago      338MB
192.168.2.30:5000/centos   yum       ee9375ac855c   2 hours ago      272MB
centos                     yum       ee9375ac855c   2 hours ago      272MB
registry                   latest    cfb4d9904335   10 months ago    25.4MB
centos                     latest    5d0da3dc9764   2 years ago      231MB

8.将打标签的镜像上传到registry上

[root@docker ~]# docker push 192.168.2.30:5000/centos:yum
The push refers to repository [192.168.2.30:5000/centos]
8c519ad003b5: Pushed 
acbca50d3a83: Pushed 
75cdf155cf76: Pushed 
07d4b334a739: Pushed 
74ddd0ec08fa: Pushed 
yum: digest: sha256:fa3b19efa5dd7e95f2ea17a3d3ffd2b22ffedc3ddef9af6a69e60239db568a6c size: 1366

9.在主机上进行访问

[root@docker ~]# curl localhost:5000/v2/_catalog
{"repositories":["centos"]}

10.浏览器访问

192.168.2.30:5000/v2/_catalog

11.查看regist文件

[root@docker ~]# ls /regist/
docker
[root@docker ~]# cd /regist/docker/
[root@docker docker]# ls
registry
[root@docker docker]# cd registry/
[root@docker registry]# ls
v2
[root@docker registry]# cd v2
[root@docker v2]# ls
blobs  repositories
[root@docker v2]# cd repositories/
[root@docker repositories]# ls
centos
[root@docker repositories]# cd centos/
[root@docker centos]# ls
_layers  _manifests  _uploads
[root@docker centos]# cd _layers/
[root@docker _layers]# ls
sha256
[root@docker _layers]# cd sha256/
[root@docker sha256]# ls
30902bbca4a1d948703fd88d834ddf4dc8db69bb7e787ff18a762b6b4761fe37
46aacd2dd64664a37b01ee3a9f37561ed9b34c5af6661b880ee07a15d5fe8fd6
5a3d9ba04912ac6b1353410a0f18cc206b4920888e3187443abacefe3c9086ab
a1d0c75327776413fa0db9ed3adcdbadedc95a662eb1d360dad82bb913f8a1d1
ca847d29e1079f24cfc92b68cf37d5866060814df67cbdcd83edb16f1dc4fe54
ee9375ac855c3a1f8ec3eb03cee9317c3127e4be47cb3d6f519d9e6921a6a044
相关推荐
咖喱鱼蛋4 分钟前
Ubuntu安装Electron环境
linux·ubuntu·electron
ac.char8 分钟前
在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
linux·ubuntu·npm
肖永威14 分钟前
CentOS环境上离线安装python3及相关包
linux·运维·机器学习·centos
tian2kong17 分钟前
Centos 7 修改YUM镜像源地址为阿里云镜像地址
linux·阿里云·centos
Code_Artist17 分钟前
使用Portainer来管理并编排Docker容器
docker·云原生·容器
mengao123419 分钟前
centos 服务器 docker 使用代理
服务器·docker·centos
布鲁格若门21 分钟前
CentOS 7 桌面版安装 cuda 12.4
linux·运维·centos·cuda
Eternal-Student26 分钟前
【docker 保存】将Docker镜像保存为一个离线的tar归档文件
运维·docker·容器
不是二师兄的八戒28 分钟前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
C-cat.28 分钟前
Linux|进程程序替换
linux·服务器·microsoft