Hello World! In a Python 3.8 Docker Container

Getting started with docker.

I looked at docker a couple of years ago and then stopped for reasons I can’t remember. I shouldn’t have stopped because docker and containerization are the future. So now I am diving back into it. This is my “first” docker container.

Let’s Do It

  1. Make sure you have docker installed first.
  2. Make a new folder named python-hello-world.
  3. Make a new file in the folder named hello-world.py.
  4. Edit hello-world.py and paste in the following code:

     def main():
       print("Hello World!")
     if __name__== "__main__":
  5. Make a new file in the folder named Dockerfile.
  6. Edit Dockerfile and paste in the following text:

     FROM python:3.8-slim-buster
     ADD hello-world.py /
     CMD [ "python", "./hello-world.py" ]

    This is going to pull an official docker Python 3.8 image based on Debian Buster from the docker hub for our use. It is then going to add our Python script to the docker image. After it adds our Python script it is going to run a command python hello-world.py and then exit.

  7. Using the terminal, navigate to the folder and enter the following command to build the image:

     docker build -t python-hello-world .
  8. Now, enter the following command to run the image:

     docker run python-hello-world
  9. You should see Hello World! print out in the terminal.

You’ve run your first docker container. Success.

If you’ve done any sort of docker tutorial, one of the first images they have you run is a hello-world project like this, but I wanted to start a similar project myself to get a better taste of things from the ground up.

categories :  development