- [Instructor] Now, let's build a Dockerfile.
Let's start with the most basic Dockerfile
that's still at all interesting.
Make a file called Dockerfile, with a capital D,
and I'm gonna put this in it,
and then I'm gonna build it and run it.
Let's go take a look.
So first, let me make a spot
to put our examples, mkdir example.
Alright, now let's go into that folder, ls.
Nothing there.
I'm gonna use a simple text editor, nano Dockerfile.
Alright, the first line in a Dockerfile
specifies what image to start with.
Where do we begin?
That's FROM, and I'm gonna start
from an image called busybox.
It's very small and it has nothing but a shell in it.
Super tiny.
And starting from busybox, I would like to RUN,
so create a container in that container, RUN echo,
building simple docker image.
Okay, after that's run, it's gonna save it into an image,
and then from that image, it's going to set
CMD to run when this image is started.
So, when this image is started,
I'm gonna have it run echo,
hello container.
So now I'm gonna exit, Control + X.
It asks me do I wanna save?
Yes, and I want to save it to the file Dockerfile.
So I've made a Dockerfile.
Now, let's build it.
Docker build -t hello, just to give it a name, dot.
Remember that dot?
Very important.
Alright, built pretty quickly.
Let's go through what this did.
It printed out the step, FROM busybox,
it created the image that we will be starting with,
47bcc, don't have to remember these numbers.
Step two, it said starting from that image,
create a container, run this command.
So it said okay, I'm running in the container,
55d71 16, dot dot dot.
Alright, did that.
And then, running in that container,
it ran the line where I have
echo building simple docker image.
There we see, the echo ran,
and it wrote this to the screen while it was building.
Okay that container is done, and it produced this image.
Now it's saying that it's got an intermediate container,
55d71, which was created here to run our build step.
It's saying, nobody's gonna use this.
It's not referenced anywhere farther down in the Dockerfile,
so I'm gonna go ahead and clean it up.
See how this number matches that number?
So it cleaned up the intermediate container,
and left this image.
Now, starting in that image, it's going to set the CMD
to be run when this container starts,
so it's updating the state of the image.
That resulted in a container called dd715, dot dot dot,
which then was committed to produce the image, df9ec.
And since no further step is going to use the container,
dd715, it said okay, I'm gonna go ahead
and clean up that intermediate container,
and the final result is the image, df9ec3,
which is the image from the last step.
And then it went ahead and tagged it with the name hello.
It doesn't print the tagging line right here.
So now we've walked through the full build process
for a very simple Dockerfile.
Let's run it and see if it works.
I'm gonna put the --rm there
so that it cleans up the container afterwards.
Hello container!
Now, let's make a slightly more interesting image
which actually installs something in the image.
For this one, we're gonna start from debian
instead of busybox, because we want to install things,
and I'm just gonna have a step
that says update the latest available packages,
install nano, and we're gonna make an image
that when you run it, opens up a little notes file
for you to type some notes in.
Very simple, but it expresses all the important concepts.
So I'm back in my example directory.
I'm going to edit my Dockerfile,
and change it to be more interesting.
Instead of starting from busybox this time,
I'm gonna start from debian version sid.
And the RUN step, instead of just printing
a little text to the screen and not actually doing anything,
we're gonna add a RUN step that says,
the -y says don't ask me yes or no do I want to update,
just go ahead and say yes.
Then, once everything's updated,
we're gonna go ahead and install nano.
Then, we're gonna set the CMD to run
when this container starts.
In this case, I'm gonna use a different format
for writing the CMD, just so that we get a chance
to demonstrate both formats.
This way of writing it is called the exec form.
Note the comma there.
It matters.
This says, when the container starts,
it will run the command, /bin/nano,
with the argument, /tmp/notes, to open it up in an editor.
Let's save that.
Yes, save it to Dockerfile.
Docker build -t example/nanoer.
We started from debian version sid.
Now it started a container called f24, dot dot dot,
and in that it's running apt-get update.
Doing a whole lot of work, alright.
Down here, it removed that container,
and it set the CMD to start when this image runs,
/bin/nano /tmp/notes.
Alright, and it produced an image we can use.
Let's go ahead and run that.
Docker run --rm, so that it cleans up afterwards,
and since I'm running an interactive command,
inside this container I want to run it
in an interactive mode with -ti.
Example/nanoer.
Note I'm not putting a command at the end
like we have in all the previous examples in this course,
because the command to run is baked into the image.
Alright, I have some notes file.
TODO make this more interesting,
save.
Alright, all done there.
Now let's make an even more interesting Dockerfile.
This one is gonna say, start with a notes file
with some content already in it.
The important part about this one
is that I'm going to begin this Dockerfile
by starting off where the previous example left.
Instead of saying FROM debian,
I'm gonna say FROM example/nanoer,
which says, start from the image I just produced
in the last example, and add more to it.
This is kind of what makes Dockerfiles cool,
the way you can chain them together to build on each other.
I'm back in my examples directory.
Let's make this Dockerfile more interesting.
Instead of starting from debian:sid,
we're gonna start from where we left off.
So now we're just gonna ADD a file called notes.txt
to /notes.txt.
Alright, so now let's change it
so when this container starts,
we open our pre-created notes file.
Okay, let's save that.
Now, nano notes.txt.
Let's get ourselves started with a notes file.
TODO, learn more about dockerfiles.
Okay.
Let's build it, and tag it example/notes, dot.
Alright, it started right from where it left off,
added the file, set the CMD to run
when the container starts.
Let's run it.
I'm gonna add the --rm here so it cleans up,
make it interactive, and run example/notes.
Alright, there's our first TODO item,
to learn more about dockerfiles.