title: using jenkins docker plugin for storage persistant containers in a build pipeline
body: <p>This is the groovy script for a simple build pipeline that uses the docker image of SQL Server on Linux:</p>
  
  <pre><code>def PowerShell(psCmd) {
      bat "powershell.exe -NonInteractive -ExecutionPolicy Bypass -Command \"\$ErrorActionPreference='Stop';$psCmd;EXIT \$global:LastExitCode\""
  }
  node {
      stage('git checkout') {
          git 'file:///C:/Projects/SsdtDevOpsDemo'
      }
  
      stage('build dacpac') {
          bat "\"${tool name: 'Default', type: 'msbuild'}\" /p:Configuration=Release"
          stash includes: 'SsdtDevOpsDemo\\bin\\Release\\SsdtDevOpsDemo.dacpac', name: 'theDacpac'
      }
  
      stage('start container') {
          sh 'docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssword1" --name SQLLinuxLocal2 -d -i -p 15566:1433 microsoft/mssql-server-linux'
      }
  
      stage('deploy dacpac') {
          unstash 'theDacpac'
          bat "\"C:\\Program Files\\Microsoft SQL Server\\140\\DAC\\bin\\sqlpackage.exe\" /Action:Publish /SourceFile:\"SsdtDevOpsDemo\\bin\\Release\\SsdtDevOpsDemo.dacpac\" /TargetConnectionString:\"server=localhost,15566;database=SsdtDevOpsDemo;user id=sa;password=P@ssword1\""
      }
  
      stage('run tests') {
          PowerShell('Start-Sleep -s 5')
      }
  
      stage('cleanup') {
          sh 'docker stop SQLLinuxLocal2'
          sh 'docker rm SQLLinuxLocal2'
      }
  }  
  </code></pre>
  
  <p>I got to this point with some help with a question I posted a day or so ago, this was my attempt (with some help) at doing the same thing but with the docker plugin:</p>
  
  <pre><code>def PowerShell(psCmd) {
      bat "powershell.exe -NonInteractive -ExecutionPolicy Bypass -Command \"\$ErrorActionPreference='Stop';$psCmd;EXIT \$global:LastExitCode\""
  }
  node {
      stage('git checkout') {
          git 'file:///C:/Projects/SsdtDevOpsDemo'
      }
  
      stage('Build Dacpac from SQLProj') {
          bat "\"${tool name: 'Default', type: 'msbuild'}\" /p:Configuration=Release"
          stash includes: 'SsdtDevOpsDemo\\bin\\Release\\SsdtDevOpsDemo.dacpac', name: 'theDacpac'
      }
  
      stage('start container') {
          docker.image('-e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssword1" --name SQLLinuxLocal2 -d -i -p 15566:1433 microsoft/mssql-server-linux').withRun() {
              unstash 'theDacpac'
              bat "\"C:\\Program Files\\Microsoft SQL Server\\140\\DAC\\bin\\sqlpackage.exe\" /Action:Publish /SourceFile:\"SsdtDevOpsDemo\\bin\\Release\\SsdtDevOpsDemo.dacpac\" /TargetConnectionString:\"server=localhost,15566;database=SsdtDevOpsDemo;user id=sa;password=P@ssword1\""
          }
  
          sh 'docker run -d --name SQLLinuxLocal2 microsoft/mssql-server-linux'
      }
  
      stage('sleep') {
          PowerShell('Start-Sleep -s 30')
      }
  
      stage('cleanup') {
          sh 'docker stop SQLLinuxLocal2'
          sh 'docker rm SQLLinuxLocal2'
      }
  }    
  </code></pre>
  
  <p>The problem with this is that although it works, the docker run -d line spins up a different incarnation of the carnation. Could someone please point me in the correct direction as to getting the same result as per the first pipeline but by using the docker plugin.</p>