0

i'm trying to access a exposed port from a container to another container inside the same node, but i can't figure out how to do this using kubernetes plugin in pipeline.

In my script I have created both containers and exposed the port of a database container, in the other container i'm trying to access the port 1521 of a host.

def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
    containerTemplate(name: 'oracle', image: 'repo:5000/ng-oracle:latest',privileged: true, ttyEnabled: true, command: 'cat', ports:[
        portMapping(name: 'oracle1', containerPort: 1521, hostPort: 1521),
        portMapping(name: 'oracle2', containerPort: 22, hostPort: 2222),
    ]),
    containerTemplate(name: 'maven', image: 'repo:5000/ng-satelites:4', ttyEnabled: true, command: 'cat')
  ]) {
        node(label) {
            stage('all') {
                container('maven') {
                    stage('test-db') {
                        sh 'curl $(/sbin/ip route|awk \'/default/ { print $3 }\'):1521'
                    }
                }
            }
        }
}
Eric Freitas
  • 90
  • 11
  • did you find a solution for this? ran into the same problem myself, both containers within a Pod should be accessible via their name, I have mongo and maven but my build fails due to timeout connecting to `mongo` container from maven container. – Anadi Misra Jan 30 '22 at 12:08

1 Answers1

2

Inside a Kubernetes POD, all containers 'see each other' through localhost. Therefore, you should be able to connect to your oracle container from your maven container on localhost:1521.

gmc
  • 3,910
  • 2
  • 31
  • 44
  • Thanks for your answer @gmc, but i have tried with localhost and it don't work! "+ curl localhost:1521 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (7) Failed to connect to localhost port 1521: Connection refused" Is there any other configuration that i have to do to make this working? – Eric Freitas Mar 27 '19 at 18:15
  • 2
    Are you sure the service is started and listening inside the container? Bare in mind that when you specify the option `command: cat` you are overriding the container's default entrypoint. – gmc Mar 27 '19 at 18:36