Docker 安裝與Registry V2私有倉庫搭建

Docker 是一個開源的容器引擎,可讓開發者把應用以及依賴打包到一個可移植的容器中。Docker 採用Go語言編寫,當時學習Go語言的時候已經知道這個神器,可是一直沒有合適的場景正式使用,如今機會來啦。linux

Registry 是Docker 鏡像的倉庫,使用Registry 能搭建私有的Docker倉庫,方便分發自定義鏡像。docker

環境

本文的內容在如下版本中測試經過,不一樣的版本可能存在不一樣的地方,如下內容僅供參考。
vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@CentOS72-40 /]
CentOS Linux release 7.2.1511 (Core)

[root@CentOS72-40 /]
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built:
OS/Arch: linux/amd64

Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built:
OS/Arch: linux/amd64

[root@CentOS72-40 /]
REPOSITORY TAG IMAGE ID CREATED SIZE
registry 2.5.1 c9bd19d022f6 4 weeks ago 33.27 MB

Docker 安裝

安裝Docker 須要內核是3.10 以上64位的Linux 系統。查看Linux 內核版本,CentOS7.2是沒問題的。(如今Mac 和 Windows 均可以安裝Docker)
centos

1
2
[root@CentOS72-40 ~]# uname -r
3.10.0-327.36.3.el7.x86_64

CentOS 7 以後的默認防火牆是firewall,習慣使用iptables 作防火牆,因此仍是修改成iptables 作防火牆。
中止firewall ,而後禁止firewall 開機啓動。
tomcat

1
2
[root@CentOS72-40 ~]
[root@CentOS72-40 ~]

檢查iptables 的狀態,若沒有安裝iptables 服務則須要安裝iptables-services。
tcp

1
2
3
4
5
6
[root@CentOS72-40 ~]
· iptables.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)

[root@CentOS72-40 ~]# yum install iptables-services

編輯iptables 防火牆文檔,開放22和5000端口(5000端口供Registry對外提供服務)。
學習

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@CentOS72-40 ~]# vim /etc/sysconfig/iptables

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5000 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

設置iptables 防火牆開機啓動,啓動防火牆使更改的配置生效。
測試

1
2
[root@CentOS72-40 ~]# systemctl enable iptables
[root@CentOS72-40 ~]# systemctl start iptables

建立Docker 的yum 的倉庫。
ui

1
2
3
4
5
6
7
8
[root@CentOS72-40 ~]# tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

安裝docker-engine 會自動安裝依賴 docker-engine-selinux。
this

大專欄  Docker 安裝與Registry V2私有倉庫搭建">
1
[root@CentOS72-40 ~]# yum install docker-engine

設置開機啓動docker,而後啓動docker。

1
2
[root@CentOS72-40 ~]# systemctl enable docker
[root@CentOS72-40 ~]# systemctl start docker

安裝完docker以後將會發現建立docker0網卡。

1
2
3
4
5
6
7
8
[root@CentOS72-40 /]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:2b:79:c2:73 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

配置Registry

拉取2.5.1版本的Registry 鏡像。

1
[root@CentOS72-40 /]# docker pull registry:2.5.1

啓動Registry鏡像。根據Registry的文檔把/tmp/registry-dev 目錄映射出來是有問題的,真正的數據不是存儲再這個目錄。不知道是不新版的存在這個目錄,反正2.5.1版的Registry 數據不是保存在這裏,進入容器發現真正存儲鏡像的時在/var/lib/registry 目錄,因此把這個目錄映射出來。(跟着官方文檔竟然也走不通,鬱悶,會不會是官方故意挖個坑讓你們踩的呢)

1
[root@CentOS72-40 /]# docker run -d -p 5000:5000 -v /data/registry:/var/lib/registry --name registry registry:2.5.1

打開http://172.18.2.40:5000/v2/_catalog 驗證是否安裝成功。

如果啓用iptables的時候沒有放開5000端口,在registry 運行的時候再放開。以後停了registry 從新打開registry 就會有下面的錯誤。不單單是Registry這個容器存在這個問題,全部容器的在運行中,主機的iptables 被修改過,停了容器再啓動都會有這個問題。目前沒有找到好的解決版本,可是從新啓動docker 服務 systemclt restart docker 能夠臨時解決這個問題。

1
2
Error response from daemon: driver failed programming external connectivity on endpoint registry (4b748e54bb59d8bfe1f1bd961c6f0fcfbd8ec94c75c5d4880134bd2a453b0d51): iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5000 -j DNAT --to-destination 172.17.0.2:5000 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)

使用Registry

對已經存在的鏡像wendyeq/tomcat8 打上帶Registry地址的tag,版本號位1.0。而後推送到Registry,發現推送不了,緣由是Registry V2採用https協議。

1
2
3
4
[root@M-CentOS72-36 ~]# docker tag wendyeq/tomcat8 172.18.2.40:5000/wendyeq/tomcat8:1.0
[root@M-CentOS72-36 ~]# docker push 172.18.2.40:5000/wendyeq/tomcat8:1.0
The push refers to a repository [172.18.2.40:5000/wendyeq/tomcat8]
Get https://172.18.2.40:5000/v1/_ping: http: server gave HTTP response to HTTPS client

解決這個問題的最簡單方式是修改docker的啓動腳本,添加-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock --insecure-registry 172.18.2.40:5000 內容。

1
2
3
4
5
6
7
8
[root@M-CentOS72-36 ~]# vim /usr/lib/systemd/system/docker.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock --insecure-registry 172.18.2.40:5000

修改完以後就能夠推送到Registry 的私有倉庫啦。

1
2
3
4
5
6
[root@M-CentOS72-36 ~]# systemctl start docker
Warning: docker.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@M-CentOS72-36 ~]# systemctl daemon-reload
[root@M-CentOS72-36 ~]# systemctl start docker
[root@M-CentOS72-36 ~]# docker push 172.18.2.40:5000/wendyeq/tomcat8
The push refers to a repository [172.18.2.40:5000/wendyeq/tomcat8]

總結

Docker 和 Registry 的安裝都比較簡單,可是Registry查找鏡像好麻煩,特別是查看版本號,要好好研究一下Registry 的文檔才行。

相關文章
相關標籤/搜索