Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Docker!
You have completed Introduction to Docker!
Preview
The `WORKDIR` line specifies a new default directory within the image's file system. The COPY and ADD instructions allow you to copy files into your image's filesystem.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The WORKDIR instruction specifies
a new default directory
0:00
within the images file system.
0:04
If the directory doesn't exist,
it'll be created automatically.
0:06
Any RUN, COPY or ADD instructions that
follow the WORKDIR instruction in
0:10
the Docker file will be executed
within the specified directory.
0:14
We'll talk about COPY and ADD in moment.
0:18
When the container launches,
the command specified by ENTRYPOINT or
0:21
CMD will be run within
the specified WORKDIR directory.
0:25
We use it here in our Python web app
Docker file to create a /app directory to
0:30
house the app.
0:34
The COPY instructions that follow WORKDIR
will copy files into the /app directory.
0:36
And the web app will run
within /app as well.
0:41
The COPY and ADD instructions allow you to
copy files into your images file system.
0:44
The COPY command is
the simpler of the two.
0:50
And it's also the more commonly used.
0:53
It takes the path of a file or directory
relative to your Docker files path,
0:55
And copies it to your images file system.
1:02
The destination path will be relative
to your WORKDIR, if you specified one.
1:05
So in this Docker file,
1:09
we've set a WORKDIR of /files
under the root directory.
1:11
The first COPY instruction will
copy a file named host_file.text
1:14
from the same directory
as the Docker file and
1:19
copy it to a file named image_file.text
within the /files directory.
1:21
The second COPY instruction
will create an image directory
1:27
within the /files directory,
a path directory within that, and
1:31
then copy host_file.text to a file named
image_file.text within the path directory.
1:35
We can confirm this works by building
an image based on this Docker file with
1:42
docker build.
1:47
We'll use a tag of temp and we'll specify
to run it within the current directory.
1:48
And then we can run our new image
with a command of ls -R /files to
1:54
list the files directory recursively.
1:59
We do that with docker run,
we'll use our tag of temp.
2:02
And here we'll specify the command
we want to run, ls -R /files.
2:07
We'll see our host_file.text file
has been copied to a file named
2:14
image_file.text within
the /files directory.
2:17
We'll also see another image_file.text
file within the files/image/path
2:22
directory.
2:27
If the destination for
the COPY instructions starts with a slash,
2:28
however, it'll be treated
as an absolute path.
2:32
This last line copies host_file.txt to
image_file.txt within the root directory,
2:36
ignoring the WORKDIR setting.
2:41
We can confirm the file is there by
listing the contents of the root directory
2:43
by running docker run temp again
with just ls /, the root directory.
2:47
We'll see the image_file.txt
among the results.
2:55
The ADD does everything that COPY does,
2:58
plus it can also download files from a URL
or decompress files from tar archives.
3:01
The second to last line in this updated
Docker file downloads a robots.txt file
3:07
from google.com.
3:12
And stores it under the WORKDIR directory.
3:14
The last line decompresses
an entire directory tree
3:17
that's stored in .tar.gz file.
3:20
And then adds it to a subdirectory
of the working directory.
3:23
It's generally pretty obvious just from
looking at a Docker file what a COPY
3:27
instruction will do.
3:32
But ADD is significantly more complex and
may behave in surprising ways.
3:33
For this reason, the Docker team
considers it a best practice to use COPY
3:37
instead of ADD, unless you need
the extra functionality ADD offers.
3:42
That's why the Docker file for
3:46
our Python web app uses COPY instead
of ADD to copy the script files over.
3:47
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up