Jenkins docker 容器无法将 war 文件部署到 tomcat docker 容器

Jenkins docker container cannot deploy war file to tomcat docker container

我无法让 Jenkins 在 Tomcat8 服务器上部署 war 文件。为什么 Jenkins 不能部署到 Tomcat?

当我 运行 Jenkins 工作时,我遇到了这个异常:

[DeployPublisher][INFO] Deploying /var/jenkins_home/workspace/Deploy_to_Tomcat_server/webapp/target/webapp.war to container Tomcat 8.x Remote with context null
ERROR: Build step failed with exception
java.net.ConnectException: Connection refused (Connection refused)

我认为docker两个容器都有问题,所以我将描述我所做的事情。

Jenkins 服务器和 Tomcat8 都在 运行 我本地机器上 docker 容器中。为了让双方都能看到对方,我创建了一个公共网络。

~ % docker network ls
NETWORK ID     NAME                            DRIVER    SCOPE
da6fc157710c   bridge                          bridge    local
...
// network bridge already exists!
~ % docker network create --driver bridge  my_jenkins_tomcat_network
378ef3f01e215207e90ca0a6e93e89a9610be1e9bd972f94f02f9b1ce6199923
**// Run jenkins container**
~ % docker run -d -p 8080:8080 --name jenkins_container_test --network my_jenkins_tomcat_network jenkinsci/blueocean
08a2ce5e609f0c50e3a4c9ce73a5c88918e6a0ab69c582d75bc44162ae7e58fd
**// Run tomcat container. I had an image name mywebapp with Tomcat8...**
~ % docker run -d -p 80:8080 --name tomcat_container_test --network my_jenkins_tomcat_network mywebapp
5ac868dbeb69512c7c2d5b62f067de72592a01e763cf5b20808d22c06de1fe0e
~ % docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS              PORTS                               NAMES
5ac868dbeb69   mywebapp              "catalina.sh run"        9 seconds ago        Up 8 seconds        0.0.0.0:80->8080/tcp                tomcat_container_test
08a2ce5e609f   jenkinsci/blueocean   "/sbin/tini -- /usr/…"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp, 50000/tcp   jenkins_container_test

我可以同时检查容器和新网络:

~ % docker network ls
NETWORK ID     NAME                            DRIVER    SCOPE
da6fc157710c   bridge                          bridge    local
378ef3f01e21   my_jenkins_tomcat_network       bridge    local

~ % docker inspect my_jenkins_tomcat_network
[
    {
        "Name": "my_jenkins_tomcat_network",
        "Id": "378ef3f01e215207e90ca0a6e93e89a9610be1e9bd972f94f02f9b1ce6199923",
        "Created": "2021-04-12T08:07:52.770548349Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.23.0.0/16",
                    "Gateway": "172.23.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "08a2ce5e609f0c50e3a4c9ce73a5c88918e6a0ab69c582d75bc44162ae7e58fd": {
                "Name": "**jenkins_container_test**",
                "EndpointID": "80adf0fe02288d76f24e675ad0fdf25bf89ac64ac135dee03cdd4b91a74a6d3e",
                "MacAddress": "02:42:ac:17:00:02",
                "IPv4Address": "**172.23.0.2/16**",
                "IPv6Address": ""
            },
            "5ac868dbeb69512c7c2d5b62f067de72592a01e763cf5b20808d22c06de1fe0e": {
                "Name": "**tomcat_container_test**",
                "EndpointID": "ca216dc9302db6eee66393d9210aab4e4236c7442dba5c3701bcebc11b2e9463",
                "MacAddress": "02:42:ac:17:00:03",
                "IPv4Address": "**172.23.0.3/16**",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

我可以在 Jenkins 容器中执行 bash 并 ping tomcat 容器:

~ % docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS                               NAMES
5ac868dbeb69   mywebapp              "catalina.sh run"        About an hour ago   Up About an hour   0.0.0.0:80->8080/tcp                tomcat_container_test
08a2ce5e609f   jenkinsci/blueocean   "/sbin/tini -- /usr/…"   About an hour ago   Up About an hour   0.0.0.0:8080->8080/tcp, 50000/tcp   jenkins_container_test
~ % docker exec -it -u:root 08a2ce5e609f bashh
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: "bashh": executable file not found in $PATH: unknown
aironman@MacBook-Pro-de-Alonso ~ % docker exec -it -u:root 08a2ce5e609f bash 
bash-5.0# ping 172.23.0.3
PING 172.23.0.3 (172.23.0.3): 56 data bytes
64 bytes from 172.23.0.3: seq=0 ttl=64 time=0.163 ms
64 bytes from 172.23.0.3: seq=1 ttl=64 time=0.139 ms
...

在我的 tomcat 容器中,我修改了 tomcat-users.xml 文件,默认内容为:

    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
    <user username="tomcat" password="tomcat" roles="manager-gui"/>
    <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
    **<user username="deployer" password="deployer" roles="manager-script"/>** 

当我创建 Jenkins 作业时,我使用凭证部署器和 tomcat url,如上所示

我也尝试过使用内部 ip,172.23.0.3,没有运气。

我看过thislink,没有回复,有点不一样,所以我觉得回答这个问题是合理的。

实现此目标的一种方法是安装此 plugin,在 tomcat 容器中配置 sshd 并在 Jenkins 中创建一个 post 任务以复制 war 文件到 webapps 文件夹。