title: Best way to reset/revert a docker container to its original image state
body: <p>I need to be able to restart/revert a container to its original image state. Simply doing a <code>docker restart</code> will not work (e.g. files created during a session etc. are still persisted).</p>
  
  <p>Currently I have the following python script that does the work:</p>
  
  <pre><code>import subprocess
  # Stop and remove a named container if it exists (meaning is running or have exited).
  def resetContainer( imageName, containerName ):   
      containerExists=subprocess.check_output(['docker', 'ps','-aqf name=%s' % CONTAINER_NAME])
      if containerExists:
          print('Stop and remove container')
          subprocess.call(['docker', 'stop','%s' % CONTAINER_NAME])
          subprocess.call(['docker', 'rm','%s' % CONTAINER_NAME])
      return;
  
  resetContainer(IMAGE_NAME,CONTAINER_NAME)
  # Finally re-create it from image
  subprocess.call(['docker', 'run','-d','--name',CONTAINER_NAME,IMAGE_NAME,'tail', '-f','/dev/null'])
  </code></pre>
  
  <p>But is there a better way than this?</p>
  
  <p>I have looked at:
  <a href="https://docker-py.readthedocs.io/en/stable/containers.html" rel="noreferrer">https://docker-py.readthedocs.io/en/stable/containers.html</a></p>
  
  <p>but from what I can see I will end up with the same number of lines + the additional "overhead" of introducing an additional layer on top of docker "native" commands.</p>