Sidecar containers get stuck in loop waiting to start

I’m attempting to use sidecar containers following the examples detailed here, the first example gets stuck in a loop unable to find ‘mysqladmin’.

+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1
+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1
+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1
+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1
+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1
+ mysqladmin ping -h0.0.0.0 --silent
/Users/clint/.jenkins/workspace/aaa@tmp/durable-66a0a38e/script.sh: line 1: mysqladmin: command not found
+ sleep 1

The second example utilizing two containers simultaneously works as expected though. But when I try to do a similar style using PostgreSQL instead:

node {
    checkout scm
    docker.image('postgres:12').withRun('-p 5432:5432 -e POSTGRES_DB=mydb -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypass') { c ->
        docker.image('postgres:12').inside("--link ${c.id}:db") {
            sh 'while ! pg_isready; do sleep 1; done'
        }
        docker.image('centos:7').inside("--link ${c.id}:db") {
            sh 'make check'
        }
    }
}

It also gets stuck waiting:

+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt
+ sleep 1
+ pg_isready
/var/run/postgresql:5432 - no attempt

I ran the two mysql demos, and your postgres demo

sidecar-mysql-simple Config [Jenkins] ← guessing the one you tried to run?

I’ve honestly never seen withRun before, and according to this stack overflow page withRun tries to run the sh statements outside of the container.

@MarkEWaite (we should get a docs group) it probably could be clearer that the first snippet is concept and doesn’t actually work?

sidecar-mysql-inner Config [Jenkins] ← works just fine, was the second example in the docs

As for your postgres thingie - postgres-sidecar Config [Jenkins] - I did mange to eventually get it working. I think its your env variables. the “no attempt” means essentially it failed to connect. If you look at PostgreSQL: Documentation: 9.3: Environment Variables the variables used by the client are very different.

PGPASSWORD=mypass PGHOST=db PGDATABASE=mydb PGUSER=myuser pg_isready
db:5432 - accepting connections

runs fine

Btw, you can wrap the entire thing in withEnv() block (use the syntax generator to generate you one) and jenkins will automatically share all the env variables, so you don’t have to mange them on each statement/block

Thank you @halkeye !

Correct, sidecar-mysql-simple was the example that wouldn’t work for me, and sidecar-mysql-inner did work.

Ahh… I didn’t quite understand the difference with .withRun and .inside and the env variables. So .withRun uses the docker image env variables and then .inside is the PostreSQL ones. Thanks for the working example (and tip about withEnv() block)!

I think inside() means everything(any pipeline step-ish) that is inside of the block runs inside of the container
withRun is for background tasks, like setting up servers, and gives you a reference variable to that container.

its a lot better than what i’ve used before which is manual docker run, then a try/finally then cleanup by hand.