The intro to Docker I wish I had when I started
Summary
TLDRThis video introduces Docker, explaining the fundamental differences between virtualization and containerization. The speaker discusses how Docker helps manage lightweight, isolated environments called containers, which are commonly used in web development, continuous integration, and cloud deployment. Key concepts such as Docker files, images, and containers are explained, showing how they work together to streamline development and deployment. The video provides a step-by-step guide on setting up and using Docker, emphasizing its importance in modern web development and encouraging further learning on advanced topics.
Takeaways
- π¦ Docker is a tool that helps manage the life cycle of containers, used in various web development processes like continuous integration and deployment.
- π Containers are lightweight, isolated environments where processes run, and are used in many common web development workflows, even if developers aren't aware of it.
- π» Virtualization involves creating entire virtual machines with dedicated hardware resources, whereas containerization shares the host's OS but isolates processes.
- π₯οΈ Docker makes containerization easier by automating the management of containers, so developers don't need to handle process isolation manually.
- π Docker images are templates used to create containers, and Docker Hub is a repository where developers can pull pre-made images like 'hello-world' or 'Postgres'.
- π A Dockerfile contains instructions to build a Docker image, such as specifying a base image and commands to run inside the image (e.g., installing software).
- π§ Docker containers are created from images and run isolated processes, making them ideal for replicating environments across different machines.
- π Docker images are immutable; once built, they donβt change. If modifications are needed, a new image version is created using a modified Dockerfile.
- π Docker Compose, volume mounting, and port mapping are additional features that can enhance the use of Docker in complex projects.
- π Using Docker in development allows for consistent, reproducible environments, reducing issues with dependencies across different setups.
Q & A
What is Docker and why is it important for developers to learn?
-Docker is a tool that manages the lifecycle of containers, allowing developers to create reproducible, lightweight environments for running processes. It's important because containers are used in many aspects of modern development, including continuous integration, continuous deployment, and cloud deployments.
What is the main difference between virtualization and containerization?
-Virtualization involves creating virtual machines (VMs) with their own operating system, while containerization uses the host machine's operating system but isolates processes in containers. VMs require a hypervisor to manage them, whereas containers are more lightweight and efficient.
What role does a hypervisor play in virtualization?
-A hypervisor manages the lifecycle of virtual machines. It provisions resources, starts, stops, and deletes virtual machines. Common hypervisors include VMware and VirtualBox.
What is a Docker container?
-A Docker container is a lightweight, isolated environment that shares the host's operating system but runs processes independently. Containers cannot access resources outside their environment unless explicitly allowed.
How does Docker manage container lifecycles?
-Docker uses commands to build, run, and manage containers. It simplifies the manual process of setting up containers and managing their resources, allowing developers to easily create isolated environments.
What is a Docker image, and how is it related to containers?
-A Docker image is a snapshot of a file system that includes everything needed to run a container, such as code, dependencies, and libraries. Containers are instances of these images, running the processes defined in the Docker image.
What is the purpose of a Dockerfile?
-A Dockerfile is a set of instructions that tells Docker how to build an image. It specifies the base image, what dependencies to install, what files to copy, and what commands to run inside the image.
What does the 'docker build' command do?
-The 'docker build' command reads the instructions from a Dockerfile and creates a Docker image. You can also specify a tag (name) for the image, allowing you to keep track of different versions.
What happens when you run the 'docker run' command?
-The 'docker run' command creates a container from a Docker image and executes the default command specified in the Dockerfile. It runs the container in isolation, based on the image's environment and code.
Can Docker images be modified after they are built?
-No, Docker images are immutable, meaning they cannot be changed after being built. If you want to make changes, you need to modify the Dockerfile and build a new image with a different tag.
Outlines
π³ Introduction to Docker and Containerization
The speaker begins by sharing their personal journey with Docker, acknowledging that despite being a Ruby on Rails developer, they didn't need to use Docker extensively. They recount their initial resistance to Docker, humorously stating their lack of knowledge about it. However, they realize that Docker is a fundamental technology that they should have understood earlier. The paragraph sets the stage for an educational video that will explain virtualization and containerization, clarify the differences between them, and introduce Docker files, images, and containers. The speaker emphasizes the importance of Docker in web development, noting its prevalence in continuous integration/continuous deployment (CI/CD) pipelines and cloud deployments. The paragraph concludes with a teaser about getting hands-on with Docker in the video.
π§ Understanding Virtualization and Docker Installation
This paragraph delves into the concept of virtualization, explaining it as the process of creating a virtual machine (VM) on a host machine, complete with its own operating system. The speaker mentions that VMs are commonly used in cloud services like AWS EC2 instances. They introduce the term 'hypervisor,' which is a program that manages VMs, with examples like VMware and VirtualBox. The paragraph then transitions into containerization, contrasting it with virtualization. Containerization is described as a method to run processes in isolated environments on a host PC without the overhead of a full VM. The speaker clarifies that while both technologies are similar, they serve different purposes. Docker is then introduced as a tool that manages container lifecycles. The paragraph concludes with a practical demonstration of installing Docker on the speaker's Arch Linux system using the Pac-Man package manager.
π Docker File and Image Creation
The speaker explains the process of Docker image creation using a Dockerfile. They provide an example of a Dockerfile that uses the 'FROM' instruction to base the image on the latest Ubuntu image. The Dockerfile is used to update the package index, install packages, copy local directories into the image, and define default commands for the container. The paragraph walks through the Dockerfile step by step, illustrating how each line contributes to building the image. The speaker emphasizes that Docker images are immutable, meaning that any changes require the creation of a new image. They also discuss the process of running a container from an image using the 'docker run' command and how the container's default command is specified in the Dockerfile.
π Practical Docker Usage and Image Versioning
In this paragraph, the speaker demonstrates a practical example of using Docker by creating a container that runs a script to display ASCII art. They walk through the contents of a Dockerfile and a script file, explaining how the Dockerfile installs necessary packages, copies the script into the image, and sets the default command to run the script. The speaker then shows how to build the Docker image using the 'docker build' command and run a container from the image using 'docker run'. They also discuss the immutability of Docker images and how to create a new image with changes by editing the Dockerfile and building a new version with a different tag. The paragraph concludes with the speaker running both the original and the updated containers to show how Docker handles multiple versions of the same image.
Mindmap
Keywords
π‘Docker
π‘Container
π‘Virtualization
π‘Hypervisor
π‘Docker File
π‘Image
π‘Docker Hub
π‘Continuous Integration (CI)
π‘Continuous Deployment (CD)
π‘Immutable
Highlights
Docker was often underutilized in my career as a Ruby on Rails developer, with the common joke 'Docker? Barely even know her!'
Containers are pivotal for web developers because they create reproducible, lightweight environments, commonly used in CI/CD pipelines and cloud deployments.
Virtualization involves running full operating systems on virtual machines with hardware resource allocation managed by a hypervisor.
In contrast, containerization isolates processes within a host machine using tools like `chroot` and `rlimit` for resource limits and environment boundaries.
Docker simplifies containerization by managing container life cycles, from creation to execution, through its CLI and infrastructure.
A Docker image is a blueprint for containers, often pulled from Docker Hub, which serves as a repository for pre-built images.
A Docker container is the running instance of an image, isolated from the rest of the system, executing specified processes.
Dockerfiles are scripts that define the steps to build an image, including instructions like copying files and setting up the environment.
Images are immutable snapshots of the file system and environment, and containers run them based on commands specified in the Dockerfile.
Docker Hub is a vast repository where developers can search for pre-built images, such as Postgres, reducing the need to build from scratch.
In a basic example, running `docker run hello-world` pulls the image from Docker Hub and verifies the installation by running a test container.
Using `docker build`, developers can create images from Dockerfiles and tag them to create different versions without overwriting the previous image.
By building and running a container that prints random ASCII messages using a bash script, the process showcases Dockerβs ability to automate tasks within isolated environments.
Each change to the code or environment requires building a new Docker image, demonstrating the immutability of images in Docker.
Multiple images with different tags can coexist, allowing developers to run different container versions, preserving previous states for testing or deployment.
Docker serves as an essential tool for modern development, enabling isolated, reproducible environments critical for CI/CD, deployment, and scalable applications.
Transcripts
for the longest time Docker was a tool
that I used sparingly throughout my
whole career I mean I'm a Ruby on Rails
Dev I don't have to know Docker I can
just run all my services locally on my
machine right I can even remember
throughout my career if anyone ever
mentioned that we needed to use Docker
to run something I would always say
Docker barely even know her and as
hilarious as that joke is I was missing
out on a core foundational piece of
technology that I should have known
about all along in this video we're
going to cover virtualization and
containerization and what the difference
is between the two things because for me
this was always messed up in my head
we're also going to cover Docker files
images Docker containers and how they
all fit into the grand scheme of Docker
this video is going to be a gentle
introduction to Docker and we're just
going to be scratching the surface so
stick around this is going to be a fun
[Music]
one so why is Docker an important thing
to learn in the first place well you see
you probably use containers and
containerization Technologies every
single day of your career if you're a
web developer even if you don't realize
it containers are a way to build
reproducible lightweight environments
for processes to run and we use them
everywhere in continuous integration and
continuous deployment pipelines like on
GitHub actions that you probably use all
the time and we also use it whenever
you're deploying to a server if you
deploy something to the cloud chances
are you're interacting with container
technology somewhere along the way so I
think it's a pivotal thing that you need
to learn in your web development career
but wait what the hell is a container
anyways in order to talk about
containers we should talk about
virtualization now bear with me for a
minute because these two things are very
closely related and I think it's
important to understand the distinctions
between the two technologies so let's
talk about virtualization so let's draw
up how virtualization Works in a typical
sense and we can talk through it now
typically when it comes to
virtualization you start off with a host
machine this could be your host now the
host could be anything it could be your
local PC it could be a server up in the
cloud server in a data center somewhere
whatever it is it's a piece of Hardware
now in this piece of Hardware you have
different things that control how this
Hardware works you have things like your
CPU you also have things like memory and
you have your hard drive we'll call this
IO now when it comes of virtualization
what happens is we take little pieces of
each of these pieces of hardware and
separate them out into a separate
machine this is a virtual machine and
then we take these pieces of hardware
and in this virtual machine we actually
run a full entire operating system now
this technique is commonly used in the
cloud if you're deploying something to
AWS or an ec2 instance typically what
you're doing is you're spinning up a new
virtual machine that you can then deploy
your code onto now virtual machines have
a special type of program that can run
and manage the life cycle of these
machines this program is called a
hypervisor and the hypervisor is in
charge of virtual machines it manages
the life cycle it starts them up it
stops them it creates them it deletes
them it Provisions resources for for
them that is what the hypervisor does
now a common hypervisor that you would
be aware of is VMware or virtual box
these are the programs that control the
virtual machines now virtualization is
similar but it differs from
containerization which is the thing that
Docker is kind of based around so let's
talk about containerization in a
container setup what you would do is you
would have a host PC much like the
virtualization setup that we set up
before now let's say on this host PC we
want to run a set of processes but we
want these processes to run in isolation
we don't want them to touch anything
else now we can achieve that using some
techniques right let's say we want these
processes to run on this machine
processes what we do is we can use some
techniques like the CH root command
which will create a new route for a
process so it can only live inside that
root and it can't touch anything outside
of that like any of the other users
directories or things like that that are
already on the system we could also use
a kernel feature like the r limit
feature which will limit the amount of
resources these processes take up these
techniques amongst other things will en
Compass what is containerization now
with containerization you could do all
this manually yourself but it's really
difficult and pretty tricky so there are
programs that help manage the life cycle
of your containers this is where Docker
comes into play Docker is a program that
manages the life cycles of containers
edit them run them and interact with
them so to sum up containerization is
the ability to create a lightweight
environment where processes can run on a
host operating system they share all the
same things in that operating system
system but they cannot touch anything
outside of their little bounded box Okay
so we've talked enough about
virtualization and containerization
let's see containerization through
Docker in work let's get our hands dirty
so now let's install Docker again this
is this portion of our graph right here
this is the management layer that will
manage the life cycles of all of our
containers that we want to create that's
what Docker does for us now to install
Docker there are examples of how to
install it on the docker website for me
I use Arch Linux by the way so I going
to use Pac-Man to install Docker and
it's as simple as that Docker is now
installed so how do we know that Docker
is even running correctly on our system
well Docker gives us a helpful command
that we can run to sort of give us our
first taste of what Docker will do for
us we can do Docker space run space
hello hyphen world let's see what
happens we're going to break down this
command in a little bit but let's just
see what happens
now okay a lot of stuff just happened
let's go through this line by line and
let's see what Docker is telling telling
us that it did now to start off with
Docker was unable to find the image
hello world latest now this is the name
of our image and this is the tag of our
image now in Docker speak an image is
basically the thing we run our
containers from I'll explain it again in
a second and latest is the tag of that
image by default doer tags its images
with the latest tag so doer was unable
to find the image hello world latest
locally so it pulls it from a repository
Docker will pull images that are already
known from dockerhub we can actually
check out dockerhub by going to hub.
do.com and this is where you can see all
of the images that Docker already has
pre-built in this platform here Docker
hubs this is where you can explore if I
wanted to look for like let's say a
postgress image here it is right here I
can use the postgress image from Docker
Hub I don't have to build one myself so
Docker Hub is very helpful and that's
what it does here it pulls the hello
world image from Docker Hub and as you
can see here it says status downloaded
newer image for hello World latest
awesome and then it says hello from
Docker this is the actual image running
a container I'll explain this in one
second here this message shows that your
installation appears to be working
correctly to generate this message
Docker took the following steps the
client contacted the docker Damon the
docker Damon pulled the hello world
image from Docker Hub that's what we
were just talking about and then the
Damon created a new container for us
from the image okay and now the docker
Damon streamed that output to the docker
client which sent it to your terminal
okay let's unpack this a little bit you
might be asking yourself what the hell
is an image and how does Docker know how
to build these things and run containers
from images what is all this stuff now
before we talk about how to build images
and how to then run containers from the
images we have to talk about something
called a Docker file this is an example
doer file and it's pretty contrived but
essentially what you would have with any
project anything you want to build an
image out of is you would have a
directory structure that looks something
like this in this contrived example we
have a directory that can contains a
Docker file and within the docker file
we have a coffee recipe folder let's
just imagine that this is a coffee
recipe application of some sort in this
coffee recipe folder we have two scripts
prepare Beans and Brew Coffee okay so
now let's talk about what this Docker
file is doing you can see on this very
first line we have from Ubuntu latest
now you might remember this terminology
from not too long ago that means that we
want to use the Ubuntu image at the
latest tag this is the terminology for
an image and the tag for that image
Ubuntu latest okay great the next line
says we want to run appt get update and
appt get install some contrived package
what is this this line tells Docker that
we want to run something on the image
that was added above so we're running
apt get update and apt get install on
our auntu latest image so this runs
command on the image next what we want
to do is copy the coffee recipe
directory from our local file system
into this image so what we want to do
here is basically just copy things from
our local directory again you can
remember our directory structure looks
like this we have the coffee recipe
directory and under this we have a
couple of scripts so we want to copy the
coffee recipe directory into this image
next we want to run in our image the
script prepare beans. sh so this will
run that script now remember since we
copied the directory into our image we
will have this available to us because
prepared beans is right here oh look I
misspelled it how fun and then next we
have this line that says command Now
command is the default command that this
container is going to run it's this can
be overwritten in the CLI but by default
we're going to run the Brew Coffee
command this is the default command for
our Docker image so when the darker
container runs it's going to run this
default command so now let's zoom out a
little bit here what we are going to do
is we are going to take in this
contrived example this Docker file and
from this Docker file we are going to
build a Docker image now this image we
could name anything but let's just call
it coffee right this image by default
will create will be called coffee at the
latest tag you can give an image
whatever tag you want but again by
default Docker gives a tag of latest to
every image now this image is almost
like the file system for the container
to run and it's immutable you can only
build one image you don't change your
images what you do if you want to change
anything is you change your Docker file
and then build a new version of your
image for containers to run now from our
image what we want to do is we want to
call Docker run and that will then run
our container and our container as we
said before will call the Run command
that we specified in our darker file
which was Brew coffee.
sh and again to actually build the image
we want to run Docker build and there
are certain Flags we can pass to build
like I said to change the tag of the
image from the default of latest you can
also name the image whatever you want
but this is generally the process for
Docker we want to create a Docker file
in some repository or in our directory
we want to then use this Docker file as
the instructions to build a new image
for Docker then we can run this image
and Docker will spin up a new container
on our system that is not able to touch
anything else within our system and it
will run whatever code we want it to run
using the command flag so that in a
nutshell is how Docker sets up Docker
files images and containers okay so now
we have all of our Core Concepts in
place we understand the relationship
between a Docker file how a Docker file
is the instructions to build a Docker
image and then how a Docker image is
used to then run a Docker container all
underneath the umbrella of the docker
CLI so let's get our hands dirty and
actually try out a real world example
this is going to be a simple example
it's a contrived example yet again but
we're actually going to run a real
container on our system to get a feel
for how the docker CLI works now let's
just say I have a directory called
Docker example and in this directory I
have two files a Docker file and a print
message. sh file let's take a look at
them let's start first with our print
message. sh we can see that it is a bash
script and then it has a variable that
has a list of phrases these phrases are
then randomly selected and we print them
out to the terminal using a a program
called figlet now what if I don't have
figlet installed on my local machine
well that's okay that's why we have this
Docker file so in our Docker file what
we want to do as we've seen before we
want to use auntu latest that means
we're using the latest version of the
auntu image from Docker probably from
Docker Hub then on that image we want to
run apt get update and apt get install
figlet and wget we're going to use WG
get in the next line where we run W get
and we W get some fonts that we want to
then install on our system then we want
to copy our local print message script
into the Container for print m.sh this
is what we're actually going to run then
we want to chamod plus X print m.sh that
just makes this script executable inside
of the container and our final command
is to just run print message. SSH it's a
pretty simple Docker file but this will
give us an understanding of how to use
these things in real time so now that
we're within this directory that has a
doer file we can call from within the
directory Docker build now in Docker
build we could tag that by default it
will tag it with the latest release and
we can call this asky and we want to
make sure we build everything in this
current directory this will build our
very first image let's see what this
command does okay this was a lot of
stuff but let's just go through it
really quickly and see what we did here
we can see that it sent the build
context to the docker Damon and then we
did the steps that were in the docker
file we can see the docker file working
for us step one out of six from auntu
latest now it pulls from Library auntu
this is probably pulling from dockerhub
we then run appt get update and app get
install these couple of programs onto
our abtu image very good we can see that
that is running and that's what all this
output is and then in our next step step
three we want to run W get for these
files now these files were then taken
and saved into this image very good and
now in step four out of six we copy our
local print message script into the
image then we run chamod plus X which
will make it executable and at the very
end we want to make sure our default
command is just by running this print
message script cool okay so now we have
our image built how do we run the
container based on this image well what
we can do is we can check what images we
have currently built on our system with
the docker images command we hit enter
and we can see that amongst a couple
others like Ubuntu and hello world we
have our asky image tagged at the latest
tag with an image ID created about a
minute ago and that's what we have right
here we built our image I'm going to
make my font smaller here because we're
printing out asky text and it's going to
be pretty large on the screen you'll see
what I mean in a second but basically
what we want to do is now that we have
our image built we can run Docker run
and then type the name of the image
which is asky we could also optionally
add the tag which would be latest and
I'll just add it right here if we do
this and enter Then This command we can
see that it actually runs the container
and it runs the script that prints out
asky art to our screen isn't that
awesome so basically what we've done
here is we have now a Docker file that
installs STS things into an image we
have an image that has all these
programs on it like figlet w get and
everything else and then it has a script
inside of that image as well now the
container runs and the container's
default command is to then print this
stuff out to the terminal and that's
what it is we just did that awesome now
we can keep running this command we can
keep running this Docker container and
it will just randomly select another
thing apparently it was the same for a
bunch of those but it will select random
sayings and then print them out in an
asky text to us very cool but now let's
say we want to modify this script and we
want to print out different things what
do we do now to update this image well
images are immutable what you're going
to have to do is edit your Docker file
and then create a new image probably
with a different tag let's get into that
so we see we have our Docker file here
and we have our print message. sh let's
just say we want to change some of these
phrases this is
fun I love
Docker cool okay so now we've changed
the script that actually prints the
messages out what we're going to want to
do is build a new image from this Docker
file because this new image is going to
contain different things okay so let's
do Docker
build- T and what we want to do is we
want to name this asky with a colon the
colon is going to denote the tag and I'm
just going to call it different we can
call whatever we want and we want to
build it from our local directory now
let's hit enter so we can see here we
get very similar output to what we had
before we probably get less output
because we already have the ab image
installed and we already have these
programs installed in the abutu image
that Docker is using to build this image
but we can see down below we've
successfully built this new image which
has a new ID and there's a new tag
called asky different very cool all
right so now let's check Docker images
to see what this looks like okay great
we can see that we have as's latest
build which isn't currently the latest
because we tagged it with something
different but that's okay for now and we
have our new asky different tag so this
tag is a snapshot of our Docker file
that was built into an image so now
let's run our new image in a container
we can call Docker run asky and we want
to add the tag of different let's see
what happens you can see it pushes out
the different text that we did now we
have different sayings that we're
putting out there like I love you or I
love Docker let's make this a little bit
smaller so we can see it a little bit
better but I love Docker this is fun I
love you these are all the different
messages that we put in our print
message Dosh now the cool thing is we
still have the previous version of this
image like I said they're immutable you
don't change the images you just create
new ones so we can also say Docker run
asky latest and that was the previous
one that we built which has all of these
Star Wars things in there let's make
this smaller so we can run both of our
containers based on the images that
we've built the images are mutable you
don't delete them you just create new
versions of them very cool so what have
we learned here well I think we've
learned a lot we've learned about the
difference between virtualization and
containerization and we've also learned
about Docker as a whole and how Docker
files images and containers all relate
to one another but this is just the
surface of this surface that we're
scratching here if you want to learn
more about Docker like Docker compose
mounting Docker volumes or even doing
things like Port mapping and darker then
leave a comment down below of what you
would like to learn next and hey thanks
nerds
[Music]
5.0 / 5 (0 votes)