Accessing Service Container

I have a pipeline that has a postgres service. I run integration tests with it. So basically my job start the postgres and it runs the tests that connects to this database and runs the tests.

What I wanted to do is when this tests fails, I wanted to generate a dump from this database as an artifact. I tried using docker exec but from what I read the services are ran side by side with the job runner and not inside it, so it won’t see it.

Is there any way I can generate a dump from the service database if the job fails?