一、dockerfile应用
[root@docker ~]# mkdir http0
[root@docker ~]# cd http0/
[root@docker http0]# vim abc.sh
[root@docker http0]# ls
abc.sh
[root@docker http0]# vim abc.sh
#!/bin/bash
rm -rf /run/*httpd*
exec /sbin/httpd -D FOREGROUND
[root@docker http0]# echo "http server is running" > index.html
[root@docker http0]# vim Dockerfile
FROM centos:latest
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
CMD ["/bin/bash","/abc.sh"]
[root@docker http0]# ls
abc.sh Dockerfile index.html
[root@docker http0]# docker build -t centos:http0 .
[root@docker http0]# docker run -d --name c0 -p80:80 centos:http0
[root@docker ~]# mkdir 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]# echo "abc" > index.html
[root@docker 001]# ls
abc.sh index.html
[root@docker 001]# vim Dockerfile
FROM centos:latest
MAINTAINER "centos http 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]# docker build centos:httpd .
[root@docker 001]# docker run -d --name a0 centos:httpd
[root@docker 001]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce92cdc405ef centos:httpd "/bin/bash /abc.sh" 5 seconds ago Up 5 seconds a0
[root@docker 001]# curl 172.17.0.2
abc
[root@docker 001]# docker run -d -v /opt/:/var/www/html/ -p80 --name a1 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
27088f8c757f centos:httpd "/bin/bash /abc.sh" 2 minutes ago Up 2 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp a1
ce92cdc405ef centos:httpd "/bin/bash /abc.sh" 6 minutes ago Up 6 minutes a0
[root@docker 001]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd c18a3916c55a 54 minutes ago 338MB
centos yum f19cf4c62358 55 minutes ago 272MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker docker]# du -sh /var/lib/docker/overlay2/
343M /var/lib/docker/overlay2/
[root@docker ~]# mkdir 002
[root@docker ~]# cd 002/
[root@docker 002]# vim Dockerfile
FROM centos:yum
RUN yum -y install nginx
RUN echo "daemon" >> /etc/nginx/nginx.conf
CMD /usr/sbin/nginx
[root@docker 002]# docker build -t centos:nginx .
[root@docker 002]# docker run -d --name c0 centos:nginx
[root@docker 002]# curl 172.17.0.2
二、创建私有仓库,在企业中分享项目
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
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50ebe3b52ba9 registry:latest "/entrypoint.sh /etc…" 5 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp r1
4、访问仓库
[root@docker 002]# curl localhost:5000/v2/_catalog
[root@docker 002]# curl http://192.168.8.196:5000/v2/_catalog
{"repositories":[]}
5、配置pull,push 修改daemon.json文件
[root@docker 002]# 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.196:5000"
]
}
[root@docker 002]# systemctl restart docker
6、启动容器并测试
[root@docker 002]# docker start r1
r1
[root@docker 002]# curl localhost:5000/v2/_catalog
{"repositories":[]}
7、为要上传的镜像添加标记
[root@docker ~]# docker tag centos:yum 192.168.8.196:5000/centos:yum
[root@docker ~]# docker push 192.168.8.196:5000/centos:yum
[root@docker ~]# curl localhost:5000/v2/_catalog
{"repositories":["centos"]}