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
    
      
  Let's dive in a little deeper and get a better understanding of how you can use Docker. We'll be going into much more detail in the remaining stages, but for now, let's try to get a broad understanding.
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
                      Let's dive in a little deeper
to get a better understanding of
                      0:00
                    
                    
                      what Docker is useful for.
                      0:03
                    
                    
                      We'll be going into much more
detail in the remaining stages.
                      0:05
                    
                    
                      But for now,
let's try to get a broad overview.
                      0:08
                    
                    
                      Developers using Docker don't have
to worry about installing and
                      0:11
                    
                    
                      configuring complex supporting
software like databases.
                      0:14
                    
                    
                      Or worry about which version of
a language an app is built on.
                      0:17
                    
                    
                      When a developer Dockerizes an app,
                      0:21
                    
                    
                      all the complexity of building the app is
pushed into what Docker calls containers.
                      0:23
                    
                    
                      You can think of these like
shipping containers software.
                      0:28
                    
                    
                      Containers are easily built, run, and
                      0:31
                    
                    
                      shared by any developer with
access to the Docker file.
                      0:33
                    
                    
                      Docker's own website
describes it as the world's
                      0:37
                    
                    
                      leading software container platform.
                      0:39
                    
                    
                      Docker is used by developers
to ensure their apps work on
                      0:42
                    
                    
                      every machine they're deployed to.
                      0:45
                    
                    
                      And operations staff use Docker to
better scale systems in production.
                      0:46
                    
                    
                      Enterprise companies use Docker to build
agile software delivery pipelines, so
                      0:51
                    
                    
                      they can ship new features quickly,
with better security.
                      0:55
                    
                    
                      Docker lets enterprises deploy to
both Linux and Windows Server easily.
                      0:58
                    
                    
                      Employees joining a new
project no longer have to wait
                      1:03
                    
                    
                      hours while the supporting
software installs.
                      1:06
                    
                    
                      You don't have to carefully explain
how to setup various services.
                      1:08
                    
                    
                      Docker files abstract away the
installation of dependencies, allowing you
                      1:12
                    
                    
                      to easily package the app for running and
test or production environments.
                      1:17
                    
                    
                      If you've worked with
virtual machines before,
                      1:21
                    
                    
                      containers may seem similar to those,
and they are somewhat similar.
                      1:24
                    
                    
                      Both allow you to install
a set of apps and
                      1:28
                    
                    
                      the services they depend on without mixing
them what the software on the host OS.
                      1:30
                    
                    
                      But containers don't emulate
a virtual CPU, memory, and
                      1:36
                    
                    
                      other hardware like virtual machines do.
                      1:39
                    
                    
                      They run directly on the host
computer's hardware,
                      1:41
                    
                    
                      making them more efficient
in many situations.
                      1:44
                    
                    
                      To run an app in a Docker container,
you first need to write a Docker file.
                      1:47
                    
                    
                      The syntax for these files is simple, and
there are thousands of existing examples
                      1:51
                    
                    
                      for you to pull from
when packaging your apps.
                      1:55
                    
                    
                      Once a Docker file is made,
you can use the Docker commandline
                      1:58
                    
                    
                      interface to build an image
from your Docker file.
                      2:02
                    
                    
                      An image is essentially a binary file
that contains the app defined by
                      2:04
                    
                    
                      the Docker file.
                      2:08
                    
                    
                      Then you can share the image with
others who can run it as a container.
                      2:10
                    
                    
                      Suppose we have this Python web app.
                      2:14
                    
                    
                      It's a really simple app
consisting of a single file.
                      2:16
                    
                    
                      All it does is listen for
browser requests on port 8080 and
                      2:19
                    
                    
                      respond with hello from Python.
                      2:24
                    
                    
                      But even something this simple
introduces a dependency,
                      2:26
                    
                    
                      we have to have Python 3
installed in order to run it.
                      2:30
                    
                    
                      And it looks like this machine
only has Python 2 installed.
                      2:34
                    
                    
                      So we get an error when we try to launch
this app.py program, python app.py.
                      2:38
                    
                    
                      And you can see here the import
