1. 程式人生 > >Docker 環境:Nexus3.x 的私有倉庫

Docker 環境:Nexus3.x 的私有倉庫

Nexus3.x 的私有倉庫

使用 Docker 官方的 Registry 建立的倉庫面臨一些維護問題。比如某些映象刪除以後空間預設是不會回收的,需要一些命令去回收空間然後重啟 Registry 程式。在企業中把內部的一些工具包放入 Nexus 中是比較常見的做法,最新版本 Nexus3.x 全面支援 Docker 的私有映象。所以使用 Nexus3.x 一個軟體來管理 Docker , Maven , Yum , PyPI 等是一個明智的選擇。

啟動 Nexus 容器

$ docker run -d --name nexus3 --restart=always \
    -p 8081:8081 \
    --mount src=nexus-data,target=/nexus-data \
    sonatype/nexus3

等待 3-5 分鐘,如果 nexus3 容器沒有異常退出,那麼你可以使用瀏覽器開啟 http://YourIP:8081 訪問 Nexus 了。

第一次啟動 Nexus 的預設帳號是 admin 密碼是 admin123 登入以後點選頁面上方的齒輪按鈕進行設定。

(PS:我執行到這 nexus3 容器顯示是啟動狀態,但瀏覽器一直訪問不成功,埠已開,檢視日誌才發現報錯了:

2018-07-02 01:34:34,175+0000 ERROR [FelixStartLevel] *SYSTEM org.sonatype.nexus.internal.orient.OrientBootstrap - Failed transition: NEW -> STARTED
java.lang.reflect.InvocationTargetException: null
        at org.sonatype.nexus.common.stateguard.MethodInvocationAction.run(MethodInvocationAction.java:45)
        at org.sonatype.nexus.common.stateguard.StateGuard$TransitionImpl.run(StateGuard.java:191)
        at org.sonatype.nexus.common.stateguard.TransitionsInterceptor.invoke(TransitionsInterceptor.java:56)
        at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:77)
        at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:55)
        at org.sonatype.nexus.internal.orient.OrientBootstrap$$EnhancerByGuice$$221620cd.start(<generated>)
        at org.sonatype.nexus.extender.NexusLifecycleManager.startComponent(NexusLifecycleManager.java:155)
        at org.sonatype.nexus.extender.NexusLifecycleManager.to(NexusLifecycleManager.java:95)
        at org.sonatype.nexus.extender.NexusContextListener.frameworkEvent(NexusContextListener.java:189)
        at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1429)
        at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.OutOfMemoryError: null
        at sun.misc.Unsafe.allocateMemory(Native Method)
        at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:127)
        at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
        at com.orientechnologies.common.directmemory.OByteBufferPool.allocateBuffer(OByteBufferPool.java:410)
        at com.orientechnologies.common.directmemory.OByteBufferPool.acquireDirect(OByteBufferPool.java:361)
        at com.orientechnologies.orient.core.storage.cache.local.OWOWCache.load(OWOWCache.java:841)
        at com.orientechnologies.orient.core.storage.cache.local.twoq.O2QCache.updateCache(O2QCache.java:1167)
        at com.orientechnologies.orient.core.storage.cache.local.twoq.O2QCache.doLoad(O2QCache.java:361)
        at com.orientechnologies.orient.core.storage.cache.local.twoq.O2QCache.allocateNewPage(O2QCache.java:412)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation.commitChanges(OAtomicOperation.java:434)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperationsManager.endAtomicOperation(OAtomicOperationsManager.java:468)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperationsManager.endAtomicOperation(OAtomicOperationsManager.java:412)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurableComponent.endAtomicOperation(ODurableComponent.java:113)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster.create(OPaginatedCluster.java:192)
        at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.addClusterInternal(OAbstractPaginatedStorage.java:4365)
        at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.doAddCluster(OAbstractPaginatedStorage.java:4346)
        at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.create(OAbstractPaginatedStorage.java:487)
        at com.orientechnologies.orient.core.storage.impl.local.paginated.OLocalPaginatedStorage.create(OLocalPaginatedStorage.java:129)
        at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.create(ODatabaseDocumentTx.java:438)
        at com.orientechnologies.orient.server.OSystemDatabase.init(OSystemDatabase.java:160)
        at com.orientechnologies.orient.server.OSystemDatabase.<init>(OSystemDatabase.java:44)
        at com.orientechnologies.orient.server.OServer.initSystemDatabase(OServer.java:1309)
        at com.orientechnologies.orient.server.OServer.activate(OServer.java:367)
        at org.sonatype.nexus.internal.orient.DatabaseServerImpl.doStart(DatabaseServerImpl.java:186)
        at org.sonatype.nexus.common.stateguard.StateGuardLifecycleSupport.start(StateGuardLifecycleSupport.java:67)
        at org.sonatype.nexus.common.stateguard.MethodInvocationAction.run(MethodInvocationAction.java:39)
        at org.sonatype.nexus.common.stateguard.StateGuard$TransitionImpl.run(StateGuard.java:191)
        at org.sonatype.nexus.common.stateguard.TransitionsInterceptor.invoke(TransitionsInterceptor.java:56)
        at org.sonatype.nexus.internal.orient.OrientBootstrap.doStart(OrientBootstrap.java:73)
        at org.sonatype.nexus.common.stateguard.StateGuardLifecycleSupport.start(StateGuardLifecycleSupport.java:67)
        at org.sonatype.nexus.common.stateguard.MethodInvocationAction.run(MethodInvocationAction.java:39)
        at org.sonatype.nexus.common.stateguard.StateGuard$TransitionImpl.run(StateGuard.java:191)
        at org.sonatype.nexus.common.stateguard.TransitionsInterceptor.invoke(TransitionsInterceptor.java:56)
        ... 6 common frames omitted

