我是这样使用SpringBoot(nexus3搭建docker本
公司的APP项目采用的ionic版本太低了,现在公司研究团队在重新开发整个APP。近段时间实在是压力山大,天天加班。我也就一直在做前端的工作,只能每周周末抽点时间来更新。比之前慢了。
在上一章中,将项目镜像提交到阿里云的镜像服务器中,再从服务器中pull到发布机中。有的时候,企业更愿意把镜像放到自己机房,再把本地服务器开放端口到外网部署。我想docker项目该是互联网中部署的吧,不开放端口到外网意义似乎不大。这一章就来描述下我是怎么在本地机房使用nexus3搭建docker服务器,之后开放到外网的。恩,我用的不是端口映射,是用内网穿透。
服务器环境
我选了一台centos7的服务器来搭建这个环境,我一直是用centos的服务器,对其它服务器的部署情况不了解。其它系统的朋友就只能作为参考了。
既然主题是docker,我也来新研究下,docker部署nexus3。
centos7安装docker
现在docker分为CE(开源版)和EE(企业版),如果centos中用yum install docker命令安装的话,版本有点低。我就遇到了因为docker版本不对,php:7.2-apache运行报错的问题,这是题外话。我安装的是docker-ce。照着官方文档操作,我找的这个机子系统是刚昨天装的,新环境跳过删除旧版的操作(官方文档地址https://docs.docker.com/install/linux/docker-ce/centos/
)。
- 安装安装存储驱动
yum install -y yum-utils device-mapper-persistent-data lvm2
- 添加云源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- 安装docker-ce
yum install docker-ce
- 启动docker
systemctl start docker
- 设置为开机自启动
docker会部署许多服务,有的服务是希望开机运行的,把docker设置为开机启动还是有必要的
sytemctl enable docker
- 查看docker版本
docker version
关于docker 中国官方镜像加速的问题可以查看https://www.docker-cn.com/registry-mirror

安装nexus3
下载nexus3镜像
docker pull sonatype/nexus3

查看镜像
docker images

创建容器并运行
docker run --name=nexus \
-p 7001:8081 \
-p 5000:5000 \
-v /datas/nexus3/nexus-data:/nexus-data \
--privileged=true \
-d sonatype/nexus3
说明
-d: 创建后台运行的容器
--name: 容器名称为nexus
-p: 宿主机端口7001指向容器8081, 5000端口后面会用到
-v: nexus的数据文件夹指向宿主机的/datas/nexus3/nexus-data
--privileged=true: 授予root权限(挂载多级目录必须为true,否则容器访问宿主机权限不足)
查看容器
docker ps

容器没有运行,用docker ps -a命令查看

说明容器已经创建了,但是没有成功启动。查看报错日志
docker logs nexus

可以看到权限不足的错误。可以在宿主机中执行如下命令解决。
chown -R 200 /datas
执行如下命令再次启动容器
docker start nexus

容器正在运行
如果希望容器开机自动运行,比如主机断电重启后就能得到运行。执行如下命令
docker update --restart=always nexus
访问nexus3
我的服务器地址是192.168.2.125

nexus登录
-
登录nexus
点击右上角的Sign in,弹出登录容器,用户admin,密码admin123。这是初始的,登录后可以更改。
图片.png
-
修改密码
登录后点击右上角用户名,进入账户修改界面。点击Change password
修改密码
这个界面输入的是当前密码。点击蓝色按钮后,进入输新密码的界面。
创建docker私服
创建Blob Store
创建docker自己的存储器,名称为“docker-blob”,nexus还能创建maven、npm等服务。每个服务的数据分开存储。


创建仓库
切换到Repositories菜单创建仓库


这里用到的就这三类仓库
-
创建代理仓库
创建了私服后,公司内部就可以配置连接到私服下载镜像。这里需要创建代理。点击docker(proxy)创建。
创建仓库信息
创建仓库信息
-
创建aliyun代理
创建aliyun代理
创建了两个代理
-
创建私服
选择docker(hosted)类型创建仓库
创建私服
-
创建仓库组
选择docker(group)类型创建
创建仓库组
关键是下面的选择组中包含的仓库,从上至下顺序加载。
选择包含
使用仓库
这个服务是公司内部使用的,总不能大家都使用admin用户操作。另外创建个用户。
-
创建角色
进入Roles菜单,需要创建一个角色,这个角色需要可以下载、上传的权限。
角色
权限
-
创建用户
创建
创建用户
开放到外网
把5000端口映射到外网,本地的某服务映射到外网有很多方式。这里用的是内网穿透。完成后外网地址是docker.blueheart.cn 。不要用这个地址试,文章结束后就改了。
修改项目
打开上一章的项目,修改pom.xml文件。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<docker.aliyun.registry>registry.cn-hangzhou.aliyuncs.com</docker.aliyun.registry>
<docker.image.prefix>biboheart</docker.image.prefix>
<dockerfile.maven.version>1.4.10</dockerfile.maven.version>
</properties>
改成
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<docker.registry>docker.blueheart.cn</docker.registry>
<docker.image.prefix>biboheart</docker.image.prefix>
<dockerfile.maven.version>1.4.10</dockerfile.maven.version>
</properties>
<configuration>
<repository>${docker.aliyun.registry}/${docker.image.prefix}/${project.artifactId}</repository>
<contextDirectory>./</contextDirectory>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
改成
<configuration>
<repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}</repository>
<contextDirectory>./</contextDirectory>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
完成后的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bhparent</artifactId>
<groupId>com.biboheart.demos</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bhhello</artifactId>
<name>bhhello</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<docker.registry>docker.blueheart.cn</docker.registry>
<docker.image.prefix>biboheart</docker.image.prefix>
<dockerfile.maven.version>1.4.10</dockerfile.maven.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--Docker maven plugin-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile.maven.version}</version>
<configuration>
<repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}</repository>
<contextDirectory>./</contextDirectory>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
打包及上传
执行命令打包项目编译项目
mvn clean package dockerfile:build

