Sonatype Nexus deployment docker installation nexus3

Nexus is a powerful Maven warehouse manager that greatly simplifies the maintenance of your own internal warehouse and access to external warehouses.

1. Check available Nexus3 versions
You can check other versions of nexus3 through Sort by. The default is the latest version sonatype/nexus3:latest.
https://hub.docker.com/r/sonatype/nexus3/tags?page=1 &name=3.30.1
Because the version used by the company is nexus3:3.30.1, we directly search and download this version.
Before deployment, jump through the dockerfile connection and look at the build code.
https://github.com/sonatype/docker-nexus3/blob/main/Dockerfile
Dockerfile

# Copyright (c) 2016-present Sonatype, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM registry.access.redhat.com/ubi8/ubi-minimal

LABEL name="Nexus Repository Manager" \
      maintainer="Sonatype <[email protected]>" \
      vendor=Sonatype \
      version="3.62.0-01" \
      release="3.62.0" \
      url="https://sonatype.com" \
      summary="The Nexus Repository Manager server \
          with universal support for popular component formats." \
      description="The Nexus Repository Manager server \
          with universal support for popular component formats." \
      run="docker run -d --name NAME \
          -p 8081:8081 \
          IMAGE" \
      stop="docker stop NAME" \
      com.sonatype.license="Apache License, Version 2.0" \
      com.sonatype.name="Nexus Repository Manager base image" \
      io.k8s.description="The Nexus Repository Manager server \
          with universal support for popular component formats." \
      io.k8s.display-name="Nexus Repository Manager" \
      io.openshift.expose-services="8081:8081" \
      io.openshift.tags="Sonatype,Nexus,Repository Manager"

ARG NEXUS_VERSION=3.62.0-01
ARG NEXUS_DOWNLOAD_URL=https://download.sonatype.com/nexus/3/nexus-${NEXUS_VERSION}-unix.tar.gz
ARG NEXUS_DOWNLOAD_SHA256_HASH=845ec18801f1ff36e1309141ebe69bd7ed0f417dac98e0756ebdc902e72036e1

# configure nexus runtime
ENV SONATYPE_DIR=/opt/sonatype
ENV NEXUS_HOME=${SONATYPE_DIR}/nexus \
    NEXUS_DATA=/nexus-data \
    NEXUS_CONTEXT=''\
    SONATYPE_WORK=${SONATYPE_DIR}/sonatype-work \
    DOCKER_TYPE='rh-docker'

# Install Java & tar
RUN microdnf update -y \
     & amp; & amp; microdnf --setopt=install_weak_deps=0 --setopt=tsflags=nodocs install -y \
          java-1.8.0-openjdk-headless tar procps shadow-utils gzip \
     & amp; & amp; microdnf clean all \
     & amp; & amp; groupadd --gid 200 -r nexus \
     & amp; & amp; useradd --uid 200 -r nexus -g nexus -s /bin/false -d /opt/sonatype/nexus -c 'Nexus Repository Manager user'

WORKDIR ${SONATYPE_DIR}

# Download nexus & setup directories
RUN curl -L ${NEXUS_DOWNLOAD_URL} --output nexus-${NEXUS_VERSION}-unix.tar.gz \
     & amp; & amp; echo "${NEXUS_DOWNLOAD_SHA256_HASH} nexus-${NEXUS_VERSION}-unix.tar.gz" > nexus-${NEXUS_VERSION}-unix.tar.gz.sha256 \
     & amp; & amp; sha256sum -c nexus-${NEXUS_VERSION}-unix.tar.gz.sha256 \
     & amp; & amp; tar -xvf nexus-${NEXUS_VERSION}-unix.tar.gz \
     & amp; & amp; rm -f nexus-${NEXUS_VERSION}-unix.tar.gz nexus-${NEXUS_VERSION}-unix.tar.gz.sha256 \
     & amp; & amp; mv nexus-${NEXUS_VERSION} $NEXUS_HOME \
     & amp; & amp; chown -R nexus:nexus ${SONATYPE_WORK} \
     & amp; & amp; mv ${SONATYPE_WORK}/nexus3 ${NEXUS_DATA} \
     & amp; & amp; ln -s ${NEXUS_DATA} ${SONATYPE_WORK}/nexus3

