Welcome back.
Now go ahead and open up the workbook starter projects.
Uh, now we're going to run a Ruby app, a Python app, as well as a go app without installing any of
their respective, uh, environments into our machine.
Everything is going to happen inside of Docker itself.
Let's start with the Ruby application.
So many of you are probably not familiar with Ruby, uh, and probably never will use Ruby after this
course.
And that's okay.
We're just going to run a simple Ruby application for the sake of demonstrating that regardless of whatever
app you're running, the process is going to be similar.
Okay, now, am I going to force you to install the Ruby environment into your machine just to run this
app?
No, everything is going to happen inside of Docker itself, which is the beauty of running Docker containers.
So here, the developer who created this app, they're telling us how to run it bare metal.
We are going to modify the command so that it runs within our container properly.
So let's go ahead and create a docker container.
Docker run dash v.
Actually, before we do any mounting, we're going to specify the flag dash dash RM to remove the container
once we're done with it.
And dash v we're going to use to mount.
Let's go to the ruby schematic.
We're going to use it to mount the source code that is inside of our host machine into a working directory
within our Docker container.
All right.
So we need to get the path.
Where our code lives in our machine and mount it into some working directory inside of our container.
You can call the working directory whatever you want, as long as you're consistent when you provide
the container with a command.
We're going to call the container, uh, Ruby application.
And the container.
Now it has the application mounted at the working directory.
Now it needs a Ruby environment in order to run this app.
Going over to Docker Hub, let's write Ruby.
And this is the official repo for a bunch of Ruby images.
Uh, we're going to get the Ruby image tagged 3.0.0.
Okay.
And let's go ahead and pull Ruby 3.0.0 into our Docker container.
So at this point our container has the Ruby image.
Remember an image is a snapshot of a preconfigured environment taken at a specific point in time that
we can use to run this Ruby app.
So now we can provide the container with the command Ruby.
Slash app slash scriptdb.
So it's using the Ruby command to run the script dot rb file that it's going to be inside of the container's
working directory.
Let's go ahead and run this.
It's unable to find the ruby image locally.
So it's going to pull this pre-configured ruby environment into our local Docker cache, where our container
can have direct access to it and thereby execute this Ruby command on our app.
And that's exactly what it did.
All right.
Now let's move on to the Python script.
All right.
So let's go ahead and press clear.
And I'm going to go to my Docker dashboard which you can access on Mac by going over here and clicking
on the go to dashboard icon on windows by going over to the bottom right toolbar, clicking the bottom,
clicking the Docker icon.
It'll take you here.
Remember what's nice about Docker is everything happens inside of it.
So by virtue of removing this Ruby image.
It's like nothing ever happened.
There is no trace of any ruby.
Activity on our system, and there are no containers because we specify dash dash m.
Now we move on.
To the Python app.
So again we're going to mount the Python app into the working directory of whatever container we create.
We're going to pull a Python environment so that our container can use it.
Um.
Then we're going to run the Python application by giving it command line arguments as well.
So the script.py file was programmed to accept command line arguments.
So when we provide the container with the uh with the executing command, we need to make sure to give
it the arguments that the application expects.
All right.
So let's say Docker run and we're going to create a container that automatically gets removed.
Once we're done with it then we're going to say volume mount.
We're going to mount we're going to get the path.
The folder where the application lives, and we're going to mount whatever is inside of the Python script
folder into a working directory within our container.
We're going to call the container Python script container.
And we're going to create we're going to pull the image Python 3.8 slim.
So we're so for the sake of exercise we're going to locate it within Docker Hub.
Go to the official Python repo.
With all of the images.
We're going to look for the image tagged 3.8 slim.
This one right here.
We're going to pull that so that our container can use it to execute the command.
Python script.py.
And the application that exists at our container's working directory.
It expects command line arguments within the command itself.
So um, we can just say hello from Docker.
We're going to give it three command line arguments unable to find the image locally.
So we pull it from Docker Hub.
Can't open file script.py.
No such file or directory I messed up.
The application is inside of our container's working directory slash app, so I need to specify.
Python slash app slash Script.py so that it can find the application within the working directory that
we mounted it to.
And then we give that application the command line arguments that it that it that it is expecting.
All right.
Hello from Docker.
And so now we move on to the go application.
Uh, this app expects an environment variable named message to customize the greeting.
Now, an environment variable is a key value pair that an application expects to receive from the environment
that it's running in.
So in this case, the container is going to be responsible for providing this application with the message
environment variable so that this app can function accordingly.
All right.
So let's write clear to clear the output and create a container docker run dash dash rm that's going
to mount the application at this path.
Into the working directory of this Docker container.
And we're going to call the container.
Go.
Container.
And we're going to pull.
The image Golang 1.16 into our container.
1.16.
Let's just verify the image.
Make sure it exists.
It does.
So we're going to use this pre-configured Golang environment in order to run our application using the
command go run.
Now the Main.go file is going to be located within the container's working directory slash app.
So we say go run slash app slash Main.go.
And there's one thing I forgot to do, which is to provide this application that is going to be run
within the container with environment variables.
So when the container wants to provide its underlying application environment variables, it does so
using the flag dash e.
All right.
And the environment variable that the app is expecting is called message.
So we say dash e message is equal to some value which we can say hello from Docker.
All right.
And now if you press enter.
Unable to find the Golang image locally so it pulls it from Docker Hub.
And once it pulls the image, it can use it to execute the following command and run our go app.
Beautiful.
Our container was able to successfully run the go application and provide it with the environment variable
that the app was expecting.
Now I just want to note the difference between an environment variable that you see here, and the command
line argument that we passed into the Python script.
A command line argument is any value that you can pass directly to the application when running it from
the command line, and an environment variable is a specific key value pair that an application is expecting
to receive from the environment that it's running in.
So command line argument any value environment variable it's expecting a particular key value pair.
All right.
So we were able to run a go application a Python application and a Ruby application without installing
anything in our system.
This would not change whether we're using Java, whether we're using C plus plus.
You just need to make sure that you pull the appropriate image into the container.
Once you have that image, you can run whatever commands you need to run in order to execute said app.
Before we wrap up, go to your Docker dashboard.
Delete all the images that we pulled.
Uh, delete whatever.
Containers haven't been deleted already.
And you should be golden.
I will see you in the next section when we discuss Docker files.
See you then.