Now up to this point,
we always had a look at this demo Node.js application
which spin up that web server,
and indeed web development and running web servers
and other processes that deal with incoming requests
and so on, that is the main selling point of Docker.
That is the use case it's getting used for the most
no matter if you then use Node.js
for building that web service
or if you use PHP, Python, whatever it is.
But it's not just that, Docker is not restricted to that,
and attached you find another example.
A very simple Python application
if you wanna call it like this
which is built for Python version three
and that's important here,
which does not create any web server or anything like that.
Instead of this application, in case you don't know Python
fetches two user inputs a min and a max number,
stores those in variables, runs a quick check,
seeing whether the min number really is smaller
than the max number,
and then we calculate a random value here
and output that value.
Now you can install Python on your system to run this
or you put it into a Docker image and container,
and that's what I wanna do here
for two main reasons, of course, to show you
that Docker is not just about node and web servers
I said this before, but I also wanna show it to you.
But the second reason is related to what we learned
in the last lecture attached and detached containers.
So containers running in the front and in the background.
Here, this will play a greater role because this app
actually needs the user to interact with the app.
Running this example here in background
won't really work, but step-by-step, let's Dockerize this
and of course, if you feel like this
definitely also try it on your own.
Otherwise you can now follow along with me
as I will write a Docker file for this application.
Now, how could we run this with Docker?
Well, in the end I wanna build a custom image
which uses Python as a base image
so that I can copy my code, the Python code file here
into that image and executed with the help of Python.
Now on Docker hub I can therefore search for Python
and will find official Python images here
and that of course means
that here we can absolutely use Python as base image.
So we wanna use the Python base image as a next step,
I also wanna set a working directory
and I'll again, set this to app
this of course is up to you.
Now as a next step
I wanna copy in my code files
or in this case, the single code file which I have here
still I can use copy dot
and copy everything in the local folder
which essentially is the Python file
therefore into this app folder.
Now, as a last step we can add the command here
that should be executed when this container is started
and here, I wanna use the Python executable
which will be available, thanks to the Python image
and run the RNG dot PY file with it.
That's my command with which this container
should start when we started
and that could be our Docker file here.
Now with that created
we of course can run Docker build dot to build this image,
and since this is the first time I'm building this
it will, of course also download this Python base image
and they offered this initial building takes a bit longer.
So let me wait for this to finish
and now with that done, this image is built.
Now we can use the image ID here
to then bring up a container by running Docker run.
Now here, I don't wanna publish any port
because we're not exposing any port here,
it's not a network based application
so no needs to do that
instead I really just wanna run it.
And if I run it like this though I get an error here.
To be precise, I see some output here
because with Docker run as you learned
by default we start in attached mode,
but I don't see anything else besides this error message
I'm not able to interact with this running container
and with the application running into container
and the reason for that is that by default,
yes, when using Docker run
we are attached to the container
so we can listen to output printed by the container
but we're not attached to it
in the sense of being able to enter anything,
we can't input anything into the container
or into the application running into container
and that is something I need to fix here.
Now for that let's execute dash, dash help
the dash dash help flag on Docker run
and see which useful configuration options we got here.
And here we actually have two options that will help us.
The first one is the dash I option
which allows to launch this container in interactive mode,
you could also use dash dash interactive instead
but of course, dash I is shorter, so we'll use that
this keeps standard input open even if not attached
which means we will be able to input
something in a container.
However, typically we combine this with dash T
this allocates a pseudo TTY
and in the end this means it creates a terminal
and therefore if you combine the I and the T flag
we are able to input something
so the container will listen for our input
and we'll also get a terminal exposed by the container
which is actually the device where we enter the input.
So with that, we can rerun this container
but now by adding the dash I and the dash T flag
and we can also combine this to just one flag dash IT
and with this now you see it doesn't crash
because now it's getting the input it needs
and we can now type something here in our terminal
which communicates with this pseudo terminal
exposed by the container.
Which then in turn, also is connected
to the container process that listens for user input.
So here I can then enter a min and a max number
and I get back this random number.
And there after you see the container shuts down
it's no longer running
we see it with dash A but that is okay here.
We could now restarted with Docker start using that name
and it would be running again.
However, of course, now you see we have a problem
now, since Docker starts in detached mode by default
we can't communicate with the container.
So how can we now deal with that?
If we restarted the container.
Well, there are two ways,
for one, let me stop this again real quick
we can also restart the container
in attached mode, of course.
So if I do that Docker start and I add dash A here
we start an attached mode
and now we can also enter something here
but only once, there after it behaves strangely.
So that doesn't look right.
So let me cancel this
and then also stop this container again
and show you the other way of restarting this container
and being able to input something again.
Because if we have a look at Docker start
configuration options, you'll see we don't just have dash A
for listening to output
but all the dash I to again, be able to input something
into the container.
Now here will not need the dash T flag
because that was still memorized,
if we ran the container originally with that flag,
with the Docker run command which we did,
but I need to kind of re-emphasize
that when I restarted here, I also again wanna listen.
So now, I can again start this container
but now also add dash A and dash I
and now I can enter a min and a max number
and I get back my random number.
So this is how we can now restart this
and still attach us to the container
but not just in the listen only mode
but also in the I want to input something mode.
And this hopefully again, shows you the idea
behind being attached or detached
and why some applications like this one
might need us to be attached
not just for listening to output, but also to provide input
because Docker is not just about web servers
and web applications and long running processes.
Docker can also be used to dockerize
simple utility applications like this
which need input and provide some output
and eventually might be done.
That is also something you can use Docker for,
Well, and that is how you can use Docker for this.
Now, with that said,
I will go back to this node application
which will serve as an example for the next lectures,
but I hope it's clear that you cannot just use node
and not just use web servers
and that you can interact with the container
exactly as you need to,
if you don't need interaction you don't have to,
but if you do need it
you can communicate just as shown in the lecture.