<!--
  canonical-url: https://www.avonture.be/blog/docker-python
  published:     2023-11-29
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# Play with Docker and Python

> Forget complex Python setup. Learn how to quickly run your Python scripts using Docker containers, from "Hello World" to a Hangman game, and simplify your development.

---
![Play with Docker and Python](/img/v2/python.webp)

**TL;DR**

This article runs Python scripts with zero local install using the official `python` Docker image (`docker run ... python python Hello.py`), going from a "Hello World" script to running a console Hangman game — showing how quickly you can start experimenting with a new language thanks to Docker.

Do you remember when you wanted to learn a new programming language such as Python?  What did you need before you could even program your first *Hello World*? You had to install the language on your computer; maybe you also had to install a lot of libraries/dependencies; you had to spend time configuring your machine before you could even start writing your first script.

That's all over with the advent of Docker and the concept of containers.

This time, we'll play with Python for this article. **I am discovering Python at the same time as writing this article: at this precise moment, I have never written `.py` scripts.**

## Welcome to the world

Please start a Linux shell and run `mkdir -p /tmp/python && cd $_` to create a folder called `python` in your Linux temporary folder and enter it.

Please create a new file called `Hello.py` with this content:

**Hello.py**

```python
print("Hello World!")
```

So, now, how to run that script? Because I'm familiar with Docker, I know that:

1. I need a Python Docker image (<https://hub.docker.com/_/python>),
2. I will need to use a `docker run` command line instruction,
3. I will need to share my script using a volume and
4. I need to know how to run the script.

```bash
$ `docker run -it --rm -v \${PWD}:/app -w /app python python Hello.py`
Hello World!
```

And voilà, my first Python script has been written. Remember the old days, before Docker, how many hours and how much reading you needed to be able to run your first script? **Here, it did not take me five minutes to get up and running.**

*Once past "Hello World", two articles take this further: [Docker - Python devcontainer](/blog/docker-python-devcontainer) for a real VSCode environment, and [Python - Code Quality tools](/blog/python-qa) to keep your scripts clean from day one.*

> **Docker CLI reminder:**
>
> As a reminder, the used Docker run commands are (almost always the same):
>
> * `-it` to start Docker interactively, this will allow the script running in the container to ask you for some prompts for example,
> * `--rm` to ask Docker to kill and remove the container as soon as the script has been executed (otherwise you will have a lot of exited but not removed Docker containers; you can check this by not using the `--rm` flag then running `docker container list` on the console),
> * `-v ${PWD}:/app` to share your current folder with a folder called `/app` in the Docker container,
> * `-w /app` to tell Docker that the current directory, in the container, will be the `/app` folder
> * then `python` which is the name of the Docker image to use (you can also specify a version like `python:3.9.18` if needed; see <https://hub.docker.com/_/python/tags>) and, finally,
> * `python Hello.py` i.e. the command line to start within the container.

## Playing the hangman

As said above, in November 2023, I haven't started to learn Python so let's try to find some sample scripts. On <https://hackr.io/blog/python-projects>, we can find a Hangman script.

Please create the `Hangman.py` file on your disk with this content:

**Hangman.py**

```python
import os
import random
import time


def play_again():
    question = "Do You want to play again? y = yes, n = no \n"
    play_game = input(question)
    while play_game.lower() not in ["y", "n"]:
        play_game = input(question)

    if play_game.lower() == "y":
        return True
    else:
        return False


def hangman(word):
    display = "_" * len(word)
    count = 0
    limit = 5
    letters = list(word)
    guessed = []
    while count < limit:
        guess = input(f"Hangman Word: {display} Enter your guess: \n").strip()
        while len(guess) == 0 or len(guess) > 1:
            print("Invalid input. Enter a single letter\n")
            guess = input(f"Hangman Word: {display} Enter your guess: \n").strip()

        if guess in guessed:
            print("Oops! You already tried that guess, try again!\n")
            continue

        if guess in letters:
            letters.remove(guess)
            index = word.find(guess)
            display = display[:index] + guess + display[index + 1 :]

        else:
            guessed.append(guess)
            count += 1
            if count == 1:
                time.sleep(1)
                print(
                    "   _____ \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "__|__\n"
                )
                print(f"Wrong guess: {limit - count} guesses remaining\n")

            elif count == 2:
                time.sleep(1)
                print(
                    "   _____ \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "__|__\n"
                )
                print(f"Wrong guess: {limit - count} guesses remaining\n")

            elif count == 3:
                time.sleep(1)
                print(
                    "   _____ \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |      \n"
                    "  |      \n"
                    "  |      \n"
                    "__|__\n"
                )
                print(f"Wrong guess: {limit - count} guesses remaining\n")

            elif count == 4:
                time.sleep(1)
                print(
                    "   _____ \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |     O \n"
                    "  |      \n"
                    "  |      \n"
                    "__|__\n"
                )
                print(f"Wrong guess: {limit - count} guesses remaining\n")

            elif count == 5:
                time.sleep(1)
                print(
                    "   _____ \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |     | \n"
                    "  |     O \n"
                    "  |    /|\\ \n"
                    "  |    / \\ \n"
                    "__|__\n"
                )
                print("Wrong guess. You've been hanged!!!\n")
                print(f"The word was: {word}")

        if display == word:
            print(f"Congrats! You have guessed the word '{word}' correctly!")
            break


def play_hangman():
    print("\nWelcome to Hangman\n")
    name = input("Enter your name: ")
    print(f"Hello {name}! Best of Luck!")
    time.sleep(1)
    print("The game is about to start!\nLet's play Hangman!")
    time.sleep(1)
    os.system("cls" if os.name == "nt" else "clear")

    words_to_guess = [
        "january",
        "border",
        "image",
        "film",
        "promise",
        "kids",
        "lungs",
        "doll",
        "rhyme",
        "damage",
        "plants",
        "hello",
        "world",
    ]
    play = True
    while play:
        word = random.choice(words_to_guess)
        hangman(word)
        play = play_again()

    print("Thanks For Playing! We expect you back again!")
    exit()


if __name__ == "__main__":
    play_hangman()
```

Then, run it using `docker run -it --rm -v ${PWD}:/app -w /app python python Hangman.py` and Good luck:

```none
Hangman Word: ______ Enter your guess:
A
   _____
  |
  |
  |
  |
  |
  |
__|__

Wrong guess: 4 guesses remaining

Hangman Word: ______ Enter your guess:
E
   _____
  |     |
  |     |
  |
  |
  |
  |
__|__

Wrong guess: 3 guesses remaining

Hangman Word: ______ Enter your guess:
I
   _____
  |     |
  |     |
  |     |
  |
  |
  |
__|__

Wrong guess: 2 guesses remaining

Hangman Word: ______ Enter your guess:
C
   _____
  |     |
  |     |
  |     |
  |     O
  |
  |
__|__

Wrong guess: 1 guesses remaining

Hangman Word: ______ Enter your guess:
O
   _____
  |     |
  |     |
  |     |
  |     O
  |    /|\
  |    / \
__|__

Wrong guess. You've been hanged!!!

The word was: border
Do you want to play again? y = yes, n = no
```

Darn, I lost.

It is up to you to find out... You will find a script that will [test the strength of a password](https://hackr.io/blog/python-projects#toc-6-password-strength-checker); it is up to you to create the `check_password.py` script and run it on your machine.

If you're new to Docker itself, start with [Docker - Explain me like I'm five](/blog/docker-definition-like-im-five). And since the whole point is that this approach works for any language, I've done the same experiment with [Pascal](/blog/docker-pascal), [Assembly](/blog/docker-assembly), and [Java](/blog/docker-java).
