Curl: (7) Failed to connect to 127.0.0.1 port 3000: Connection refused

Here is the section in my Jenkinsfile:

stage(‘Start container’) {
steps {
sh ‘docker compose up -d --no-color --wait’
sh ‘docker compose ps’
}
}
stage(‘Run tests against the container’) {
steps {
sh ‘curl http://127.0.0.1:3000/param?query=demo | jq’
}
}

In the test stage above, I got the error below:

  • curl http://127.0.0.1:3000/param?query=demo

  • jq
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed

    0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
    0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
    curl: (7) Failed to connect to 127.0.0.1 port 3000: Connection refused.

Could anyone please help me resolve the issue? Thanks much.

Sounds like your service isn’t running or isn’t listening on that port.

My guess is its not finished starting up yet but it’s purely a guess. I recommend checking your app logs

Thanks much for your reply. The container is running as shown by ‘docker compose ps’. Below is more console output:

  • docker compose up -d --no-color --wait
    Container jenkins-docker-compose_master-echo-server-1 Recreate
    Container jenkins-docker-compose_master-echo-server-1 Recreated
    Container jenkins-docker-compose_master-echo-server-1 Starting
    Container jenkins-docker-compose_master-echo-server-1 Started
    [Pipeline] sh

  • docker compose ps
    NAME COMMAND SERVICE STATUS PORTS
    jenkins-docker-compose_master-echo-server-1 “node webserver” echo-server running 0.0.0.0:3000->80/tcp
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Run tests against the container)
    [Pipeline] sh

  • curl http://127.0.0.1:3000/param?query=demo

  • jq
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed

    0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
    0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
    curl: (7) Failed to connect to 127.0.0.1 port 3000: Connection refused

My statement still stands, either your service isn’t running (finished starting up, or has quit) or not listening on that port. Maybe add a post { failure { sh('docker-compose logs') } } or something

Is your service listing on port 80? or port 3000? cause thats mapping port 3000 outside docker to port 80 inside docker.

Thanks again for your reply.
Yes, the docker container is running on Jenkins. The original service is on port 80 which is mapped to 3000.
I added post { always { sh('docker-compose logs') } }

Here is the output:

  • docker compose logs
    jenkins-docker-compose_master-echo-server-1 | (node:1) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    jenkins-docker-compose_master-echo-server-1 | (Use node --trace-deprecation ... to show where the warning was created)
    jenkins-docker-compose_master-echo-server-1 | Listening on port 80.

I am just following a 9-minute tutorial here:

I am having exact same issue, and no solution on the horizon. did you manage to get this working

Docker compose up -d doesn’t wait for the process to start. You need to get your script to wait.

Also you need to make sure your ports are exposed

I recommend the wait for it nash script

Thanks for the reply, but I am pretty sure that is not the problem. I tried many approaches, even manually starting containers and trying to access after making sure they are up and running, but still no luck. Only thing that is different is that I am running Jenkins inside docker container (dind). I guess it has to do something with docker networking but still cant find my way through :frowning:

It’s not localhost:3000 (or whatever port you gave echo-server), but instead [port of your PC]:3000
e.g: instead of localhost:3000, put 172.25.16.1:3000
hope this helps!

1 Like