== Nexus3
[#sonatype-nexus3-docker-arm64-install-requirement]
=== 安装前准备
由于 nexus 没有官方提供支持 arm 架构的 docker 镜像，我们需要自行构建镜像，在构建之前，需要准备好构建镜像相关的文件:

. 需要从官网下载 nexus-${version}-unix.tar.gz 文件
. link:resources/files/9999-appendix/docker/nexus/Dockerfile[Dockerfile]

TIP:: 直接访问 https://www.sonatype.com/thanks/repo-oss 可直接进入下载页面

以下是 Dockerfile 文件内容及说明:
[source,bash]
----
# Download, extract Nexus to /tmp/sonatype/nexus
FROM eclipse-temurin:8-jre-jammy as downloader

ARG NEXUS_VERSION=3.51.0-01     <1>
ARG NEXUS_DOWNLOAD_URL=https://download.sonatype.com/nexus/3/nexus-${NEXUS_VERSION}-unix.tar.gz

# Download Nexus and other stuff we need later
# Use wget to improve performance (#11)
# Install wget
RUN apt update && apt install -y wget
# Download + extract Nexus to "/tmp/sonatype/nexus" for use later

#RUN wget --quite --output-document=/tmp/nexus.tar.gz "${NEXUS_DOWNLOAD_URL}" && \ <2>
#    mkdir /tmp/sonatype && \
#    tar -zxf /tmp/nexus.tar.gz -C /tmp/sonatype && \
#    mv /tmp/sonatype/nexus-${NEXUS_VERSION} /tmp/sonatype/nexus && \
#    rm /tmp/nexus.tar.gz

COPY ./nexus-3.51.0-01-unix.tar.gz /tmp/nexus.tar.gz  <3>

RUN \
    mkdir /tmp/sonatype && \
    ls /tmp && \
    tar -zxf /tmp/nexus.tar.gz -C /tmp/sonatype && \
    mv /tmp/sonatype/nexus-${NEXUS_VERSION} /tmp/sonatype/nexus && \
    rm /tmp/nexus.tar.gz

# Runtime image
# Logic adapted from official Dockerfile
# https://github.com/sonatype/docker-nexus3/blob/master/Dockerfile
FROM eclipse-temurin:8-jre-jammy

# Image metadata
# git commit
LABEL org.opencontainers.image.revision="-"
LABEL org.opencontainers.image.source="https://github.com/klo2k/nexus3-docker"

# Setup: Rename App, Data and Work directory per official image
# App directory (/opt/sonatype/nexus)
COPY --from=downloader /tmp/sonatype /opt/sonatype
RUN \
    # Data directory (/nexus-data)
    mv /opt/sonatype/sonatype-work/nexus3 /nexus-data && \
    # Work directory (/opt/sonatype/sonatype-work/nexus3)
    ln -s /nexus-data /opt/sonatype/sonatype-work/nexus3

# Fix-up: Startup command line: Remove hard-coded memory parameters in /opt/sonatype/nexus/bin/nexus.vmoptions (per official Docker image)
RUN sed -i '/^-Xms/d;/^-Xmx/d;/^-XX:MaxDirectMemorySize/d' /opt/sonatype/nexus/bin/nexus.vmoptions

# Enable NEXUS_CONTEXT env-variable via nexus-default.properties
RUN sed -i -e 's/^nexus-context-path=\//nexus-context-path=\/\${NEXUS_CONTEXT}/g' /opt/sonatype/nexus/etc/nexus-default.properties

# Create Nexus user + group, based on official image:
#   nexus:x:200:200:Nexus Repository Manager user:/opt/sonatype/nexus:/bin/false
#   nexus:x:200:nexus
RUN groupadd --gid 200 nexus && \
    useradd \
      --system \
      --shell /bin/false \
      --comment 'Nexus Repository Manager user' \
      --home-dir /opt/sonatype/nexus \
      --no-create-home \
      --no-user-group \
      --uid 200 \
      --gid 200 \
      nexus

# Data directory "/nexus-data" owned by "nexus" user
RUN chown -R nexus:nexus /nexus-data

# Data volume
VOLUME /nexus-data

EXPOSE 8081

USER nexus

# Default environment variables, adapted from upstream Dockerfile
ENV NEXUS_HOME=/opt/sonatype/nexus \
    NEXUS_DATA=/nexus-data \
    NEXUS_CONTEXT='' \
    SONATYPE_WORK=/opt/sonatype/sonatype-work \
    # Low `-Xms`, `-Xmx` default for Raspberry Pi
    INSTALL4J_ADD_VM_PARAMS="-Xms1200m -Xmx1200m -XX:MaxDirectMemorySize=2g -Djava.util.prefs.userRoot=/nexus-data/javaprefs"

CMD ["/opt/sonatype/nexus/bin/nexus", "run"]
----
<1> 指定需要构建的 nexus 版本号
<2> 如果能够正常访问 https://download.sonatype.com 网站下载需要的 nexus 安装程序
<3> 直接使用事先通过其他途径获取的 nexus 安装程序

=== 构建支持 linux/arm64 镜像
以下将以 nexus-3.51.0-01 版本为例:

[source,bash]
----
# 1. 创建用户构建 docker 镜像工作目录
mkdir /Users/wangshaoping/wspsc/software/docker/Dockerfile/nexus3-docker-arm64

# 2. 将事先准备好的 nexus-3.51.0-01-unix.tar.gz 文件复制到上面新建的镜像工作目录中
# 3. 在镜像工作目录中新建名为 Dockerfile 文件, 文件内容可从上一节中直接下载

# 4. 构建镜像
docker buildx build --pull \
  --platform "linux/arm64" \
  --tag "sonatype/nexus3-arm64:3.51.0-01" \
  --output=type=docker \
  .
----

=== save 新构建的镜像
[source,bash]
----
# 保存新构建的镜像成 tar 文件以便后续直接 load 使用
docker save -o nexus3-3.51.0-01-arm64.tar sonatype/nexus3-arm64:3.51.0-01

# load 镜像 tar 文件
docker load --input nexus3-3.51.0-01-arm64.tar
----

=== 启动容器
[source,bash]
----
docker run \
  --name nexus \
  -d \
  -p 8000:8081 \
  -v /Users/wangshaoping/wspsc/software/docker/volume/nexus_v3:/nexus-data \
  sonatype/nexus3-arm64:3.51.0-01

# 需要等待一定的时间,可通过以下命令查看日志
docker logs -f nexus

# 当控制台出现类似以下内容时,表示容器已经启动好了。
2023-04-13 11:23:21 -------------------------------------------------
2023-04-13 11:23:21
2023-04-13 11:23:21 Started Sonatype Nexus OSS 3.51.0-01
2023-04-13 11:23:21
2023-04-13 11:23:21 -------------------------------------------------
----

=== 管理员用户名密码
[source,bash]
----
# 默认登录用户名:admin
# 默认登录密码: 存储于 /nexus-data/admin.password 文件中,当登录后重新设置用户名和密码后该文件消失
----
