Sonatype Nexus Repository 是一款支援多種協定的 Artifacts Management 成品管理工具,
下圖出自:Sonatype Platform Integrations | Sonatype
Docker Image 現在已經應用程式產出的標準配備,想要利用 Nexus 建立 Private Docker Image Registry,當我要 Push Docker Image 時碰到了一些小亂流特此記錄下操作步驟。
開發環境
- Windows 11 Home
- Rider 2024.2.7
- nexus 3.74.0.05
安裝 Nexus
參考:sonatype/nexus3 - Docker Image | Docker Hub
docker run -d -p 8081:8081 -p 8082:8082 --name nexus sonatype/nexus3
取得 admin 密碼
docker exec -it nexus cat /nexus-data/admin.password
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker exec -it nexus cat /nexus-data/admin.password
15f6b9c8-e3cc-4ad8-b529-c9c8830d875e
What's next:
Try Docker Debug for seamless, persistent debugging tools in any container or image → docker debug nexus
Learn more at https://docs.docker.com/go/debug-cli/
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker>
帳號:admin,成功登入後會要求修改密碼
建立 docker hosted
建立 HTTP connector
docker repository 有三種模式
- hosted:建立私有 Docker registry
- proxy:指向其他的 Docker registry,例如 docker hub - https://hub.docker.com/
- group:將 hosted 與 proxy 整合一起,提供一個單一入口進行操作
在 nexus 建立 docker image
建立 Dockerfile
新增一個 Web API 專案,用 Rider 新增 Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj", "Lab.Nexus.Docker.WebApi/"]
RUN dotnet restore "Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj"
COPY . .
WORKDIR "/src/Lab.Nexus.Docker.WebApi"
RUN dotnet build "Lab.Nexus.Docker.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Lab.Nexus.Docker.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Lab.Nexus.Docker.WebApi.dll"]
docker build
用 Windows Terminal 切到方案目錄,執行 docker build
docker build -t localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev -f ./Lab.Nexus.Docker.WebApi/Dockerfile .
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker build -t localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev -f ./Lab.Nexus.Docker.WebApi/Dockerfile .
[+] Building 13.1s (18/18) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 850B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:8.0 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 380B 0.0s
=> [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:cab0284cce7bc26d41055d0ac5859a69a8b75d9a201cd226999f4f00cc983f13 0.0s
=> => resolve mcr.microsoft.com/dotnet/sdk:8.0@sha256:cab0284cce7bc26d41055d0ac5859a69a8b75d9a201cd226999f4f00cc983f13 0.0s
=> [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:8.0@sha256:3ded9ccf06f222ec934311be4f9facda83d144331c028340e3a694733cad7d4b 0.0s
=> => resolve mcr.microsoft.com/dotnet/aspnet:8.0@sha256:3ded9ccf06f222ec934311be4f9facda83d144331c028340e3a694733cad7d4b 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 544B 0.0s
=> CACHED [build 2/7] WORKDIR /src 0.0s
=> CACHED [build 3/7] COPY [Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj, Lab.Nexus.Docker.WebApi/] 0.0s
=> [build 4/7] RUN dotnet restore "Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj" 6.3s
=> [build 5/7] COPY . . 0.1s
=> [build 6/7] WORKDIR /src/Lab.Nexus.Docker.WebApi 0.0s
=> [build 7/7] RUN dotnet build "Lab.Nexus.Docker.WebApi.csproj" -c Release -o /app/build 4.6s
=> [publish 1/1] RUN dotnet publish "Lab.Nexus.Docker.WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false 1.3s
=> CACHED [base 2/2] WORKDIR /app 0.0s
=> CACHED [final 1/2] WORKDIR /app 0.0s
=> [final 2/2] COPY --from=publish /app/publish . 0.1s
=> exporting to image 0.4s
=> => exporting layers 0.2s
=> => exporting manifest sha256:5d69377f53634931bbb0ca0d901f2efe0fe246dcf702d3781b5c73f66a8d8d53 0.0s
=> => exporting config sha256:b44f3da2bf52f9760377a1dc9bbf954e07ae51e4506468323da33f90cebd88cf 0.0s
=> => exporting attestation manifest sha256:036a42bc9e337ad1ace39c2e45bf3034a0beac1886ad375bccb9ddb093866bcc 0.0s
=> => exporting manifest list sha256:a4ba92c5a44d71665981f040690e94bf71661388546ae028eb81fa149a53d935 0.0s
=> => naming to localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev 0.0s
=> => unpacking to localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev 0.1s
What's next:
View a summary of image vulnerabilities and recommendations → docker scout quickview
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker>
登入nexus
docker login -u admin -p admin localhost:8082
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker login -u admin -p admin localhost:8082
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
docker push
docker push localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker push localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
The push refers to repository [localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi]
a6ffd88031f8: Pushed
6fdcff8f9d6d: Pushed
eb08848e1820: Pushed
6a3e92c1a691: Pushed
ec1c931922f8: Pushed
a480a496ba95: Pushed
dbd7d5267d16: Pushed
4b48b4779698: Pushed
64d5ab8f4e7b: Pushed
4f4fb700ef54: Pushed
dev: digest: sha256:2d5c800fd809aa67713aa982c7e4b8109b572c4cf63f4a0c7375e9c1f58bca80 size: 856
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker>
docker rmi
要試著能不能從 private docker registry 下載 docker image,我先刪除本機的 image
docker rmi localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker rmi localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
Untagged: localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
Deleted: sha256:2d5c800fd809aa67713aa982c7e4b8109b572c4cf63f4a0c7375e9c1f58bca80
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker>
docker run
試著從 private docker registry 啟動容器
docker run -it -p 8080:8080 --name demo-container localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> docker run -it -p 8080:8080 --name demo-container localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
Unable to find image 'localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev' locally
dev: Pulling from repository/docker-hosted/lab.nexus.docker.webapi
6a3e92c1a691: Already exists
6fdcff8f9d6d: Already exists
4f4fb700ef54: Already exists
Digest: sha256:2d5c800fd809aa67713aa982c7e4b8109b572c4cf63f4a0c7375e9c1f58bca80
Status: Downloaded newer image for localhost:8082/repository/docker-hosted/lab.nexus.docker.webapi:dev
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:8080
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app
呼叫 API
curl -X 'GET' 'http://localhost:8080/weatherforecast'
PowerShell 7.4.6
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker> curl -X 'GET' 'http://localhost:8080/weatherforecast'
[{"date":"2024-11-18","temperatureC":28,"summary":"Chilly","temperatureF":82},{"date":"2024-11-19","temperatureC":45,"summary":"Scorching","temperatureF":112},{"date":"2024-11-20","temperatureC":41,"summary":"Mild","temperatureF":105},{"date":"2024-11-21","temperatureC":-6,"summary":"Hot","temperatureF":22},{"date":"2024-11-22","temperatureC":-12,"summary":"Warm","temperatureF":11}]
PS D:\lab\sample.dotblog\Docker\Lab.Nexus.Docker>
範例位置
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET