Jenkins cannot start a docker agent container in declarative pipeline with specific run arguments

  agent, docker, jenkins-pipeline

I have the next setup:

  • one amazon EC2 instance;
  • Jenkins installed on it;
  • Docker instance installed along with Jenkins;
  • Docker instance configured as cloud on Jenkins;
  • docker agent template is also configured and can be used when the pipeline calls it by label on the agent section.

What I want to accomplish is to use a docker container as the main agent, in a Jenkins pipeline. Now this agent needs to run some build steps and remain running until some other build plan stops the running containers and cleans things up.
This is the pipeline script:

pipeline {
agent {
    docker {
        image 'localhost:5000/build_deploy_agent:base'
        label 'build-deploy-agent'
        args  '-t --network bridge -p 9102:9102'
stages {
    stage('Example Build') {
        steps {
            sh 'ps aux'

What happens is:
Jenkins starts the container but it cannot be used to run the script from the stage.
I receive three errors like this one:

docker inspect -f . localhost:5000/build_deploy_agent:base
/home/jenkins/workspace/view name/Build plan [email protected]/durable-9a8a9027/ 1: 
/home/jenkins/workspace/view name/Build plan [email protected]/durable-9a8a9027/ docker: not found

I am definitely doing something wrong here, but I am clueless.

Source: Docker Questions