微服務--使用Nexus Repository Manager 3.0搭建私有Docker倉庫

微服務--使用Nexus Repository Manager 3.0搭建私有Docker倉庫

Posted on Jun 05, 2018 By Freud Kanghtml

 

下載

下載最新的Nexus Repository Manager 3.0, https://help.sonatype.com/repomanager3/download。並解壓在某個目錄,Windows下,官方不建議解壓在「Program Files」或者「Program Files (x86)」目錄,緣由是空格和特殊字符。我的習慣是在c盤下建立一個」programs」的目錄,專門安裝這種解壓便可用的軟件。linux

啓動

linux下執行

./nexus run

windows下執行

nexus.exe /run

啓動成功會打印以下信息

2018-06-05 10:56:15,582+0800 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@289fb2e9{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2018-06-05 10:56:15,582+0800 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.Server - Started @26802ms
2018-06-05 10:56:15,584+0800 INFO  [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer -
-------------------------------------------------
Started Sonatype Nexus OSS 3.12.0-01
-------------------------------------------------

登陸

Nexus啓動以後默認監聽8081端口,訪問http://localhost:8081docker

/images/blog/micro-service/02-nexus-repository/01-visit.png

而後在右上角點擊Sign in,使用默認admin/admin123用戶名和密碼登陸,多了一個管理的標籤,表明登陸成功了。json

/images/blog/micro-service/02-nexus-repository/02-login.png

建立Docker倉庫

在Nexus中Docker倉庫被分爲了三種bootstrap

+ hosted: 託管倉庫 ,私有倉庫,能夠push和pull 
+ proxy: 代理和緩存遠程倉庫 ,只能pull
+ group: 將多個proxy和hosted倉庫添加到一個組,只訪問一個組地址便可,只能pull

建立hosted repository

依次點擊管理BUTTON -》 Repository -》 Repositories -》 Create Repository -》 Docker(hosted), 而後在彈出的頁面中填寫以下信息。windows

/images/blog/micro-service/02-nexus-repository/03-create-docker-hosted.png

其中選擇Blob Store的時候是指想將相關內容存儲在什麼位置,若是不想存儲在default中,能夠先退出而後點擊左側Blob Stores -》 Create Blob Store先建立一個存儲位置緩存

/images/blog/micro-service/02-nexus-repository/04-create-blob-stores.png

建立proxy repository

依次點擊管理BUTTON -》 Repository -》 Repositories -》 Create Repository -》 Docker(proxy), 而後在彈出的頁面中填寫以下信息。服務器

/images/blog/micro-service/02-nexus-repository/05-create-docker-proxy.png

其中須要注意的是,在添加Proxy的Remote Storage的時候,須要選中Use certificates Stored in the Nexus truststore to connect to external systems, 而後點擊View Certificate, 點擊 Add certificate to truststoreeclipse

/images/blog/micro-service/02-nexus-repository/06-add-trust-store.png

建立group repository

依次點擊管理BUTTON -》 Repository -》 Repositories -》 Create Repository -》 Docker(group), 而後在彈出的頁面中填寫以下信息。curl

/images/blog/micro-service/02-nexus-repository/07-create-docker-group.png

Repository完工

至此,Nexus Docker Repository 部分配置完成了

Docker配置

因爲本地Window下的Docker有問題,因此我選擇的虛擬機下安裝Linux來執行Docker,具體的Docker安裝過程略過,本部分只涉及如何配置Docker從私服Pull和Push鏡像。

配置daemon.json

其中192.168.59.1指安裝Nexus的服務器地址

#vi /etc/docker/daemon.json
{
  "insecure-registries": [
    "192.168.59.1:8551",
    "192.168.59.1:8552",
    "192.168.59.1:8553"
  ],
  "disable-legacy-registry": true
}

重啓Docker

#重啓Docker服務
[root@localhost ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
#查看Docker服務運行狀態
[root@localhost ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-06-04 13:00:17 EDT; 5s ago

建立鏡像

[root@localhost docker]# mkdir docker
[root@localhost docker]# cd docker/
[root@localhost docker]# touch Dockerfile

[root@localhost docker]# vi Dockerfile
FROM debian
MAINTAINER Freud <luoyan35714@126.com>

CMD ["echo", "hello, this is freud!"]

[root@localhost docker]# docker build -t="hifreud" .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM debian
 ---> 8626492fecd3
Step 2/3 : MAINTAINER Freud <luoyan35714@126.com>
 ---> Running in 2695b8243d64
 ---> de27ed62ef24
Removing intermediate container 2695b8243d64
Step 3/3 : CMD echo hello, this is freud!
 ---> Running in ac5f9b2d1d01
 ---> 2e8231b2ab8d
Removing intermediate container ac5f9b2d1d01
Successfully built 2e8231b2ab8d
[root@localhost docker]# docker run hifreud
hello, this is freud!

Push鏡像

[root@localhost docker]# docker tag hifreud 192.168.59.1:8551/freud:latest
#登陸
[root@localhost docker]# docker login -u admin -p admin123 192.168.59.1:8551
Login Succeeded
#Docker login成功後會在本地的config.json文件寫入相應的認證信息
[root@ip-192-168-75-128 .docker]# cat ~/.docker/config.json 
{
  "auths": {
    "192.168.59.1:8551": {
      "auth": "YWRtaW46YWRtaW4xMjM="
    }
  }
}
[root@localhost docker]# docker push 192.168.59.1:8551/freud:latest
The push refers to a repository [192.168.59.1:8551/freud]
0f3a12fef684: Pushed 
latest: digest: sha256:f1a08c9b6066ecee674a56ea1effeb115dc0c31a4a741632730aa6d69abf7705 size: 529

登陸nexus查看上傳的鏡像

/images/blog/micro-service/02-nexus-repository/08-check-images.png

Pull鏡像

[root@localhost ~]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.59.1:8551/freud         latest              2e8231b2ab8d        About an hour ago   101 MB
hifreud                         latest              2e8231b2ab8d        About an hour ago   101 MB
[root@localhost ~]# docker rmi -f 192.168.59.1:8551/freud
Untagged: 192.168.59.1:8551/freud:latest
Deleted: sha256:2e8231b2ab8d72cb506328635593c0303e72ce51f3e5806a63c41a41ead552de
Deleted: sha256:de27ed62ef247b09066669c0ed71113069544957b8e83eb105495398cb1d22ac
[root@localhost ~]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
hifreud                         latest              2e8231b2ab8d        About an hour ago   101 MB
[root@localhost ~]# docker pull 192.168.59.1:8551/freud
Using default tag: latest
Trying to pull repository 192.168.59.1:8551/freud ... 
latest: Pulling from 192.168.59.1:8551/freud
cc1a78bfd46b: Already exists 
Digest: sha256:f1a08c9b6066ecee674a56ea1effeb115dc0c31a4a741632730aa6d69abf7705
Status: Downloaded newer image for 192.168.59.1:8551/freud:latest
[root@localhost ~]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.59.1:8551/freud         latest              2e8231b2ab8d        2 hours ago         101 MB
hifreud                         latest              2e8231b2ab8d        About an hour ago   101 MB

搜索鏡像

因爲在建立docker repository的時候,在Enable Docker V1 API的時候,並無選中Allow clients to use the V1 API to interact with this Repository , 因此經過docker search直接檢索private的repository或者images會報錯,以下。

[root@localhost ~]# docker search 192.168.59.1:8553/freud
Error response from daemon: Unexpected status code 404

解決辦法有兩個,一個是在nexus上修改repository配置,設置Enable Docker V1 API是選中狀態,另外一種是經過V2的API來訪問

#因爲咱們建立了Group Repository,因此此處IP能夠是`192.168.59.1:8551`,也能夠是`192.168.59.1:8553`
[root@localhost ~]# curl http://192.168.59.1:8551/v2/_catalog
{"repositories":["freud"]}
[root@localhost ~]# curl http://192.168.59.1:8553/v2/freud/tags/list
{"name":"freud","tags":["latest"]}

參考資料

Using Nexus 3 as Your Repository - Part 3: Docker Images: http://codeheaven.io/using-nexus-3-as-your-repository-part-3-docker-images/

sonatype nexus 3搭建Docker私有倉庫:http://www.javashuo.com/article/p-eeenkdif-b.html

Installation Methods: https://help.sonatype.com/repomanager3/installation/installation-methods

docker私有鏡像倉庫搭建 : https://www.2cto.com/kf/201702/594302.html

相關文章
相關標籤/搜索