Connecting docker containers in the same network in Jenkins

Here is my Jenkins pipeline that runs mysql container and app container in the same network test:

stage ('Test') {
            steps {
                sh "docker network create test"
                script {
                    docker.image("mysql:5.7").withRun("-p 3306:3306 --name=db -e MYSQL_DATABASE=my-secret-pw -e MYSQL_DATABASE=database -e MYSQL_USER=user -e MYSQL_PASSWORD=devpass --entrypoint "/bin/bash" -itd --network=test") {
                        sh 'docker network inspect test'
                        docker.image("$CONTAINER_NAME:back").inside("-itd --network=test") {
                            sh 'telnet db 3306'
                        }
                    }
                }
            }
        }

Here is the output

+ docker network inspect test
[
    {
        "Name": "test",
        "Id": "34549b251607857a7cd5fef30e631b06c7a8e256e8cdb580275c5e769654bccf",
        "Created": "2020-03-15T21:24:27.942976689Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3c95827efa7d5cc86de5f20ac3a11f3a3e72800ab573aaca6e77dcd6a4e9c070": {
                "Name": "dvmeal_back",
                "EndpointID": "9f8c29a376e3470ea00bae235190079b3ae5185e37d46bab92d7fd7741a435aa",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "4ad297b527c348cd5f813a2d243862b9290b9de1e815ce14950437e492f6a433": {
                "Name": "db",
                "EndpointID": "b87fe276a80582f2e41d3ef2ac0651b01ec8a48b14123b9151235efac9267e96",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . vacation_bot:back
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 0a2e622c2f0359bc5ba61582f4f7a9f1e627a34fe2e01de31e458317bcc25dd6
$ docker run -t -d -u 1000:1000 -itd --network=test -w /var/jenkins_home/workspace/vacation_bot --volumes-from 0a2e622c2f0359bc5ba61582f4f7a9f1e627a34fe2e01de31e458317bcc25dd6 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** vacation_bot:back cat
$ docker top 653714067d13a2cc1535404109ce107832e2c139bfaeb387e6f180e81d1c4a3f -eo pid,comm
[Pipeline] {
[Pipeline] sh
+ telnet db 3306
telnet: Unable to connect to remote host: Connection refused
Trying 172.18.0.2...

So the db container is in the test network and telnet tries to resolve the ip but fails. What is the reason for such behaviour?

Source: StackOverflow