编译完成后,会生成一个image

执行命令上传镜像到私服
docker push docker.blueheart.cn/biboheart/bhhello:1.0.0-SNAPSHOT

我这里开发机用的是windows8.1的系统,docker配置在虚拟机中。前面我是这样使用SpringBoot(安装docker)中讲到了怎么连接到虚拟机。进入虚拟机后进行如下操作
编辑/var/lib/boot2docker/profile文件增加--insecure-registry docker.blueheart.cn
sudo vi /var/lib/boot2docker/profile
EXTRA_ARGS='
--label provider=virtualbox
--insecure-registry docker.blueheart.cn
'
CACERT=/var/lib/boot2docker/ca.pem
DOCKER_HOST='-H tcp://0.0.0.0:2376'
DOCKER_STORAGE=aufs
DOCKER_TLS=auto
SERVERKEY=/var/lib/boot2docker/server-key.pem
SERVERCERT=/var/lib/boot2docker/server.pem
操作完成后,重启docker。

再次尝试提交

表示没有操作权限,执行
docker login docker.blueheart.cn

再次尝试提交,可以看到上传中

具体上传时间根据网络环境的不同而不同
上传完成后可以到nexus3中查看到镜像列表

使用镜像
就在本机尝试使用,将打包时生成的镜像先删除掉。删除后镜像列表如下。

执行命令提取镜像,会从私服中提取镜像。
docker pull docker.blueheart.cn/biboheart/bhhello:1.0.0-SNAPSHOT


使用这个镜像创建容器并运行,执行命令docker run -idt -p 80:80 --name bhhello docker.blueheart.cn/biboheart/bhhello:1.0.0-SNAPSHOT
使用docker ps命令查看容器

容器已经正在运行。访问测试
