Dockerfile+私有仓库
使用Dockerfile创建应用镜像
通过dockerfile创建⼀个在启动容器时,就可以启动httpd服务的镜像
[root@docker ~]# mkdir httpd0
[root@docker ~]# cd httpd0
编辑启动脚本:启动http服务
[root@docker httpd0]# vim abc.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /sbin/httpd -D FOREGROUND
[root@docker httpd0]# ls
abc.sh
编辑index.html文件:
[root@docker httpd0]# echo "httpd server is running" > index.html
[root@docker httpd0]# ls
abc.sh index.html
编辑Dockerfile
[root@docker httpd0]#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 httpd0]# docker build -t centos:httpd .
查看镜像
创建容器测试
[root@docker httpd0]#docker run -d --name c0 centos:httpd
e50856f7eee27e8aa8d9803ffd11f4b8f02c67bce489165f39f9251acf143f6b
[root@docker httpd0]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e50856f7eee2 centos:httpd "/bin/bash /abc.sh" 8 seconds ago Up 7 seconds c0
查看IP地址
[root@docker httpd0]#docker inspect c0
[root@docker httpd0]#curl 172.17.0.3
httpd server is running
通过Dockerfile创建yum镜像
在上述的配置(通过Dockerfile创建httpd镜像)中进行修改:
[root@docker httpd0]#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 httpd0]# docker build -t centos:yum .
[root@docker httpd0]# docker run -it --name c1 centos:yum /bin/bash
crtl+p+q
[root@docker httpd0]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e36a888b2a24 centos:yum "/bin/bash" 29 seconds ago Up 28 seconds c1
e50856f7eee2 centos:httpd "/bin/bash /abc.sh" 23 minutes ago Up 23 minutes c0
通过Dockerfile创建nginx镜像
利用已经存在的yum镜像来创建nginx镜像
[root@docker ~]#mkdir 002
[root@docker ~]#cd 002
[root@docker 002]# vim Dockerfile
FROM centos:yum
MAINTAINER 和平
RUN yum -y install epel-release
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 c2 centos:nginx
f18c2b57f40f442726b1837d4c28f96dcb17c4c812ecfbac532d4ae2692fc98b
[root@docker 002]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f18c2b57f40f centos:nginx "/bin/sh -c /usr/sbi…" 12 seconds ago Up 11 seconds 80/tcp c2
私有仓库
1.安装仓库镜像
# 创建挂载目录
[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.118.55: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.118.55: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.118.55:5000/centos:yum
# 查看打标签的镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.118.50:5000/centos yum ee9375ac855c 2 hours ago 272MB
centos yum ee9375ac855c 2 hours ago 272MB
registry latest cfb4d9904335 10 months ago 25.4MB
8.将打标签的镜像上传
[root@docker ~]# docker push 192.168.118.55:5000/centos:yum
9.在主机上进行访问
[root@docker ~]# curl localhost:5000/v2/_catalog
{"repositories":["centos"]}
10.浏览器访问:192.168.118.55: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
测试:
在docker上上传镜像(192.168.118.55)
[root@docker ~]# systemctl start docker
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.118.55:5000/centos yum 11c7f617cd4a 17 hours ago 272MB
centos yum 11c7f617cd4a 17 hours ago 272MB
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f0f62812f33 registry:latest "/entrypoint.sh /etc…" 17 hours ago Exited (255) About a minute ago 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp r1
[root@docker ~]# docker start r1
r1
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f0f62812f33 registry:latest "/entrypoint.sh /etc…" 17 hours ago Up 33 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp r1
[root@docker ~]# netstat -lnput | grep 5000
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 1600/docker-proxy
tcp6 0 0 :::5000 :::* LISTEN 1604/docker-proxy
新建一台docker机器,作为客户端来拉取刚在docker主机上上传的镜像
12 sh docker.sh //安装docker的脚本
17 vim /etc/docker/daemon.json
20 systemctl restart docker
21 docker pull 192.168.118.55:5000/centos:yum
[root@docker1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.118.55:5000/centos yum 11c7f617cd4a 18 hours ago 272MB
[root@docker1 ~]# docker run -d --name c5 192.168.118.55:5000/centos:yum
2fca882e6fbb4818f436b3b202b0cfab8ebffd5540f25fa6cba062b6db9f5ff9
[root@docker1 ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fca882e6fbb 192.168.118.55:5000/centos:yum "/bin/bash" About a minute ago Exited (0) About a minute ago c5