原因不明,如果你解決了請給我講解一下。)

建立倉庫

建立一個私有倉庫的方法: Repository->Repositories 點選右邊選單 Create repository 選擇 docker (hosted)

  • Name: 倉庫的名稱
  • HTTP: 倉庫單獨的訪問埠
  • Enable Docker V1 API: 如果需要同時支援 V1 版本請勾選此項(不建議勾選)。
  • Hosted -> Deployment pollcy: 請選擇 Allow redeploy 否則無法上傳 Docker 映象。

其它的倉庫建立方法請各位自己摸索,還可以建立一個 docker (proxy) 型別的倉庫連結到 DockerHub 上。再建立一個 docker (group) 型別的倉庫把剛才的 hosted 與 proxy 新增在一起。主機在訪問的時候預設下載私有倉庫中的映象,如果沒有將連結到 DockerHub 中下載並快取到 Nexus 中。

新增訪問許可權

選單 Security->Realms 把 Docker Bearer Token Realm 移到右邊的框中儲存。

新增使用者規則:選單 Security->Roles->Create role 在 Privlleges 選項搜尋 docker 把相應的規則移動到右邊的框中然後儲存。

新增使用者:選單 Security->Users->Create local user 在 Roles 選項中選中剛才建立的規則移動到右邊的視窗儲存。

NGINX 加密代理

證書的生成請參見 私有倉庫高階配置 裡面證書生成一節。

NGINX 示例配置如下

upstream register
{
    server "YourHostName OR IP":5001; #埠為上面新增的私有映象倉庫是設定的 HTTP 選項的埠號
    check interval=3000 rise=2 fall=10 timeout=1000 type=http;
    check_http_send "HEAD / HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_4xx;
}

server {
    server_name YourDomainName;#如果沒有 DNS 伺服器做解析,請刪除此選項使用本機 IP 地址訪問
    listen       443 ssl;

    ssl_certificate key/example.crt;
    ssl_certificate_key key/example.key;

    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    client_body_buffer_size 512k;
    proxy_connect_timeout 600;
    proxy_read_timeout   600;
    proxy_send_timeout   600;
    proxy_buffer_size    128k;
    proxy_buffers       4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 512k;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://register;
        proxy_read_timeout 900s;

    }
    error_page   500 502 503 504  /50x.html;
}

Docker 主機訪問映象倉庫

如果不啟用 SSL 加密可以通過前面章節的方法新增信任地址到 Docker 的配置檔案中然後重啟 Docker

使用 SSL 加密以後程式需要訪問就不能採用修改配置的訪問了。具體方法如下:

$ openssl s_client -showcerts -connect YourDomainName OR HostIP:443 </dev/null 2>/dev/null|openssl x509 -outform PEM >ca.crt
$ cat ca.crt | sudo tee -a /etc/ssl/certs/ca-certificates.crt
$ systemctl restart docker

使用 docker login YourDomainName OR HostIP 進行測試,使用者名稱密碼填寫上面 Nexus 中生成的。

轉自:https://yeasy.gitbooks.io/docker_practice/repository/nexus3_registry.html