Skip to content

Instantly share code, notes, and snippets.

@nmagee
Created March 12, 2026 13:22
Show Gist options
  • Select an option

  • Save nmagee/cebda640403cbe348532f8df08fa59a5 to your computer and use it in GitHub Desktop.

Select an option

Save nmagee/cebda640403cbe348532f8df08fa59a5 to your computer and use it in GitHub Desktop.

Joke Machine Container Image

This simple application fetches the setup and punchline from a joke API. The larger point is to demonstrate the steps involved to containerize a Python application and its dependencies.

Test

Create a Python virtual environment and install the requests library. You can then test the script by running:

python joke.py

Feel free to inspect the full JSON payload from the API, or customize the output however you like.

Build

To build the container:

docker build -t joke-machine .

In this case joke-machine will be the name of your container image. Don't forget to include the trailing dot at the end of the command, which directs docker build as to where to find the Dockerfile.

Run

To run the joke machine image:

docker run joke-machine
FROM python:3.12-slim
RUN python3 -m pip install requests
COPY joke.py /joke.py
ENTRYPOINT ["python3", "joke.py"]
import httpx
import time
url = 'https://official-joke-api.appspot.com/random_joke'
payload = httpx.get(url)
response = payload.json()
setup = response['setup']
punchline = response['punchline']
print(f"{setup}")
time.sleep(3)
print(f"{punchline}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment