server.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from flask import Flask
  2. from flask_restful import Resource, Api
  3. from docker import APIClient
  4. dockercli = APIClient(base_url='unix://var/run/docker.sock')
  5. app = Flask(__name__)
  6. api = Api(app)
  7. class Containers(Resource):
  8. def get(self):
  9. return dockercli.containers(all=True)
  10. class ContainerInfo(Resource):
  11. def get(self, container_id):
  12. return dockercli.containers(all=True, filters={"id": container_id})
  13. class ContainerStart(Resource):
  14. def post(self, container_id):
  15. try:
  16. dockercli.start(container_id);
  17. except:
  18. return 'Error'
  19. else:
  20. return 'OK'
  21. class ContainerStop(Resource):
  22. def post(self, container_id):
  23. try:
  24. dockercli.stop(container_id);
  25. except:
  26. return 'Error'
  27. else:
  28. return 'OK'
  29. api.add_resource(Containers, '/info/container/all')
  30. api.add_resource(ContainerInfo, '/info/container/<string:container_id>')
  31. api.add_resource(ContainerStop, '/stop/container/<string:container_id>')
  32. api.add_resource(ContainerStart, '/start/container/<string:container_id>')
  33. if __name__ == '__main__':
  34. app.run(debug=False, host='0.0.0.0', port='8080')