搭建docker本地私有仓库

一:准备工作如下:

1、建立相关文件夹:

[root@iZ28bi7khfvZ registry]# pwd
/laughing/registry
[root@iZ28bi7khfvZ registry]# ls
config.yml docker

2、上传配置文件config.yml至服务器/laughing/registry目录下,内容如下:

[root@iZ28bi7khfvZ registry]# vi config.yml

version: 0.1
log:
fields:
service: registry
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: :5000
headers:
X-Content-Type-Options: [nosniff] health:
storagedriver:
enabled: true
interval: 10s
threshold: 3

3、从官方仓库中pull下registry镜像:

docker pull registry

二:开始运行registry镜像

运行registry镜像时要挂载仓库目录,挂载配置文件,指定端口,如下:

docker run -d -p 5000:5000 -v /laughing/registry/:/var/lib/registry -v /laughing/registry/config.yml:/etc/docker/registry/config/yml –name registry –restart=always registry

运行上条指令后可看到registry容器已经启动啦:

[root@iZ28bi7khfvZ registry]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47602f950bf9 registry “/entrypoint.sh /etc…” 37 minutes ago Up 30 minutes 0.0.0.0:5000->5000/tcp registry

三:测试

例如将本地已有的镜像上传至仓库中:

将本地镜像打标签;

docker tag base:jdktomcat IP:5000/base:jdktomcat

然后推送到私有仓库中:

docker push IP:5000/base:jdktomcat

此时出现错误:http: server gave HTTP response to HTTPS client

因为docker与docker registry默认交互使用https,而我们搭建的私有仓库使用http服务

因此需要在服务器上配置如下:

vim /usr/lib/systemd/system/docker.service

找到ExecStart=/usr/bin/dockerd这一行 在后面追加  –insecure-registry IP:5000

保存后需要重启docker

systemctl daemon-reload

ststemctl restart docker.service

此时再次执行docker push IP:5000/base:jdktomcat

发现已可推送成功

通过查看仓库镜像来确认下:

[root@iZ28bi7khfvZ registry]# curl -get http://IP:5000/v2/_catalog
{“repositories”:[“base”]}

[root@iZ28bi7khfvZ registry]# curl -get http://IP:5000/v2/base/tags/list
{“name”:”base”,”tags”:[“jdktomcat”]}

至此我们已确认私有仓库搭建完成!

参考链接:https://www.cnblogs.com/Tempted/p/7768694.html

创建第一个基础镜像

说明:使用Centos及jdk1.7、tomcat7.0制作基础镜像。baseos:1.0中主要为了调整时区

注意在启动容器时,指定端口映射-p 8080:8080

一:首先创建空白的centos基础镜像

docker pull cetos

docker tag cetos:latest centos:base

二:目录结构如下

[root@iZ28bi7khfvZ laughing]# ls
baseos jdktomcat
[root@iZ28bi7khfvZ laughing]# pwd
/laughing
[root@iZ28bi7khfvZ laughing]# ls
baseos jdktomcat

三:在baseos文件夹下创建Dockerfile,并制作镜像

[root@iZ28bi7khfvZ laughing]# cd baseos/
[root@iZ28bi7khfvZ baseos]# ls
Dockerfile
[root@iZ28bi7khfvZ baseos]# cat Dockerfile
#Baseosimage
From centos:base
MAINTAINER Laughing_Lz 645596034@qq.com
LABEL Description=”this image is the baseos image.” Version=”1.0″
ENV TIME_ZONE Asia/Shanghai
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo ‘Asia/Shanghai’ >/etc/timezone

制作镜像如下:

docker build -t base:1.0 .

四:在jdktomcat文件夹下创建Dockerfile,内容如下

[root@iZ28bi7khfvZ laughing]# cd jdktomcat/
[root@iZ28bi7khfvZ jdktomcat]# ls
apache-tomcat-7.0.88.tar.gz Dockerfile jdk-7u79-linux-x64.tar.gz
[root@iZ28bi7khfvZ jdktomcat]# cat Dockerfile
From base:1.0
MAINTAINER Laughing_Lz 645596034@qq.com

ADD jdk-7u79-linux-x64.tar.gz /usr/local
ADD apache-tomcat-7.0.88.tar.gz /usr/local

ENV JAVA_HOME /usr/local/jdk1.7.0_79
ENV CLASSPATH $JAVA_HOME/lib /dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.88
ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.88
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD [ “/usr/local/apache-tomcat-7.0.88/bin/catalina.sh”, “run” ]

五:以第四步的Dockerfile及tomcat和jdk的安装包制作镜像

docker build -t base:jdktomcat .

六:以第五步制作的镜像启动容器

docker run -d -p 8080:8080 –name=”zero” base:jdktomcat

七:进入容器查看:

docker exec -it  [ContainID] /bin/bash