Ingen beskrivning

docker.ipynb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Docker with Python "
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "%run cleanup.py\n",
  17. "%run monitor.py"
  18. ]
  19. },
  20. {
  21. "cell_type": "code",
  22. "execution_count": null,
  23. "metadata": {
  24. "slideshow": {
  25. "slide_type": "slide"
  26. }
  27. },
  28. "outputs": [],
  29. "source": [
  30. "import docker\n",
  31. "client = docker.from_env()"
  32. ]
  33. },
  34. {
  35. "cell_type": "markdown",
  36. "metadata": {},
  37. "source": [
  38. "### Start a container"
  39. ]
  40. },
  41. {
  42. "cell_type": "code",
  43. "execution_count": null,
  44. "metadata": {},
  45. "outputs": [],
  46. "source": [
  47. "client.containers.run(image=\"ubuntu\", name=\"ubuntu\")"
  48. ]
  49. },
  50. {
  51. "cell_type": "markdown",
  52. "metadata": {},
  53. "source": [
  54. "### List all containers"
  55. ]
  56. },
  57. {
  58. "cell_type": "code",
  59. "execution_count": null,
  60. "metadata": {},
  61. "outputs": [],
  62. "source": [
  63. "for container in client.containers.list(all=True):\n",
  64. " print(container.name)"
  65. ]
  66. },
  67. {
  68. "cell_type": "markdown",
  69. "metadata": {},
  70. "source": [
  71. "### Get a container"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": null,
  77. "metadata": {},
  78. "outputs": [],
  79. "source": [
  80. "ubuntu = client.containers.get(\"ubuntu\")"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": null,
  86. "metadata": {},
  87. "outputs": [],
  88. "source": [
  89. "print(ubuntu.status)"
  90. ]
  91. },
  92. {
  93. "cell_type": "markdown",
  94. "metadata": {},
  95. "source": [
  96. "### Start a container"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": null,
  102. "metadata": {},
  103. "outputs": [],
  104. "source": [
  105. "ubuntu.start()"
  106. ]
  107. },
  108. {
  109. "cell_type": "code",
  110. "execution_count": null,
  111. "metadata": {},
  112. "outputs": [],
  113. "source": [
  114. "ubuntu.wait()"
  115. ]
  116. },
  117. {
  118. "cell_type": "code",
  119. "execution_count": null,
  120. "metadata": {},
  121. "outputs": [],
  122. "source": [
  123. "ubuntu.stop()"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": null,
  129. "metadata": {},
  130. "outputs": [],
  131. "source": [
  132. "ubuntu.remove()"
  133. ]
  134. },
  135. {
  136. "cell_type": "code",
  137. "execution_count": null,
  138. "metadata": {},
  139. "outputs": [],
  140. "source": [
  141. "for container in client.containers.list(all=True):\n",
  142. " print(container.name)"
  143. ]
  144. },
  145. {
  146. "cell_type": "code",
  147. "execution_count": null,
  148. "metadata": {},
  149. "outputs": [],
  150. "source": [
  151. "# %load monitor.py\n",
  152. "import threading\n",
  153. "import docker\n",
  154. "\n",
  155. "def Monitor():\n",
  156. " client = docker.from_env()\n",
  157. " for event in client.events(decode=True):\n",
  158. " eventType = event[\"Action\"]\n",
  159. " if eventType == 'create':\n",
  160. " print(\"Monitor: A container has been created\")\n",
  161. " if eventType == 'start':\n",
  162. " print(\"Monitor: A container has been started\")\n",
  163. " if eventType == 'die':\n",
  164. " print(\"Monitor: A container has been stopped\")\n",
  165. " if eventType == 'destroy':\n",
  166. " print(\"Monitor: A container has been deleted\")\n",
  167. " \n",
  168. " \n",
  169. "t = threading.Thread(target=Monitor)\n",
  170. "t.start()"
  171. ]
  172. },
  173. {
  174. "cell_type": "code",
  175. "execution_count": null,
  176. "metadata": {},
  177. "outputs": [],
  178. "source": [
  179. "monitor.t.stop()"
  180. ]
  181. },
  182. {
  183. "cell_type": "code",
  184. "execution_count": null,
  185. "metadata": {},
  186. "outputs": [],
  187. "source": []
  188. }
  189. ],
  190. "metadata": {
  191. "kernelspec": {
  192. "display_name": "Python 3",
  193. "language": "python",
  194. "name": "python3"
  195. },
  196. "language_info": {
  197. "codemirror_mode": {
  198. "name": "ipython",
  199. "version": 3
  200. },
  201. "file_extension": ".py",
  202. "mimetype": "text/x-python",
  203. "name": "python",
  204. "nbconvert_exporter": "python",
  205. "pygments_lexer": "ipython3",
  206. "version": "3.7.0"
  207. }
  208. },
  209. "nbformat": 4,
  210. "nbformat_minor": 2
  211. }