In the and register there with your email and password. You will be asked also to create an username
git clone https://github.com/username/hello-world-docker-github
# Remember to change 'username' with your actual username!
cd hello-world-docker-github/
And now, let's create and start editing our app.py file (.py is the extension that python scripts have):
touch app.py
code app.py
The touch command creates the file, whereas the code command opens the file within Visual Studio Code (a IDE, Integrated Development Environment) to modify it. You can obviously use different IDEs: no IDE is better than another one, as long as it works for you.
Generally, a "hello world" application prints "Hello world!" on the terminal, like this:
print("Hello world!")
But we want to do something more: we don't like vanilla white text on our terminal, we want some 🌈color🌈.
To do this, we simply need to install the .
Once the change is pushed, you will see, on your online GitHub repo, a brown dot: it means that the workflow we triggered with our push is now working and will build and push the image. If the workflow run is successful, you will see a green tick at the end of it, whereas if it fails, you will see a red cross.
Make sure that, once the package is created, it is public: if not, change its visibility to public, otherwise you won't be able to download it.
Testing - Trying our app
As we said, the Docker image will be loaded as a package under ghcr.io; we can then pull and run the image like this:
docker pull ghcr.io/username/hello-world-github-docker:main
docker run -t ghcr.io/username/hello-world-github-docker:main
If Docker says that it can't pull the image because you are not logged in to the GitHub Container Registry, you can simply run:
docker login ghcr.io -u username -p GITHUB-ACCESS-TOKEN
And this should do the magic!
When we run our application, we should see a colored "Hello world!" printed on the terminal.
Modifying our application
Now, let's say that we want to let the user choose the color they want "Hello world" to be printed with. We can modify our app.py like this:
from termcolor import cprint
colors = ["red", "green", "blue", "magenta", "yellow"]
# Tell the user the instructions
print(f"Hello user! What color would you like 'Hello world' to be printed with?\nChoose among: {', '.join(colors)}")
# Take the input from the user
color = input("-->")
# Check if the input is in the available colors: if not, tell the user that it is not available
if color.lower() in colors:
cprint("Hello world!", color=color)
else:
print("ERROR! The color you chose is not among the available colors :(")
To modify the Docker image, it is now sufficient to add, commit and push the local changes to the online repo:
git add .
git commit -m "user defined color"
git push origin main
If the workflow run is successful again, we should be able to pull and run the new image with updated features:
docker pull ghcr.io/username/hello-world-github-docker:main
docker run -it ghcr.io/username/hello-world-github-docker:main
We will stop here for this article, but in the next one we will see, hands-on, how to use Docker for on-cloud deployments: stay tuned and have fun!🥰
SOCIAL SHARE CARD GENERATOR