Bläddra i källkod

Make better message

Jimmy Allen 3 månader sedan
förälder
incheckning
461f20a3bc
7 ändrade filer med 106 tillägg och 48 borttagningar
  1. 1 0
      .gitignore
  2. 0 2
      Dockerfile
  3. 18 1
      README.md
  4. 9 0
      docker-compose.yml
  5. 59 43
      docker.ipynb
  6. 0 2
      hello.py
  7. 19 0
      monitor.py

+ 1 - 0
.gitignore

@@ -63,3 +63,4 @@ target/
63 63
 .ipython/
64 64
 .jupyter/
65 65
 .local/
66
+.mypy_cache

+ 0 - 2
Dockerfile

@@ -11,8 +11,6 @@ COPY entry.sh /entry.sh
11 11
 RUN useradd -d /data jupyter \
12 12
     && chown -R jupyter:jupyter /data
13 13
 
14
-
15
-
16 14
 EXPOSE 8888
17 15
 
18 16
 VOLUME /data

+ 18 - 1
README.md

@@ -1,2 +1,19 @@
1
-# Docker-Python-Demo
1
+# An example of how to use the Docker Python API
2 2
 
3
+You will need Docker 
4
+
5
+`curl -fsSL https://get.docker.com -o get-docker.sh`
6
+
7
+`sudo sh get-docker.sh`
8
+
9
+And Docker Compose
10
+
11
+`sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose`
12
+
13
+`sudo chmod +x /usr/local/bin/docker-compose`
14
+
15
+Then run
16
+
17
+`docker-compose up`
18
+
19
+The in your browser go to http://localhost:8888/notebooks/docker.ipynb

+ 9 - 0
docker-compose.yml

@@ -0,0 +1,9 @@
1
+version: "3"
2
+services:
3
+  jupyter:
4
+    build: .
5
+    ports: 
6
+      - "8888:8888"
7
+    volumes: 
8
+      - ./:/data  
9
+      - /var/run/docker.sock:/var/run/docker.sock  

+ 59 - 43
docker.ipynb

