Skip to content

Latest commit

 

History

History

python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

A Simple Hello Docker Python Demo

Example used to demonstrate docker init CLI for a simple Hello World Python Program

Run the application

You can simply use python3 app.py command.

This code defines a handler that responds to GET requests with the specified text and starts an HTTP server listening on port 8080. When you run the script, you can access the server at http://localhost:8080 and see the same message as the Python program.

Those commands will start a http server listening on port 8080 and if your request http://localhost:8080 you'll see the following output:

❯ curl http://localhost:8080

          ##         .
    ## ## ##        ==
 ## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
{                       /  ===-
\______ O           __/
 \    \         __/
  \____\_______/


Hello from Docker!

Using Docker init

Run the following command:

 docker init

This utility will walk you through creating the following files with sensible defaults for your project:

  • .dockerignore
  • Dockerfile
  • docker-compose.yaml

Modify the Dockerfile

FROM python:3.8-alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
CMD ["python3", "app.py"]

Modify the Docker Compose file

 version: '3'

services:
  app:
    build: .
    ports:
      - "8080:8080"
    command: python3 app.py

Running the container service

 docker compose up -d --build

Accessing the Python app

curl localhost:8080

         ##         .
   ## ## ##        ==
## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
{                       /  ===-
\______ O           __/
\    \         __/
 \____\_______/


Hello from Docker!

FAQs

My requirements.txt shows gunicorn. What's that?

Gunicorn (Green Unicorn) is a popular Python Web Server Gateway Interface (WSGI) HTTP server. It is designed to be a lightweight and reliable server for running Python web applications. Gunicorn is commonly used in production environments to serve Python web applications, especially when combined with popular web frameworks like Django or Flask.

Here are some key features and characteristics of Gunicorn:

  • WSGI Server: Gunicorn is a WSGI HTTP server, which means it adheres to the Web Server Gateway Interface specification. This allows it to interface with Python web applications that also follow the WSGI standard.

  • Pre-fork Worker Model: Gunicorn follows a pre-fork worker model, where it creates multiple worker processes during startup. Each worker is a separate Python process that can handle incoming requests independently. This model allows Gunicorn to handle concurrent requests efficiently.

  • Load Balancing: Gunicorn supports various worker types, including synchronous, asynchronous, and thread-based workers. This enables it to handle different types of applications and workloads efficiently.

  • Graceful Restart: Gunicorn supports graceful restarts, which means it can reload workers without dropping active connections. This feature is crucial for maintaining the availability of the web application during updates or code changes.

  • Configurability: Gunicorn provides various configuration options that allow you to customize its behavior according to your specific application needs.

  • Integration with Popular Frameworks: Gunicorn integrates well with popular Python web frameworks like Django, Flask, Pyramid, and more. It can be used as a standalone server or in combination with other tools like Nginx or Apache to create a production-ready web application stack.

  • Using Gunicorn as a WSGI server is a common choice for deploying Python web applications in production environments due to its performance, reliability, and ease of use. It helps handle concurrent requests efficiently, making it suitable for serving web applications to multiple users simultaneously.

How docker init helps in producing best practices Python Images?

If you look closely, docker init produces Docker image with the best practices. Few examples:

1. ENV PYTHONDONTWRITEBYTECODE=1

The PYTHONDONTWRITEBYTECODE environment variable is a Python environment variable that controls whether Python bytecode files (.pyc files) are generated or not. When you set it to 1, it instructs Python not to write bytecode files, which can be useful in certain scenarios.

Here's what it does:

  • When PYTHONDONTWRITEBYTECODE is set to 1, Python will avoid creating .pyc files. These bytecode files are generated by the Python interpreter to improve the startup time of Python scripts, as they can be reused if the source code doesn't change. However, in some cases, you might not want these files to be generated.

  • Setting PYTHONDONTWRITEBYTECODE to 1 is commonly used in production environments or when you want to ensure that your Python code does not create additional files on the file system.

For example, in a Docker container or a production server environment, you might set PYTHONDONTWRITEBYTECODE=1 to minimize the number of files created and to prevent any potential issues related to file system access or disk space usage.

You can set this environment variable in your shell or in your deployment scripts like this:

export PYTHONDONTWRITEBYTECODE=1

Or, if you're running a Python script, you can set it directly in your script before running your Python code:

import os
os.environ['PYTHONDONTWRITEBYTECODE'] = '1'

Just be aware that setting PYTHONDONTWRITEBYTECODE to 1 will disable the creation of .pyc files, and in some cases, it may slightly increase the startup time of your Python scripts as Python won't be able to use cached bytecode.

2. ENV PYTHONUNBUFFERED=1

The PYTHONUNBUFFERED environment variable is another Python environment variable that controls the buffering behavior of the standard streams (stdin, stdout, and stderr) when running Python scripts.

When you set PYTHONUNBUFFERED to 1, you're essentially telling Python to run in unbuffered mode. Here's what it does:

By default, when Python writes to stdout and stderr, it uses buffering to improve performance. This means that the output is not immediately displayed but is stored in a buffer until it reaches a certain size or a newline character is encountered.

Setting PYTHONUNBUFFERED=1 disables this buffering behavior. It makes Python write each output line immediately to stdout and stderr without buffering. This can be useful in certain scenarios, especially when you want to see the real-time output of a Python script, such as when running it in a Docker container or when dealing with log aggregation systems.

Here's how you can set PYTHONUNBUFFERED in your environment:

export PYTHONUNBUFFERED=1

Or, if you're running a Python script, you can set it directly in your script before running your Python code:

import os
os.environ['PYTHONUNBUFFERED'] = '1'

Setting PYTHONUNBUFFERED=1 is particularly useful when you want to ensure that the output of your Python script is immediately visible, which can be crucial for debugging, monitoring, and logging purposes in certain situations.