Search code examples
dockerjenkinscontainersagent

Is there a way to assign network and binding ports to a docker agent in Jenkins?


I have the next setup:

  • one amazon EC2 instance;
  • Jenkins installed on it;
  • Docker instance installed along with Jenkins

When I am starting the container (in a pipeline script) I am trying to add also the host network and also binding ports.

node {
    docker.image('localhost:5000/build_deploy_agent:base').withRun('-t --network host -p 9101:9101') { c ->
        sh 'doing stuff'
    }
}

I need this because at the end I want to keep the container alive with the deployed application.

The problem is, no network or ports are assigned to the container. Only the -t command is taken in consideration.

enter image description here

Is there a way to do this or I am losing time?


Solution

  • Check first if the network is visible through inspection, instead of just docker ps.
    See "How to find the network my container is in?"

    docker inspect <lyContainer> -f "{{json .NetworkSettings.Networks }}"
    

    Or you could filter docker ps by network, again, to check if the containers were assigned to the expected network.

    You can also check your network stack through a Jenkins step like ip addr show to see if a new network interface was created.