Docker构建私有仓库
程序员文章站
2024-03-12 22:43:03
...
1.部署私有仓库应用
仓库镜像
Docker hub官方已提供容器镜像registry,用于搭建私有仓库
1.拉取镜像
[[email protected] ~]# docker pull daocloud.io/library/registry:latest
2.运行容器
[[email protected] ~]# docker run --restart=always -d -p 5000:5000 daocloud.io/library/registry
-p 指定端口
--restart=always 重启docker服务时该容器不会关闭
-d 后台运行
3.查看镜像及其ip
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00140cb3c38c daocloud.io/library/registry "/entrypoint.sh /etc…" 10 seconds ago Up 9 seconds 0.0.0.0:5000->5000/tcp wizardly_ride
[[email protected] ~]# docker inspect 00140cb3c38c | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
4.在本机查看能否访问该私有仓库, 看看状态码是不是200
[[email protected] ~]# curl -I 172.17.0.2:5000
HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Fri, 24 Apr 2020 13:38:50 GMT
5.查看镜像用于上传到仓库
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_docker v1 d9510c5285ad 2 days ago 452MB
daocloud.io/library/registry latest 708bc6af7e5e 3 months ago 25.8MB
如果你没有可用的镜像,可以拉取一个如:
[[email protected] ~]# docker pull busybox #为了方便,下载1个比较小的镜像
6.上传前必须给镜像打tag 注明ip和端口
[[email protected] ~]# docker tag nginx_docker:v1 192.168.13.150:5000/nginx
# nginx_docker:v1 对应我们拉取镜像的REPOSITORY和TAG
# 192.168.13.150:5000/nginx 前面是IP和端口号,nginx是我们自定义的名字
7.创建配置文件
[[email protected] ~]# vim /etc/docker/daemon.json
{ "insecure-registries": ["192.168.13.150:5000"] }
# 如果你之前用过加速器,需要用逗号隔开
[[email protected] ~]# systemctl restart docker #重启服务
8.上传镜像到私有仓库
[[email protected] ~]# docker images #查看我们创建的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.13.150:5000/nginx latest d9510c5285ad 2 days ago 452MB
nginx_docker v1 d9510c5285ad 2 days ago 452MB
daocloud.io/library/registry latest 708bc6af7e5e 3 months ago 25.8MB
[[email protected] ~]# docker push 192.168.13.150:5000/nginx #上传
9.查看私有仓库里的镜像
[[email protected] ~]# curl 192.168.13.150:5000/v2/_catalog
{"repositories":["nginx"]}
2.容器固定IP
docker安装后,默认会创建三种网络类型,bridge、host和none
显示当前网络
[[email protected] ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
a43bb9d2ad66 bridge bridge local
ba3d37ddba81 host host local
a9edbcbe2e0c none null local
===============================================================
bridge:网络桥接
默认情况下启动、创建容器都是用该模式,所以每次docker容器重启时会按照顺序获取对应ip地址,这就导致容器每次重启,ip都发生变化
none:无指定网络
启动容器时,可以通过–network=none,docker容器不会分配局域网ip
host:主机网络
docker容器的网络会附属在主机上,两者是互通的。
===============================================================
创建固定ip容器
1.创建自定义网络类型,并且指定网段
[[email protected] ~]# docker network create --subnet=192.168.30.0/24 customnet
[[email protected] ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
a43bb9d2ad66 bridge bridge local
9d90df98d668 customnet bridge local
ba3d37ddba81 host host local
a9edbcbe2e0c none null local
多了一个我们自定义的类型
2.使用新的网络类型创建并启动容器
[[email protected] ~]# docker run -it --name custom --net customnet --ip 192.168.30.2 centos:7 /bin/bash
[[email protected] /]# hostname -I
192.168.30.2
或者在容器外面查看
[[email protected] ~]# docker inspect custom | grep IPAddres
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "192.168.30.2",
上一篇: Java 数据结构链表操作实现代码