0

I am encountering a connection refused error while running a Jenkins pipeline with Docker. The pipeline is responsible for setting up a PostgreSQL container, performing a database migration, and then stopping and removing the container. However, when the migration step is executed, I receive the following error.

pipeline {
    agent any

    stages {
        stage('Setup Docker') {
            steps {
                // Set up Docker environment
                sh 'docker run --name postgres12 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=admin -d postgres:12-alpine'
                sh 'sleep 10'
            }
        }

        stage('Run db migration') {
            steps {
                script {
                    // Run migration with the updated hostname
                    sh 'migrate -path db/migration -verbose -database "postgres://root:admin@localhost:5432/bank?sslmode=disable" up'
                }
            }
        }
    }

    post {
        always {
            // Stop and remove the container after the pipeline finishes
            sh 'docker stop postgres12'
            sh 'docker rm postgres12'
        }
    }
}

I have ensured that the PostgreSQL container is running before attempting the migration step, but it seems that the connection to the database is being refused. I suspect that there might be a timing issue or a problem with the hostname/address I'm using in the migration command.

0 Answers0