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: