Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The ENTRYPOINT instruction sets an executable to be run each time a container starts. If you're always going to want the container to run the same executable, whether that be Python, MongoDB, Redis, or whatever, you should set it here. The CMD instruction is a list of arguments that get passed to the ENTRYPOINT executable. (Or, if no ENTRYPOINT has been set, the first argument is treated as the executable to be run.)
The entry point instruction
sets an executable to be run
0:00
each time a container starts.
0:04
If you're always going to want
the container to run the same executable,
0:06
whether that Python, MongoDB, Redis or
whatever, you should set it here.
0:10
Just like the run instruction,
both shell and exec forms,
0:15
that is the form with the square brackets,
are supported.
0:19
But the exec form is preferred,
0:23
because the command can be shutdown
more cleanly when the container stops.
0:24
Unlike run, you can have only one
entrypoint instruction per Dockerfile.
0:29
If there are additional arguments that
you always want passed to the command,
0:34
you can add them after the executable.
0:38
Here's the Dockerfile for
the Apache image, again.
0:41
It gives us the entrypoint
instruction to run the Apache server.
0:44
By default, Apache runs in the background,
but we want to run it in the foreground so
0:48
we can simple press Ctrl+C to stop it.
0:52
To do that, we need to pass -D and
0:55
FOREGROUND arguments to
the apache2ctl executable.
0:58
So this entrypoint runs
the Apache2ctl executable, and
1:02
ensures that -D and
FOREGROUND are passed to it as arguments.
1:06
The CMD or command instruction is a list
1:11
of arguments that get passed
to the entrypoint executable.
1:14
Or, if no entrypoint has been set,
1:19
the first argument is treated
as the executable to be run.
1:21
As with entrypoint, you can only have
one command instruction per Dockerfile.
1:25
So this command instruction combined
with the entrypoint instruction,
1:30
causes a command of python3 app.py to
be rung when the container starts.
1:34
The user can override the value of the
command instruction by passing additional
1:41
arguments when they call docker run.
1:45
If we call docker run with the same
command line we've been using to run our
1:48
Python web app all along, We'll see
the usual message in our web browser.
1:52
But if we stop the container, And
2:02
then call docker run again with
an additional alternate.py argument.
2:05
Our default command value of
app.py will be overridden, and
2:12
the container will end up running
a command of python3 alternate.py.
2:16
We've set up alternate.py to contain
a web app that's just like app.py but
2:21
prints a different message.
2:27
And if we visit local host
port 8080 in our browser,
2:29
we'll see it's the alternate
app that's running.
2:32
Like run an entrypoint, the command
instruction also has a shell form.
2:36
Whatever command you type in shell form
will be executed in the /bin/sh shell.
2:41
Here's a minimal Dockerfile
that just uses the shell to
2:48
echo a message to the terminal.
2:51
If we build it with docker build -t echo,
2:54
In the current directory, and
run it with docker run echo,
3:00
The container will simply
print that message and exit.
3:07
You need to sign up for Treehouse in order to download course files.
Sign up