欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CLOUD:自定义镜像与仓库

程序员文章站 2022-03-14 08:57:59
...

自定义镜像

docker commit

使用镜像启动容器,在该容器基础修改,并另存为一个新的镜像

  • 在容器基础上修改yum源
docker run -it docker.io/centos
rm -rf /etc/yum.repos.d/*  
vi /etc/yum.repos.d/dvd.repo
[dvd]
name=dvd
baseurl=ftp://192.168.1.254/centos-7
enabled=1
gpgcheck=0

yum clean all
yum repolist
  • 安装测试软件
    yum -y install net-tools iproute psmisc vim-enhanced
  • 另存为另外一个镜像
docker ps -a #查看id
docker commit 8d07ecd7e345 docker.io/myos:latest 
  • 查看新建的镜像
docker images 
REPOSITORY           TAG             IMAGE ID          CREATED              SIZE
docker.io/myos     latest       87feda116c17         6 weeks ago         321.6 MB
docker.io/centos   latest       1e1148e4cc2c        4 months ago        201.8 MB

Dockerfile

  • 语法格式
    – FROM :基础镜像
    – MAINTAINER:镜像创建者信息(说明)
    – EXPOSE:开放的端口
    – ENV:设置变量
    – ADD:复制文件到镜像
    – RUN:制作镜像时执行的命令,可以有多个
    – WORKDIR:定义容器默认工作目录
    – CMD:容器启动时执行的命令,仅可以有一条CMD

创建一个Apache的镜像文件

通过rpm -ql httpd找到/usr/lib/systemd/system/httpd.service启动服务文件,发现启动是/usr/sbin/httpd -DFOREGROUND,和设置环境变量的文件
/etc/sysconfig/httpd

mkdir bulid
cd bulid
echo test > index.html
vim Dockerfile #Dockerfile文件第一个字母要大写
FROM  docker.io/myos:latest
RUN yum -y install httpd
ENV EnvironmentFile=/etc/sysconfig/httpd
WORKDIR /var/www/html/    #定义容器默认工作目录
ADD index.html index.html
EXPOSE 80       #设置开放端口号
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

docker build -t docker.io/myos:http .
docker run -d  docker.io/myos:http 
d9a5402709b26b42cd304c77be442559a5329dc784ec4f6c90e4abac1c88e206
docker inspect d9
curl 172.17.0.7
test

自定义镜像仓库

  • 共享镜像的一台服务器(镜像化的一台服务器)
    CLOUD:自定义镜像与仓库

自定义私有仓库

  • 定义一个私有仓库
yum -y install docker-distribution
systemctl start docker-distribution
systemctl enable docker-distribution
docker tag docker.io/busybox:latest 192.168.1.31:5000/docker.io/busybox:latest  
//打标签
docker push 192.168.1.31:5000/docker.io/busybox:latest  
//上传
docker tag docker.io/myos:http 192.168.1.31:5000/ docker.io/myos:http
docker push  192.168.1.31:5000/docker.io/myos:http
  • 客户端配置
vim /etc/sysconfig/docker
INSECURE_REGISTRY='--insecure-registry 192.168.1.31:5000'
ADD_REGISTRY='--add-registry 192.168.1.31:5000'
systemctl restart docker
docker run -it myos:http /bin/bash    
//直接启动

查看私有仓库

  • 查看里面有什么镜像
curl  http://192.168.1.31:5000/v2/_catalog
{"repositories":["docker.io/busybox","docker.io/myos"]}
  • 查看里面的镜像标签
curl  http://192.168.1.31:5000/v2/docker.io/busybox/tags/list 
{"name":"docker.io/busybox","tags":["latest"]}
curl  http://192.168.1.31:5000/v2/docker.io/myos/tags/list 
{"name":"docker.io/myos","tags":["http"]}

主机卷的映射

  • 将真实机目录挂载到容器中提供持久化存储
    – 目录不存在就自动创建
    – 目录存在就直接覆盖掉
docker run -v /data:/data -it docker.io/centos bash #将本机的data目录映射到容器的data目录中

NFS共享存储

NFS:192.168.1.30
docker1:192.168.1.31

配置NFS服务器

yum -y install nfs-utils
mkdir /content
vim /etc/exports
/content     *(rw,no_root_squash)
systemctl restart nfs-server.service
systemctl restart nfs-secure.service
]$ exportfs  -rv
exporting *:/content

chmod 777 /content
echo "my world" > /content/index.html

配置客户端

mount挂载共享,运行容器,-v映射到容器中

yum -y install nfs-utils
systemctl restart nfs-server.service
showmount -e 192.168.1.254
Export list for 192.168.1.254:
/content *

mkdir /mnt/qq
mount -t nfs 192.168.1.254:/content /mnt/qq
ls /mnt/qq
index.html
cat  /mnt/qq/index.html 
my world
docker run  -d -p 80:80 -v /mnt/qq:/var/www/html -it docker.io/myos:http
curl 192.168.1.31
my  world

创建自定义网桥

  • 新建docker网络模型
docker  network   create  --subnet=10.10.10.0/24  docker1  #新建模型
b447cacc0373631ff7c534f119047946be5c1498b5b2e31a31180c5ee6320ab5 
docker network list  #查看网络模型
NETWORK ID          NAME                DRIVER              SCOPE
996943486faa        bridge              bridge              local               
b447cacc0373        docker1             bridge              local               
63c88dcc3523        host                host                local               
5e5ab3d45e27        none                null                local 

docker  network   inspect   docker1
[
    {
        "Name": "docker1",
        "Id": "b447cacc0373631ff7c534f119047946be5c1498b5b2e31a31180c5ee6320ab5",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.10.10.0/24"
                }            ]
        },
        "Internal": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
  • 使用自定义网桥(docker1)启动容器
docker  run  --network=docker1   -itd   docker.io/myos
5270cba305c06c3da3f56185b35dc059aabcf2884a12ef717d89a768360e5326
docker  run  --network=docker1   -itd   docker.io/myos
4b4a4e8bebfbcc18a0deaa17225f0b5dec8c6d5d52e513617849c9579b0b1813

docker  network   inspect   docker1  //可以看到容器的ip
[
......
        "Internal": false,
        "Containers": {
            "4b4a4e8bebfbcc18a0deaa17225f0b5dec8c6d5d52e513617849c9579b0b1813": {
                "Name": "big_spence",
                "EndpointID": "d5894002a9fdfd65daf52473de1735ecdc32ef53832099afc1dcfa6e86a5e8f4",
                "MacAddress": "02:42:0a:0a:0a:03",
                "IPv4Address": "10.10.10.3/24",
                "IPv6Address": ""
            },
            "5270cba305c06c3da3f56185b35dc059aabcf2884a12ef717d89a768360e5326": {
                "Name": "infallible_lalande",
                "EndpointID": "492a8cdda204f23775978758f364d577783272c83cf1a5de2d20bf640f060f05",
                "MacAddress": "02:42:0a:0a:0a:02",
                "IPv4Address": "10.10.10.2/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
  • 使用默认网桥(docker0)启动容器
docker  run     -itd   docker.io/myos
63e99284b1a78d7d5fe17d25697424502054c59e0cc61b58c3070758fff1c35d
docker  run     -itd   docker.io/myos
f41cb77a6fe0574ce5b810498d6f42223e55d677df391d050a2901c678dfea3f
docker inspect -f '{{.NetworkSettings.IPAddress}}' f41
172.17.0.3
docker inspect -f '{{.NetworkSettings.IPAddress}}' 63e
172.17.0.2
相关标签: CLOUD