Before we move on.
There's just one last little quick thing that I want to show you around this entire image building process.
Now, what I'm going to show you inside this section is not something that you're probably going to
do very often or ever, really, but it's something I thought you might find a little bit interesting
and help you get a little bit of a better handle on the relationship between an image and a container.
When we were going over the flow of actions that occurred while we were building the image, we spoke
about how we take an image from the previous step.
We create a container out of it, we do something with that container, and we then essentially create
an image out of that running container or that kind of very temporary, temporary container that was
created.
So at this point, you and I know very well that we use images to create containers, but based on everything
that we've been talking about in this section, it sure seems like the opposite is kind of true as well.
It seems like we can kind of take a container and generate an image out of it as well.
And that actually is correct.
You and I can manually create a container, run some commands inside of it, or change its file system
and then generate a usable image that we can then use at some point in the future.
In other words, we can kind of very manually do the same thing that our Docker file does.
We can create a container, run a command like say apk, add this right here and then generate an image
out of that.
I want to very quickly show you how that would look.
But again, I really don't expect you to do this on your own personal project.
I'm just showing you this as kind of something you might find interesting.
So let's try to kind of emulate the Docker file that we just put together.
We're going to create a new container manually out of the alpine image.
We're then going to install Redisai on it.
We're then going to set up a default command and generate a usable image that can be used in the future.
Essentially everything that our Docker file just did.
So at my terminal I'm going to execute docker run.
And we're going to start up a shell inside of here so that we can run a couple of commands inside of
it.
So I'm going to put on the IT flag I'll put on alpine as our image.
And then the command that I want to execute inside there is sh to start up a shell.
So then I get presented with a command prompt right here that is inside of that running container now.
And so inside that container I'm going to manually install Redice by running APK add dash dash update
redice like so.
So we now have a running container where we have modified its file system.
And the file system has now seen the installation of Redice.
So we've got the running container.
I'm now going to open up a second terminal window.
And inside of here I'm going to run a command using the Docker CLI.
That's going to essentially take a snapshot of that running container and assign a default command to
it and generate an image out of the entire thing.
So to do so I'll say first docker PS to get the ID of that running container.
And then we'll do docker commit.
I'm going to add on a dash C right here, which is going to allow us to specify the default command.
So I'm going to say dash C.
And then I'll put in cmd double quotes inside of the square brace Redice dash server.
I'll close the double quotes.
I'll close the square brace and I'll close the single quotes.
So notice how we have a single quote, single quote, and double quotes around it.
And then after all that, I'll paste the ID of that running container.
So I'm going to paste it in like so.
And the output is the ID of a new image that we just customized for our own uses.
Now you'll notice that this is a really long ID right here.
One shortcut that I haven't quite mentioned so far.
Whenever you're making use of IDs or hashes in the Docker world, you don't have to copy the entire
thing.
You can just copy a segment of the start, and it's kind of assumed that this is going to be unique
enough that Docker is going to take this and realize that, oh, you're probably talking about this
very long ID right here.
So I'm going to copy just an arbitrary number of leading characters here.
And then we'll do a Docker run that, and that's going to start up a new container out of the image
that we just created.
And inside that running container we already have this installed.
And the default starting command will be to run Redisai server.
So let's run that and we'll very quickly see all the typical output that we see from a running Redisai
instance.
All right.
So like I said, we basically just very manually created a container, added a dependency to the container,
set up the default command, and then generated an image out of all that.
Again you really don't want to use that practice.
In general.
You don't use Docker commit in general.
You really want to use the Docker file approach, because it allows you to very easily rerun that series
of steps again in the future.
But again, I just thought you might find it interesting how yes, there really is a very kind of fluid
relationship between containers and images.
All right.
So let's take another quick pause right here and we'll move on to the next section.