@@ -1,8 +1,15 @@
1 1
 {
2 2
  "cells": [
3 3
   {
4
+   "cell_type": "markdown",
5
+   "metadata": {},
6
+   "source": [
7
+    "# Docker with Python "
8
+   ]
9
+  },
10
+  {
4 11
    "cell_type": "code",
5
-   "execution_count": 3,
12
+   "execution_count": null,
6 13
    "metadata": {
7 14
     "slideshow": {
8 15
      "slide_type": "slide"
@@ -15,7 +22,7 @@
15 22
   },
16 23
   {
17 24
    "cell_type": "code",
18
-   "execution_count": 4,
25
+   "execution_count": null,
19 26
    "metadata": {
20 27
     "slideshow": {
21 28
      "slide_type": "slide"
@@ -29,12 +36,29 @@
29 36
   {
30 37
    "cell_type": "code",
31 38
    "execution_count": null,
39
+   "metadata": {},
40
+   "outputs": [],
41
+   "source": [
42
+    "%run monitor.py"
43
+   ]
44
+  },
45
+  {
46
+   "cell_type": "markdown",
32 47
    "metadata": {
33 48
     "scrolled": true
34 49
    },
50
+   "source": [
51
+    "client.containers.run(image=\"ubuntu\", name=\"ubuntu\")"
52
+   ]
53
+  },
54
+  {
55
+   "cell_type": "code",
56
+   "execution_count": null,
57
+   "metadata": {},
35 58
    "outputs": [],
36 59
    "source": [
37
-    "print(client.containers.run(image=\"ubuntu\", name=\"ubuntu\"))"
60
+    "for container in client.containers.list(all=True):\n",
61
+    "    print(container.name)"
38 62
    ]
39 63
   },
40 64
   {
@@ -43,7 +67,7 @@
43 67
    "metadata": {},
44 68
    "outputs": [],
45 69
    "source": [
46
-    "client.containers.remove(\"ubuntu\")"
70
+    "ubuntu = client.containers.get(\"ubuntu\")"
47 71
    ]
48 72
   },
49 73
   {
@@ -52,19 +76,7 @@
52 76
    "metadata": {},
53 77
    "outputs": [],
54 78
    "source": [
55
-    "import threading\n",
56
-    "\n",
57
-    "def Monitor():\n",
58
-    "    for event in client.events(decode=True):\n",
59
-    "        eventType = event[\"Action\"]\n",
60
-    "        if eventType == 'start':\n",
61
-    "            print(\"Monitor: A container has been created\")\n",
62
-    "        if eventType == 'die':\n",
63
-    "            print(\"Monitor: A container has been destroyed\")\n",
64
-    "            \n",
65
-    "import threading\n",
66
-    "t = threading.Thread(target=Monitor)\n",
67
-    "t.start()"
79
+    "print(ubuntu.status)"
68 80
    ]
69 81
   },
70 82
   {
@@ -73,43 +85,26 @@
73 85
    "metadata": {},
74 86
    "outputs": [],
75 87
    "source": [
76
-    "client.containers.run(\"ubuntu\", \"echo hello world\", environment=[\"a=b\"])"
88
+    "ubuntu.start()"
77 89
    ]
78 90
   },
79 91
   {
80 92
    "cell_type": "code",
81
-   "execution_count": 1,
93
+   "execution_count": null,
82 94
    "metadata": {},
83
-   "outputs": [
84
-    {
85
-     "name": "stdout",
86
-     "output_type": "stream",
87
-     "text": [
88
-      "b\n"
89
-     ]
90
-    }
91
-   ],
95
+   "outputs": [],
92 96
    "source": [
93
-    "%run hello.py\n",
94
-    "import hello\n",
95
-    "hello.h(\"b\")"
97
+    "ubuntu.remove()"
96 98
    ]
97 99
   },
98 100
   {
99 101
    "cell_type": "code",
100
-   "execution_count": 5,
102
+   "execution_count": null,
101 103
    "metadata": {},
102
-   "outputs": [
103
-    {
104
-     "name": "stdout",
105
-     "output_type": "stream",
106
-     "text": [
107
-      "<docker.client.DockerClient object at 0x7f5e504bc8d0>\n"
108
-     ]
109
-    }
110
-   ],
104
+   "outputs": [],
111 105
    "source": [
112
-    "print(client)"
106
+    "for container in client.containers.list(all=True):\n",
107
+    "    print(container.name)"
113 108
    ]
114 109
   },
115 110
   {
@@ -117,7 +112,28 @@
117 112
    "execution_count": null,
118 113
    "metadata": {},
119 114
    "outputs": [],
120
-   "source": []
115
+   "source": [
116
+    "# %load monitor.py\n",
117
+    "import threading\n",
118
+    "import docker\n",
119
+    "\n",
120
+    "def Monitor():\n",
121
+    "    client = docker.from_env()\n",
122
+    "    for event in client.events(decode=True):\n",
123
+    "        eventType = event[\"Action\"]\n",
124
+    "        if eventType == 'create':\n",
125
+    "            print(\"Monitor: A container has been created\")\n",
126
+    "        if eventType == 'start':\n",
127
+    "            print(\"Monitor: A container has been started\")\n",
128
+    "        if eventType == 'die':\n",
129
+    "            print(\"Monitor: A container has been stopped\")\n",
130
+    "        if eventType == 'destroy':\n",
131
+    "            print(\"Monitor: A container has been deleted\")\n",
132
+    "        \n",
133
+    "            \n",
134
+    "t = threading.Thread(target=Monitor)\n",
135
+    "t.start()"
136
+   ]
121 137
   },
122 138
   {
123 139
    "cell_type": "code",

+ 0 - 2
hello.py

@@ -1,2 +0,0 @@
1
-def h(h):
2
-    print(h)

+ 19 - 0
monitor.py

@@ -0,0 +1,19 @@
1
+import threading
2
+import docker
3
+
4
+def Monitor():
5
+    client = docker.from_env()
6
+    for event in client.events(decode=True):
7
+        eventType = event["Action"]
8
+        if eventType == 'create':
9
+            print("Monitor: A container has been created")
10
+        if eventType == 'start':
11
+            print("Monitor: A container has been started")
12
+        if eventType == 'die':
13
+            print("Monitor: A container has been stopped")
14
+        if eventType == 'destroy':
15
+            print("Monitor: A container has been deleted")
16
+        
17
+            
18
+t = threading.Thread(target=Monitor)
19
+t.start()