跳至主要內容

docker安装nexus3

tanmantang原创大约 4 分钟dockernexusmaven

提示

centos系统安装nexus3, 请跳转centos7搭建nexus3

Docker 安装 nexus3

准备工作

拉取镜像

提示

以下操作必须在能联网的服务器上,如果需要在离线环境下安装,需先从能联网的服务器上拉取 然后通过docker save -o nexus3.tar sonatype/nexus3把镜像打成tar包,将tar包拷贝到离线服务器 最后通过docker load -i nexus3.tar 解压成镜像

拉取镜像命令

docker pull sonatype/nexus3

查看拉取的镜像

docker images

创建本地挂载目录

为了方便存储nexus数据,防止数据丢失

mkdir -p /data/docker/nexus3/data

创建启动脚本{#script}

编辑脚本

cd /data/docker/nexus3/
vim create_nexus.sh

将以下内容保存到脚本

#!/bin/bash

# 设置容器名
name="nexus3"

# 若存在相同容器名,则删除该容器
docker rm -f $name

# 启动一个新的容器
docker run --name $name --restart=always --privileged=true \
 -p 8081:8081 -p 8082:8082 -p 8083:8083 \
 -v /data/docker/nexus/data:/nexus-data \
 -v /etc/localtime:/etc/localtime \
 -d sonatype/nexus3:latest
 
 ## 参数说明
 # --restart=always 在重启docker后,保持自启
 # --name 容器名
 # -v 映射文件地址  服务器文件地址:容器内部文件地址

保存后,给两个脚本添加可执行权限

chmod +x create_nexus.sh

构建容器

./create_nexus.sh

查看查看日志是否启动,如下,能看到端口,日志未报错即可

问题解决

测试容器

浏览器打开页面 http://IP:8081

默认登录账号:admin

查看登录的默认密码

less data/admin.password
# 复制密码后,按q退出

登录后会强制要求你修改密码

然后会要求你配置匿名访问

启用匿名访问意味着默认情况下,用户可以在没有凭据的情况下从存储库中搜索、浏览和下载组件。请考虑对您的组织的安全影响。

应谨慎选择禁用匿名访问,因为它需要所有用户和/或构建工具的凭据。

这个选项是配置是否开启匿名访问, 默认是禁用的。 如果你开启的话,用户无需凭据即可从存储库中搜索、浏览和下载组件。个人建议: 如果是放在局域网的话可以启用,不用去配置maven的setting文件,如果是放在公网建议禁用。

问题解决

不能正常访问

请检查你的防火墙,若是开启的状态,请开放防火墙端口

firewall-cmd --zone=public --permanent --add-port=8081/tcp
firewall-cmd --reloa

关闭防火墙

# 查看防火墙状态
firewall-cmd --state
# 停止firewall
systemctl stop firewalld.service
# 禁止firewall开机启动
systemctl disable firewalld.service

Permission denied

若提示以下信息(截取部分)

点击打开查看错误详情
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to No such file or directory

Warning:  Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning:  Forcing option -XX:LogFile=/tmp/jvm.log
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000717000000, 1890582528, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890582528 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /opt/sonatype/nexus/hs_err_pid1.log
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
Warning:  Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning:  Forcing option -XX:LogFile=/tmp/jvm.log
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to No such file or directory

OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000717000000, 1890582528, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890582528 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /tmp/hs_err_pid1.log
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to No such file or directory

Warning:  Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning:  Forcing option -XX:LogFile=/tmp/jvm.log
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000717000000, 1890582528, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890582528 bytes for committing reserved memory.
# Can not save log file, dump to screen..
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890582528 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
#  Out of Memory Error (os_linux.cpp:2799), pid=1, tid=0x00007fbcbec44700
#
# JRE version:  (8.0_282-b08) (build )
# Java VM: OpenJDK 64-Bit Server VM (25.282-b08 mixed mode linux-amd64 compressed oops)
# Core dump written. Default location: /opt/sonatype/nexus/core or core.1
#

解决方法

docker stop nexus3
docker rm nexus3

启动命令添加 --privileged=true 参数

--privileged,该参数可以设置是否给docker容器特权,如果该参数为true,使得docker容器内的root权限为宿主机的root权限,而非只是容器内的root权限

请参考本文启动脚本

然后重新安装nexus3