2024.8.27(Dokerfile的应用、私有仓库)

一、Dockerfile应用

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

[root@docker ~]# mkdir httpd0

[root@docker ~]# cd httpd0

[root@docker httpd0]# vim abc.sh

[root@docker httpd0]# ls

abc.sh

[root@docker httpd0]# echo " httpd server is running" > index.html

[root@docker httpd0]# ls

abc.sh  index.html

[root@docker httpd0]# vim Dockerfile

FROM    centos:latest
MAINTAINER      "你好"
RUN     rm -rf /etc/yum.repos.d/* && 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 httpd
ADD     index.html /var/www/html/index.html
ADD     abc.sh /abc.sh
RUN     chmod -v +x /abc.sh
EXPOSE  80
CMD     ["/bin/bash","/abc.sh"]

[root@docker httpd0]# docker build -t centos:httpd0 .

[root@docker httpd0]# docker images

[root@docker httpd0]# docker run -d --name c0 -p80:80 centos:httpd0 
 

2、http容器

[root@docker ~]# rm -rf /var/lib/docker/
[root@docker ~]# systemctl restart docker.service 
[root@docker ~]# docker pull centos

[root@docker ~]# mkdir 001
[root@docker ~]# cd 001
[root@docker 001]# vim 001.sh

#!/bin/bash
rm -rf /run/httpd/*
/sbin/httpd -D FOREGROUND

[root@docker 001]# echo "hdsoaih" > index.html
[root@docker 001]# ls
001.sh  index.html
[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     001.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]# docker build -t centos:httpd .[root@docker 001]# docker run -d --name c0 centos:httpd
fed08b58b46c04fd4bf7bd6d1fc59f9484ec98ab963c05ef3092baeb856ac108
[root@docker 001]# docker ps --all

CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS          PORTS     NAMES
fed08b58b46c   centos:httpd   "/bin/bash /abc.sh"   11 seconds ago   Up 11
seconds             c0

[root@docker 001]# curl 172.17.0.2
hdsoaih
[root@docker 001]# docker run -d -v /opt/:/var/www/html/ -p80 --name a0 centos:httpd
[root@docker 001]# ls /opt/
assets  containerd  favicon.ico  index.html
[root@docker 001]# docker ps --all

CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS          PORTS                                     NAMES
284d24e98215   centos:httpd   "/bin/bash /abc.sh"   17 seconds ago   Up 16 seconds   0.0.0.0:32768->80/tcp, :::32768->80/tcp   a0
fed08b58b46c   centos:httpd   "/bin/bash /abc.sh"   2 minutes ago    Up 2 minutes                                              c0

[root@docker 001]# du -sh /var/lib/docker/overlay2/
343M    /var/lib/docker/overlay2/

3、nginx容器

[root@docker ~]# mkdir 002
[root@docker ~]# cd 002

[root@docker 002]# vim Dockerfile

FROM    centos:yum
MAINTAINER      你好
RUN     yum -y install nginx
RUN     echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE  80
CMD     /usr/sbin/nginx

[root@docker 002]# docker build -t centos:nginx .

[root@docker 002]# docker run -d --name c0 centos:nginx

二、创建私有仓库

1、拉取registry

[root@docker 002]# docker pull registry

2、创建挂载目录

[root@docker 002]# mkdir /regist

3、启动容器,映射端口,挂载目录

[root@docker 002]# docker run -d --name r1 -v /regist/:/var/lib/registry -p5000:5000 registry:latest
[root@docker 002]# docker ps --all

CONTAINER ID   IMAGE             COMMAND                   CREATED          STATUS          PORTS                                       NAMES
2aa193212a61   registry:latest   "/entrypoint.sh /etc…"   7 seconds ago    Up 7 seconds    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   r1
4、访问仓库 

[root@docker 002]# curl localhost:5000/v2/_catalog    catalog:目录
{"repositories":[]}

 5、配置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.8.168:5000"
        ]
}

[root@docker ~]# systemctl restart docker

6、启动registry

[root@docker ~]# docker start r1
[root@docker ~]# curl localhost:5000/v2/_catalog

7、为要上传的镜像添加标记

[root@docker ~]# docker tag centos:yum 192.168.8.168:5000/centos:yum

[root@docker ~]# docker images

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值