- [Instructor] Now that we've got the Docker flow in mind
let's talk about running things in Docker
'cause that's what it's all about.
So to start off
we have the most important command, docker run.
We've been using it a whole lot
but haven't really talked about it.
Docker run starts a container by giving an image name
and a process to run in that container.
This is the main process to a container.
When that process exits, the container's done.
The container's not done till that process exits.
If you start other processes in a container later
which we'll cover, the container still exits
when that main process finishes.
So Docker containers have one main process
and they can have names.
If you don't give it a name it'll make one up.
Let's take a look at that.
So if I run docker run
just like the example we've been using a lot.
I'm going to throw in some extra arguments
we haven't used before.
So I'm going to throw in the dash dash rm.
This is a very common command.
Lots of people use it all the time.
When you just want to run something in your container
but you don't want to keep the container afterwards.
That says delete this container when it exits.
Otherwise you just have to go and say docker run,
do you stuff, exit, docker rm container name.
This saves a step.
Lots of people use it, you should know about it.
Dash terminal interactive ti just to make things look good.
Ubuntu and I'm going to run a command to say
sleep for five seconds.
So this will start a container,
it will sit there for five seconds, and then exit.
Boom, very exciting.
See the idea there?
We started a process.
The container existed for the duration of that process
and then it was done.
Now let's run a more fancy container.
So I'm going to say we're going to start a process.
That process is bash the shell.
And I'm going to give that shell an argument
that says run a few things.
Very often people will want to say
start a container that says first run this.
When it finishes, run the second thing.
This pattern is extremely common for that.
So I'm going to say for this example we're going to sleep
for three seconds.
And when we're done, the semicolon is bash
for when you finish the first command
start the second one.
It's going to echo all done.
Here we have one process batch which starts.
That bash process starts the sleep command.
When that's finished, it runs the echo, right?
So this is very common
when you want to start a container
and have it first do one thing in the container
then do another thing in the container.
All done.
Now let's talk about leaving things running in a container.
Docker has the idea of detached containers.
You can start a container running and just let it go.
So that is accomplished by doing docker run
dash, I'm going to put my usual dash ti
so it looks nice in the terminal.
Ubuntu bash.
But this time I'm going to go right back here
and I'm going to put in a dash d for detached.
So that's going to start this thing running
and leave it running in the background.
It prints out an identifier
by which you can go and find it.
You don't have to use that identifier.
You can also see it by running the docker ps command.
Now again I've put my format argument on here
to make it look better on this screen.
That's optional to you.
And because I like to see it outputted vertically.
All right, we see we have one container running.
It has this ID, 2b5, which matches that ID.
And it got a nice little name, suspicious_williams.
Now from this terminal over here
I'm going to run docker attach copy suspicious_williams.
There I go, now I have jumped into that container
that I had started over there.
That's most of what there is to docker attach.
If you started a container attached
and you want to jump away from it,
then there's a special key sequence you can type.
If you hit control+p, control+q,
it exits you out of the container by detaching you from it,
but leaves it running
so that you can attach back again
and pick up where you left off.
And again, you can find your containers with docker ps,
and you can attach back to them
with docker attach container_name.
Now let's say you want to run more things in a container.
You started a container, it's got something running,
and you want to add another process to a running container.
This is really good for debugging.
Your container's acting up, you just want to jump in there,
figure out why.
You don't get to add extra fancy stuff with this,
you just start a process in it.
You can't use it to add more ports or volumes
or any of the other stuff that you can do with docker run.
So over here I have my suspicious_williams
running in this terminal.
And I would like to attach this terminal also
to mr suspicious_williams.
Of course the example name will be different
when you run it on your computer.
So I can run docker exec dash ti.
So I'm going to execute a new process in that container.
Suspicious_williams,
and the process I would like to exec is bash
so I get an interactive shell.
All right, now I have another shell in that container.
Make a file called foo.
I see the file called foo right there.
If I go over to my other terminal, there it is.
I really am in the same container.
Now when the original container exits,
which I'll do by pressing control + d over here.
My attached process that I had exed in there with,
that died with the original container.