error No Module named http.server.
                      2:43
                    
                    
                      So let's try using Docker
to create an image that
                      2:49
                    
                    
                      bundles the gap together with
the correct version of Python.
                      2:51
                    
                    
                      We'll need a file named Docker file
with a capital D and no extension.
                      2:55
                    
                    
                      And we'll put this together in the same
directory that contains our app.
                      3:00
                    
                    
                      This Docker file will create another new
image based on an Ubuntu Linux image.
                      3:04
                    
                    
                      It'll install Python 3 on top of it.
                      3:09
                    
                    
                      It'll copy the app,py file,
plus another file that we'll use later
                      3:12
                    
                    
                      from the current directory
on the host into the image.
                      3:16
                    
                    
                      It'll set app.py to run via Python 3
whenever a container that's based on
                      3:20
                    
                    
                      the image starts.
                      3:24
                    
                    
                      And it'll ensure that outside
apps can connect to the container
                      3:26
                    
                    
                      on port 8080, so
that they can communicate with our app.
                      3:30
                    
                    
                      Now let's actually build the image.
                      3:35
                    
                    
                      We'll run the docker command
with the build subcommand, and
                      3:37
                    
                    
                      use the -t flag to tag the image
with the name sample-web-app.
                      3:41
                    
                    
                      Lastly, we'll tell it to look for
a Docker file in the current directory.
                      3:50
                    
                    
                      As before, Docker build will go through
the instructions in our Docker file
                      3:54
                    
                    
                      one by one.
                      3:58
                    
                    
                      Creating an image based on Ubuntu,
installing Python,
                      3:59
                    
                    
                      copying our app into the image and
exposing port 8080.
                      4:03
                    
                    
                      We can confirm that our image was
created successfully with the docker
                      4:08
                    
                    
                      images command.
                      4:12
                    
                    
                      We'll see an image with our sample
web app tag in the resulting list.
                      4:15
                    
                    
                      Now let's create a container
based on the image and
                      4:21
                    
                    
                      try connecting to our Python server.
                      4:23
                    
                    
                      We'll use the Docker run command,
we'll publish our exposed port 8080 as
                      4:25
                    
                    
                      port 8080 on the host with -p 8080:8080.
                      4:31
                    
                    
                      And select the image to use by
providing our tag of sample-web-app.
                      4:39
                    
                    
                      Our container will start, and
                      4:47
                    
                    
                      we'll run our Python app just like
we specified in the Docker file.
                      4:49
                    
                    
                      If we switch to our web browser and
direct it to localhost
                      4:53
                    
                    
                      port 8080, it will connect to port
8080 on our host, be passed through to
                      4:59
                    
                    
                      port 8080 on the container, and our Python
app will respond with Hello from Python.
                      5:06
                    
                    
                      Because the container is still
attached to our terminal,
                      5:11
                    
                    
                      we can shut it down by going back to
the terminal and pressing Ctrl+C.
                      5:14
                    
                    
                      That will stop the main Python process and
the container will then shut down.
                      5:18
                    
                    
                      If we go back to our browser and
Reload, we'll see that we no longer get
                      5:23
                    
                    
                      a response because on the container
that was responding before has shutdown.
                      5:28
                    
                    
                      Docker is useful for building and
deploying single apps or
                      5:32
                    
                    
                      services, but it's even better when you're
building complex distributed systems.
                      5:35
                    
                    
                      There are existing tools
such as Docker Compose,
                      5:40
                    
                    
                      which you can think of is a Docker
file for multiple Docker containers.
                      5:43
                    
                    
                      And Docker Swarm, which allows you to
build, deploy, and monitor multiple
                      5:47
                    
                    
                      Docker containers at once, either as
a single service or as a set of services.
                      5:51
                    
                    
                      Docker also has a rich networking API.
                      5:56
                    
                    
                      And connecting containers
to the outside world or
                      5:59
                    
                    
                      other Docker containers is
straightforward and easy.
                      6:02
                    
                    
                      Hopefully, now you have a better
understanding of exactly what Docker is.
                      6:05
                    
                    
                      Up next, we'll discuss why you should be
using Docker right now for your projects.
                      6:09
                    
              
        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