# Removing java memory settings from nexus.vmoptions since now we use INSTALL4J_ADD_VM_PARAMS
RUN sed -i '/^-Xms/d;/^-Xmx/d;/^-XX:MaxDirectMemorySize/d' $NEXUS_HOME/bin/nexus.vmoptions

RUN echo "#!/bin/bash" >> ${SONATYPE_DIR}/start-nexus-repository-manager.sh \
    & amp; & amp; echo "cd /opt/sonatype/nexus" >> ${SONATYPE_DIR}/start-nexus-repository-manager.sh \
    & amp; & amp; echo "exec ./bin/nexus run" >> ${SONATYPE_DIR}/start-nexus-repository-manager.sh \
    & amp; & amp; chmod a + x ${SONATYPE_DIR}/start-nexus-repository-manager.sh \
    & amp; & amp; sed -e '/^nexus-context/ s:$:${NEXUS_CONTEXT}:' -i ${NEXUS_HOME}/etc/nexus-default.properties

RUN microdnf remove -y gzip shadow-utils

VOLUME ${NEXUS_DATA}

EXPOSE 8081
USER nexus

ENV INSTALL4J_ADD_VM_PARAMS="-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m -Djava.util.prefs.userRoot=${NEXUS_DATA}/javaprefs"

CMD ["/opt/sonatype/nexus/bin/nexus", "run"]



2. Because the user in the dockerfile is nexus and the mounting directory is a file with root permissions, the mounting directory permissions must be granted.
#Create folder
mkdir -p /data/nexus/nexus-data
#Release permissions
chmod 777 -R /data/nexus
#Enter the directory to view
cd /data/nexus
2. Alternatively, you can create a nexus user.

Create Nexus users and groups
mkdir -p /data/nexus/nexus-data
useradd -r -s /sbin/nologin nexus
groupadd nexus
usermod -aG nexus nexus
Modify Nexus directory permissions
chown -R nexus:nexus /data/nexus
chmod -R 777 /data/nexus
docker pull sonatype/nexus3:3.30.1

docker run --restart=always -p 8081:8081 --name nexus3 -e TZ=Asia/Shanghai -v /data/nexus/nexus-data:/nexus-data -d sonatype/nexus3:3.30.1

explain:
--restart=always The container automatically restarts after docker restarts
--name: container name
The container's time zone setting is specified through the `-e` parameter and `TZ=Asia/Shanghai`. This method is more flexible and can dynamically specify the time zone when starting different containers.
-v: Map the directory in the container to the local machine. The previous path is the local machine path.
-d: image name

3. Log in to the page and change the password
IP + port access can directly see the effect:
nexus settings and pom file usage
The default login account for nexus is admin, and the default password needs to be obtained inside the container (the red box is the password, don’t get it wrong! Log in to nexus after taking out the password):
Copy after login
docker exec -it nexus /bin/bash #Enter the container
cat /nexus-data/admin.password #View password


This option is to configure whether to enable anonymous access, which is disabled by default. If you turn it on, users can search, browse, and download components from the repository without requiring credentials. Personal suggestion: If it is placed on the local area network, it can be enabled without configuring the maven setting file. If it is placed on the public network, it is recommended to disable it.
Set the proxy to Alibaba warehouse
Click once as shown below:

After entering, find Proxy->Remote storage, replace the value with http://maven.aliyun.com/nexus/content/groups/public/ and save it, as shown below:

pom file reference
Add the following code:

<repositories>
<!-- Configure the nexus remote warehouse and disable anonymous access. The ID needs to be consistent with the setting file -->
<repository>
<id>nexus</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.0.138:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
         </repositories>
----------------------------------

If you have enabled anonymous access, it has been configured now. If you have disabled anonymous access, you need to create a user (or use the administrator user directly but it is not safe) and configure it in the maven settings file:

<server>
      <id>This should correspond to the id in the pom file</id>
      <username>your account</username>
      <password>your password</password>
   </server>

4. Upload the jar package
Upload via web page

After uploading, click view it now to view the jar package and reference code.