In this section we're going to build three images.
One image is going to package up a Ruby environment that will be used to execute Ruby source code.
One image is going to package up a Python environment that will be used to execute Python code, and
another image will be used to package up a go environment that will be used to execute go source code.
So if we go to my schematic and Docker, we use docker files to build Docker images.
This docker file is going to build an image that pulls the Ruby base image from Docker Hub, and the
base image is the foundation onto which additional layers of the overarching image will be added.
The second layer will copy the script dot rb file the Ruby source code into a working directory.
So at this point, the Docker image is going to have the source code at some working directory.
It's going to have the ruby environment that will be needed to execute the source code.
The last step is for it to provide the container that we will create from this image, with the commands
that it can use to execute the app.
So over here in step number three, a container is created from the Docker image.
The container has access to the source code inside of the image, as well as the ruby environment inside
of the image as well.
All it has to do is run the command that was provided to it, and it's going to be able to execute the
Ruby source code and start up a process.
All right.
So step one is going to be to create this Docker file.
Let's do it right now.
So we'll go to the Ruby Docker file.
And we're going to have to CD into this directory.
So right now I'm at Docker course remastered main I'm going to have to CD into Workbook starter projects
go into oh two starter projects, followed by Ruby user input.
Let me get a drink of water real quick.
All right.
Now the base image is going to be Ruby 3.0.
The base image is going to be the foundation onto which additional layers of our overarching image will
be added.
The working directory where we're going to place our Ruby source code will be slash app.
Really, you can choose any working directory you want.
I'm going to choose app.
That's kind of my go to default.
The code that I'm going to copy into the working directory will be script dot RB.
And the destination is the working directory.
Everything we do after step two happens inside of the working directory.
So by putting a dot on the second argument.
I'm saying copy script RB into the working directory.
Okay.
And then finally the image that we build needs to provide the container that's created from it with
a command that the container can use to execute the application.
Ruby script dot RB.
All right.
Now what I can do is say Docker build dash t.
I'm going to call the image Ruby app.
And we're going to create an image from the Docker file inside of the current directory.
So dot when used in this context means current directory.
All right.
For some reason the folder is called Ruby user input.
That doesn't make much sense.
But anyways it's just the name
Should have been called something like Ruby app or something.
Whatever.
So now it's pulling the image from Docker Hub.
That step is taking quite a long time.
That took a solid 10s.
After doing so, it created the working directory and copied our scriptdb file into it.
And now I can say docker run.
I'm going to create a container that gets removed as soon as we're done with it.
Um, the container is going to be called Ruby App container.
And we're going to create a container from the image that we just built.
Ruby app.
That's it.
Beautiful.
It is able to execute the application before I move on.
What I want to do is start up an interactive shell inside of the container.
So to do that I can say dash it Right over here.
Followed by slash bin slash shell.
All right.
If I say LZ, you can see that the container has already navigated into the working directory, which
means by executing the the command ruby script RB, it was able to execute the app.
So I'm basically simulating what the container did for us previously I can say LZ and I can CD out of
this directory.
Press LZ again.
And here I can see the working directory slash app, I can CD into uh, the working directory and see
the application that was stored inside of it.
That is why our container is able to access it.
All right, enough about that.
I'm going to exit and CD out of Ruby user input.
Again, this name doesn't make much sense.
It should have been called something else.
I'm going to CD into Python script.
This is the next image that we're going to build.
All right.
Um, once again we start by setting the base image.
Python 3.8 slim, the working directory where we are going to copy over the application.
We're just going to let it be slash app.
We're going to copy script dot p y into the working directory.
Remember everything after step two happens inside of the working directory.
Then we're going to give the container a command that it can use to run the application at the Workdir.
So Python script dot p y.
And remember that this application is programmed to accept, uh, any number of command line arguments.
We're going to pass in one command line argument of I am silly.
So it's going to happen Here is let's go over here.
From the Docker file, we're building a Docker image that contains a pre-configured Python environment
that's going to be pulled from Docker Hub, as well as an application that's going to be mounted at
the working directory.
Then we're providing whatever container is going to be created from this image with a command that it
can use to execute the application at the working directory.
It will start up a process inside of the container from the source code, and pass into the running
app command line arguments.
All right.
So let's go ahead and say oh I need I am CD into Python script.
But let me clean up this mess.
Clear.
Now I'm going to say docker build T.
We'll call the image python script.
Create an image from the docker file at the current directory.
It's pulling Python 3.8 slim.
All right we're good.
Now I'm going to say docker run dash dash m.
Em.
Um, we'll call the container Python script container.
We'll create a container from the image Python script.
Everything works beautifully.
Now I can start up an interactive shell in the interest of looking into the container itself.
Here, I'll just say dash it.
Oh, forgot the dot.
I mean, the dash.
All right.
LZ so as you can see, once again the container automatically navigates to the working directory and
is ready to run the application inside of it.
So now from the working directory our container ran the command python script.py with the command line
argument.
I am silly
Oh, I keep making mistakes.
Python script.py.
I am silly.
And we see a different output because the container ran it as one argument, whereas we ran it as three
separate arguments.
I am followed by silly.
All right, let's exit.
Move on to the last image we're going to create from the following.
So we'll CD out of Python script CD into go environment variables.
All right clear the terminal output.
Once again we start from the base image.
In this case the environment that we're going to use that we're going to package up alongside the source
code is going to be Golang version 1.16.
Then we're going to set up a working directory slash app.
Then we can copy the main dot go file into the working directory.
Once we do that, we can provide the container with a command that it can use the application that it
can use to execute the application at that working directory.
Why am I using the command workdir.
So that command is going to be um main dot.
Uh, go.
Run.
Main dot.
Go.
All right.
Uh, the last thing we got to do is build this up into an image docker build, dash t go app.
Looking at the schematic, we're building an image from the Docker file.
The image is going to have a go environment as well as the go source code copied into the working directory.
The Docker image is going to provide the container that's created from it with a command that it can
use to run the application.
When the container runs the application, it's going to start up a go process within the container,
and then we can pass the necessary environment variables into the application that's running inside
the container.
And you'll remember that the environment variables that the application expects to receive from its
environment is called message.
So over here we can say docker run dash dash m dash name.
We're going to call it go app.
And we can create an image a container from the following image.
Now we'll call it go app container.
And then we're going to create a container from the go app image.
And we're going to pass into the application that's running inside the container an environment variable
called message that equals hello from Docker.
Let's try it out.
Um, did I mess up?
Oh, I'm such a knucklehead.
I placed the environment variable between the name and between the name flag and the actual name.
So here we'll say dash e message is equal to hello from Docker.
These mistakes can happen when you've been looking at a screen for too long.
I recommend you go outside.
Go for a hike, don't do what I'm doing and we are good.
All right.
So now I'm going to say docker run.
Um, let's start up an interactive shell dash it
Slash bin.
Slash shell.
So here we've launched an interactive shell inside of the container that's currently running.
The container has an environment variable set up message.
Hello from Docker.
If I say ls, uh, we see that the container has already gone into the working directory and is ready
to execute Main.go.
So now if we say go run Main.go, the container is going to run the go application and pass into it
the environment variable message.
Hello from Docker.
Beautiful.
Now we exit.
We're good to go.
That is it for the section.
In the next section we're going to be looking at Containerizing web applications and microservices.
All right.
All of this is just one big e-commerce application split up into different microservices.
This is very close to stuff that you can expect in the real world.
And I can't wait to see you there.