A very simple Python application. Using the material in Docker's Get Started guide this is a simple (classic) Python-based web-app that uses Flask and Redis. You don't have to understand any of this, the goal of this project is simply to provide a stable base for demonstrating Docker.
You should be able to build and run the containerised app using
any suitably equipped Docker host. I used a Mac
and Docker 20.10.11
.
Build and tag the image...
$ docker build -t friendlyhello .
And run it (in detached/non-blocking mode)...
$ docker run -d -p 4000:8080 friendlyhello
<container ID>
This will start the image as a container and map host port 4000
into the
container's on port 8080
. This port re-mapping is an essential part of the
Docker ecosystem. The long-and-short of all of this is that you should now
be able to connect to the app at http://localhost:4000
:
$ curl http://localhost:4000
Now stop your container and remove the stopped container using its ID:
$ docker stop <container ID>
$ docker rm <container ID>
You can also use docker-compose
and the built-in docker-compose.yml
configuration file to build and launch more than one container. In our compose
file we start the PySimple container but also start a Redis container,
a simple database that the app will use (if it can) to count the number of
visits made to its port.
$ docker-compose build
$ docker-compose up --detach
The docker-compose configuration exposes the application container using port 8080 on the local host.
And stop and remove the containers with: -
$ docker-compose down
Here, we'll deploy to the Docker hub. It's free and simple. We just need to tag our image and then push it.
$ docker login -u alanbchristie
[...]
$ docker build -t alanbchristie/pysimple:2019.6 .
$ docker push alanbchristie/pysimple:2019.6
Here my Docker account is
alanbchristie
and the repository there ispysimple
. We're giving this image the tag2019.6
.
Tags are handled very differently in Docker, it does not understand that
1
is better than2
- they're just strings that happen to also be numbers.
With the Docker image pushed we can now pull the application onto any Docker-enabled machine and run it with the command: -
$ docker run -d -p 4000:8080 alanbchristie/pysimple:2019.6
Unable to find image 'alanbchristie/pysimple:2017.6' locally
2019.3: Pulling from alanbchristie/pysimple
[...]
To build and push an image for the ARM processor (suitable for a Raspberry-Pi Kubernetes/k3s deployment): -
$ docker build --build-arg from_image=arm32v7/python:3.10.1-alpine3.14 \
-t alanbchristie/pysimple:arm32v7-latest .
$ docker push alanbchristie/pysimple:arm32v7-latest
The following related GitHub repositories might be of interest: -
Alan B. Christie
December 2021