Create a Simple Docker Container with a Python Web Server

Chris Bensen
Oracle Developers
Published in
2 min readAug 3, 2022

By Chris Bensen

Photo by Frans van Heerden

If you prefer you can read this blog post on GitHub here.

This article may seem obvious to some, but others need to know how to get started. Having a server running in a container is the beginning of so many great things.

Prerequisites

  1. Install Docker.
  2. Read Create a Simple Python Web Server because we will use this Web Server but put it into a Docker container.

Build a Web Server

  1. Create a folder and put all the files we are going to create into that folder.
  2. Create index.html:

    <!DOCTYPE html>
    <html>
    <body>
    Hello World
    </body>
    </html>
  3. Create server.py:


#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import json
from socketserver import ThreadingMixIn
import threading
hostName = “0.0.0.0”
serverPort = 80
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
# curl http://<ServerIP>/index.html
if self.path == “/”:
# Respond with the file contents.
self.send_response(200)
self.send_header(“Content-type”, “text/html”)
self.end_headers()
content = open(‘index.html’, ‘rb’).read()
self.wfile.write(content)
else:
self.send_response(404)
returnclass ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
“””Handle requests in a separate thread.”””
if __name__ == “__main__”:
webServer = ThreadedHTTPServer((hostName, serverPort), Handler)
print(“Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print(“Server stopped.”)
```

Put Everything into a Docker Container

  1. Create Dockerfile:

    FROM python:3
    ADD index.html index.html
    ADD server.py server.py
    EXPOSE 8000
    ENTRYPOINT [“python3”, “server.py”]
  2. cd into the folder you created.
  3. Run docker build -f Dockerfile . -t web-server-test
  4. Run docker run -rm -p 8000:80 — name web-server-test web-server-test

There you have it! Enjoy your web server in a Docker container. The nice thing about doing this is as a developer you can run and debug things inside a container just as if they are on your local computer but the only thing that is installed on your local system is Docker.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Oracle Developers
Oracle Developers

Published in Oracle Developers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, Oracle ACEs, and Java Champions on all things Oracle technology. The views expressed are those of the authors and not necessarily of Oracle.

Chris Bensen
Chris Bensen

Written by Chris Bensen

I make stuff. Creator of the Worlds Largest Raspberry Pi Cluster and Lego Computer.

Responses (3)

Write a response