分类 Docker 下的文章

背景

公司部门内,配置了内网使用的域名。服务器部署完成后,nginx配置时,使用了该域名。请求后发现抛出了502 Bad Gateway

原因

查看nginx日志,发现了如下信息

2022/03/14 09:04:19 [error] 27#27: *41 no resolver defined to resolve ***.***.com, client: 10.10.60.155, server: ***.***.com, request: "POST /demo/captcha/anon/getLoginCaptcha HTTP/1.1", host: "***.***.com", referrer: "http://***.***.com/login/login"

很明显,是因为nginx无法解析该域名导致了。我们只需要让nginx能识别该域名即可。

解决方案

解决方法很简单,只需要在nginx全局配置(nginx.conf)的http项内,添加对应的dns解析即可resolver dns服务器地址;。如下所示:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    resolver 10.10.10.1; #配置dns地址
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

背景

项目采用Jenkins+Maven实现自动化部署springboot项目。本地运行正常,但是gitlab提交后,执行自动化部署过程中,出现了如下错误:

[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/xmlbeans/xmlbeans/2.6.0/xmlbeans-2.6.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for  
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:14 min
[INFO] Finished at: 2022-03-14T06:17:49Z
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project **-server: Could not resolve dependencies for project *****:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.apache.poi:poi-ooxml:jar:3.17, org.apache.xmlbeans:xmlbeans:jar:2.6.0: Could not transfer artifact org.apache.poi:poi-ooxml:jar:3.17 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar: Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out (Connection timed out) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

这里的描述很清楚,本地找不到poi-ooxml包,去中央仓库下载,然后国内的网络环境下,无法在https://repo.maven.apache.org下载该包。导致了构建失败。

过程及分析

一、确认Jenkins配置是否正确

首先我想到的是maven配置问题,因为我使用的是自己部署的nexus3私库,在Jenkins-管理管理-Managed files中添加了settings.xml文件并在项目中进行了配置使用。反复检查了settings.xml文件,确认没有问题。

<!-- 检查是否添加了私库镜像 -->
  <mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://私库Ip/nexus/repository/maven-public/</url>
    </mirror>
  </mirrors>

二、nexus配置

后来想到,可能是nexus中配置的代理仓库没有修改成国内镜像。查看后发现也没有问题。nexus中添加了阿里云镜像的代理仓库。

请输入图片描述

并把其放在了靠前的位置。
请输入图片描述

三、pom.xml配置

以上检查无误后,陷入了僵局,这个问题困扰了我一整天。期间我一度想直接在本地拷贝了这个包放到Jenkins的本地仓库中,然后问题依然存在,o(╯□╰)o。
直到后来,无意中查询到, 自定义pom.xml都是继承自super pom,所以maven项目下载一些jar包时,默认会从中央仓库下载 。知道了这点,剩下的就好办了,只要在pom.xml中进行配置,覆盖掉super pom的配置即可。

<!-- pom.xml文件中添加如下配置 -->
 <repositories>
        <repository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>

背景

因工作需要,在服务器通过docker部署了一个wiki.js服务,当初为了省事,通过docker-compose进行部署,同时把数据库连接信息也以环境变量的方式配置在了docker-compose.yml文件中了。

问题

但是最近公司重新部署了网络,数据库的服务ip地址已经发生了改变,导致wiki不能使用。
容器已经生成,修改compose文件已经没有任何意义,而这个服务已经用了很久,也不可能重新部署。这就需要一种可以动态修改容器环境变量的方式。

解决方案

好在经过查找,找到了,记录一下,以备后用。

// 查看容器id,通过以下命令,列出所有容器,记住需要修改的容器的id。
docker container ls -all

CONTAINER ID  IMAGE   COMMAND       CREATED   STATUS     PORTS   NAMES
c7266ebaf77d      requarks/wiki   "docker-entrypoint.s…"   3 months ago   Exited (137) 40 seconds ago  wiki

// 停止容器
docker stop wiki

// 停止Docker服务
service docker stop

// 修改 /var/lib/docker/containers/ID/config.v2.json 中对应的参数

// 启动Docker服务
service docker start

至此,问题解决。

说明

看标题很绕,那是因为我目前也不知道怎么处理,但是误打误撞找到了一个临时解决方案,为了防止以后复现此类问题无法解决,这里简单记录一下。

背景

springboot开发的项目、本地使用nexus搭建了maven库,使用gitlab做代码管理,同时配合jenkins进行自动部署。
整体流程就是提交代码到gitlab后,触发jenkins进行编译并部署到docker容器。开发过程中使用了一个第三方的jar包,手动上传到了nexus库中。

问题

清理了jenkins中的.m2缓存的包后,再次在jenkins中构建会提示如下错误:

Downloaded from maven-releases: http://我的nexusip/nexus/repository/maven-releases/e-iceblue/spire.presentation.free/3.9.0/spire.presentation.free-3.9.0.jar (37 MB at 64 MB/s)
[INFO] 
...
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project data-maintain: Could not resolve dependencies for project 我的项目:jar:0.0.1-SNAPSHOT: Could not find artifact e-iceblue:spire.presentation.free:jar:3.9.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
[ERROR] 

这里的spire.presentation.free-3.9.0.jar便是我使用的第三方jar包,很明显前面已经从我的nexus库中下载了,但是后面又去repo.maven.apache.org中去查找没找的报错了。

解决方案

这个问题困扰了我好久,至今也没找到方案,不过阴差阳错间,找到了个临时方法。过程如下:

  1. pom文件中原本是这样的
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并自动部署。会报上述错误。
  2. 将pom修改为:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并触发自动部署。仍会报错。
  2. 修改pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 删除了这些配置-->
    <modelVersion>4.0.0</modelVersion>
  1. 提交,并jenkins进行构建。
  2. 竟然好了。

总结

先这样吧,后面找到了具体原因,在更新。

背景

开发了springboot项目,部署到了docker上。运行后发现时间总是少8小时。其实可以确定就是时区的问题,苦于不是很熟悉这块内容,来来回回折腾了好几个小时才搞定。

处理过程

修改docker-compose文件

网上搜了下,发现很多这类问题的帖子,而且处理很简单,拷贝了一个,挂载localtime。

-v /etc/localtime:/etc/localtime

并且修改了宿主机的localtime,然而并没有解决,进入容器查看时间发现是对的,但是程序日志和存入数据库的时间依然不正确。

数据库时区设置

后来想着是不是数据库的时区问题,修改了数据库的时区

mysql> set global time_zone = '+8:00';
mysql> set time_zone = '+8:00';
mysql> flush privileges;

然而,还是没有解决。

timezone修改

后来发现是少修改了一个timezone。

解决方案

Dockerfile中添加了如下内容,将timezone设置为+8,问题解决。

RUN echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata