(With this, I just summarized for myself: How I proceed with this short session.)
Based on the Youtube video from Christian Lempa Docker VSCode Python Tutorial // Run your App in a Container I just did as described and build my first service from a short Python script runing in a Docker container - it is just a Hello World:
print("Hello World! - from Docker")
- Docker Desktop
- VScode
- VScode Plugin for Python
- VScode Plugin for Docker
- optional Pipenv: Python Dev Workflow for Humans
I prepare everything, as descriped by Youtube: Docker VSCode Python Tutorial // Run your App in a Container or Github: Docker VSCode Python Tutorial // Run your App in a Containerv
At the prompt
minimal_python_docker/
|
|-- .dockerignore
|-- .gitignore
|-- Dockerfile # instruction how o build the docker image
|-- hello.py # the Python script: a one-liner
|-- Pipfile # optional, just: python_version = "3.12"
|-- README.md # this text you currently reading
|-- requirements.txt # empty
Pipfile defined at prompt:
touch Pipfile
pipenv --python 3.12
Will give you:
> more Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
[requires]
python_version = "3.12"
-
Go to the terminal at directory minimal_python_docker and build a new docker image, that image will be of the platform of your host (arm64 or amd64).
> docker tag hello_from_docker .
Actually no multiplatfrom, for Multiplatform images amd64 + arm64, see Docker Docs: Multi-platform builds.
-
run the new docker image
docker run --rm hello_from_docker
you will get
> docker run --rm hello_from_docker Hello World! - from Docker
Docker builds images that are the templates about containers, only that containers can be execcuted.
If you just docker run <image_name>
, Docker will generate a container with a random-name in backgroud and start this container. This randomly-named container will stay and be visible in you Docker Desktop appication. (Do not forget to remove unsed container from your Docker Desktop from time to time.)
If you do not need that randomly-named container you can remove it.
Alternatively you can docker run --rm <image name>
with the --rm parameter, with this docker will remove the randomly-named container after execution.
If you have an account at Docker Hub, you can push your image to the Docker Hub, so you can anytime run that services provided with this image from any host where a Docker Desktop is installed
From Terminal
docker login -u ACCOUNT-NAME
You have to enter your password .
docker tag IMAGE-NAME ACCOUNT-NAME/IMAGE-NAME
e.g.:
docker tag hello_from_docker <account name>/hello_from_docker
docker push ACCOUNT-NAME/IMAGE-NAME
e.g.:
docker push <account>/hello_from_docker
docker run ACCOUNT-NAME/IMAGE-NAME
e.g.
docker run <account>/hello_from_docker