Closed moficodes closed 4 years ago
@grvcekim @mhrosen Here's a simple Python app that runs a web server on port 8000 and returns Hello, world
for all GET requests. Simple exercise could be turn this into a Docker container and run it locally, then in a Kubernetes cluster (cloud or kind
).
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Step 1:
Here's the Python 3 Hello World app - paste this into a file called hello.py
in a new (empty) directory.
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
tuple = self.client_address
self.wfile.write(b'Hello world to: ')
self.wfile.write(tuple[0].encode("utf-8"))
print ("Sent response")
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
This can be run with python3 hello.py
. Then go to localhost:8000
on your browser to verify it works.
Step 2: Dockerize
Stop the Python app if it's still running from Step 1 (Ctrl+C). Create a file called Dockerfile
in the same directory as hello.py
from python:3
WORKDIR /usr/src/app
COPY hello.py .
CMD [ "python", "./hello.py" ]
EXPOSE 8000
Build a docker image:
docker build -t pyhello .
Run the container this way:
docker run -p 8000:8000 pyhello
Verify you can access it on localhost:8000
in your browser - note the IP address - it should not be 127.0.0.1
at this point. All done!
For an exercise lets try to containerize an application.
It could be anything.
Ideally something you wrote. (webapps work best).