简体   繁体   中英

Port forwarding through nested docker containers on Jenkins

My Jenkins pipeline uses the docker plugin that then runs a docker container from inside of that to set up a general test environment like this:

   node('docker') {
    sh """
        cat > .Dockerfile.build <<EOF
        FROM ruby:$rubyVersion
        RUN apt-get update && apt-get install -y locales && localedef -i en_US -f UTF-8 en_US.UTF-8
        ENV LANG=en_US.UTF-8 \\
            LANGUAGE=en_US:en \\
            LC_LANG=en_US.UTF-8 \\
            LC_ALL=en_US.UTF-8

        RUN \\                  
          curl -sSL -o /tmp/docker.tgz https://download.docker.com/linux/static/stable/x86_64/docker-${dockerVersion}.tgz && \\
          tar --strip-components 1 --directory /usr/local/bin/ --extract --file /tmp/docker.tgz

        RUN \\
          groupadd -g $gid  docker && \\
          useradd -d $env.HOME -u $uid build -r -m && \\
          usermod -a -G docker build
        EOF

    """.stripIndent().trim()
    }

Once the test environment container is up, I run another container that has my code and tests inside that previously made environment container. One of my tests includes making sure a firewall was set up through iptables that allow certain ports through. To test to see if my firewall is setup correctly, I simple run this from inside that container (now 3 docker containers deep):

  def listener_response(port, host = 'localhost')
    TCPSocket.open(host, port) do |socket|
      socket.read(2)
    end
  rescue SystemCallError
    nil
  end

This is called by simply passing in the random port I used and the Jenkins docker node IP. When I run my test container, I do something like:

docker run -d -e DOCKER_HOST_IP=10.x.x.x -e RANDOM_OPEN_PORT=52459 -p 52459:52459 -v /var/run/docker.sock:/var/run/docker.sock

However, I still get a nil response from my test rather than an OK. Is there a way to port forward from the Jenkins host to my test environment to my test container?

Running the test environment with the option --network host seemed to solve the problem for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM