Skip to content

Commit 993f61a

Browse files
authored
Merge pull request #55 from tecladocode/jose/improve-docker-section-recording
2 parents 1d7893a + 5d90d05 commit 993f61a

File tree

5 files changed

+111
-10
lines changed

5 files changed

+111
-10
lines changed
Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,88 @@
11
# What is a Docker container?
22

3-
I'm sure you have heard of the term "Virtual Machine". A Virtual Machine is an emulation of an Operating System.
3+
I'm sure you have heard of the term "Virtual Machine". A virtual machine is an emulation of an Operating System. For example, if you run a Windows virtual machine on your MacOS computer, it will run a whole copy of Windows so you can run Windows programs.
44

5-
This means that you can run a Windows Virtual Machine on a computer running Mac OS. When you do that, you can have the Windows Operating System running inside an app in your Mac.
5+
This diagram shows what happens in that case:
6+
7+
![Virtual Machine Diagram stack](./assets/vm.drawio.png)
68

79
When you run a Virtual Machine, you can configure what hardware it has access to (e.g. 50% of the host's RAM, 2 CPU cores, etc).
810

911
Docker containers are a bit different because they don't emulate an Operating System. They use the Operating System kernel of your computer, and run as a process within the host.
1012

11-
Containers have their own storage and networking, but because they don't have to emulate the operating system and everything that entails, they are much more lightweight and efficient. Another benefit of containers is that their start-up time is very fast.
13+
Containers have their own storage and networking, but because they don't have to emulate the operating system and everything that entails, they are much more lightweight.
14+
15+
This diagram shows how Linux containers run in a Linux host:
16+
17+
![Docker Diagram stack](./assets/docker-linux.drawio.png)
18+
19+
Looks similar, but the `docker -> container` section is much more efficient than running a VM because it **uses the host's kernel** instead of running its own.
20+
21+
## What is a kernel? 🍿
22+
23+
An Operating System is made up of two main parts:
24+
25+
- The **kernel**
26+
- Files and programs that come with the operating system
1227

13-
There are pros and cons to both. For example, you can't run a Windows Docker container if you are using Mac OS in your machine. You'd have to run a Windows Virtual Machine, and then run Docker containers in that, which isn't very efficient!
28+
The Linux kernel, for example, is used by all Linux Operating Systems (like Ubuntu, Fedora, Debian, etc.).
1429

1530
:::caution
16-
Because Docker containers use your OS kernel, you can run Linux images in a Mac OS container.
31+
Since containers use the host's kernel, you can't run a Windows Docker container natively in a MacOS host. Similarly, you can't run a Linux container natively on Windows or MacOS hosts.
32+
:::
33+
34+
## How to run Linux containers on Windows or MacOS?
35+
36+
When you use Docker Desktop (which I'll show you in the next lecture), it runs a Linux Virtual Machine for you, which then is used to run your Linux containers.
37+
38+
But aren't you then doing this?
39+
40+
```
41+
hardware -> macos -> hypervisor -> linux vm -> docker -> container -> container program
42+
```
43+
44+
And isn't that much less efficient than just running the program in a Linux virtual machine?
45+
46+
Yes. Running Linux containers on MacOS or Windows is "worse" than just running the programs in a Linux VM. However, **99% of the time, you will be running Linux containers in a Linux host, which is much more efficient**.
1747

18-
However, new Mac computers use a different CPU architecture (ARM), and this can pose problems in some cases. More on this later on!
48+
:::tip Why do we always run Linux containers in a Linux host?
49+
When you want to deploy your applications to share them with your users, you will almost always be running your app in a Linux server (provided by a _deployment company_, more on that later). There are a few reasons for this. Among them, Linux is free!
1950
:::
2051

52+
## Why are containers more efficient than VMs?
53+
54+
From now on let's assume we are running native Linux containers in a Linux host, as that is by far the most common thing to do!
55+
56+
When you run a VM, it runs the entire operating system. However, when you run a container it uses part of the host's Operating System (called the kernel). Since the kernel is already running anyway, there is much less work for Docker to do.
57+
58+
As a result, containers start up faster, use fewer resources, and need much less hard disk space to run.
59+
60+
## Can you run an Ubuntu image when the host is Linux but not Ubuntu?
61+
62+
Since the Linux kernel is the same between distributions, and since Docker containers only use the host's kernel, it doesn't matter which distribution you are running as a host. You can run containers of any distribution with any other distribution as a host.
63+
64+
## How many containers can you run at once?
65+
66+
Each container uses layers to specify what files and programs they need. For example, if you run two containers which both use the same version of Python, you'll actually only need to store that Python executable once. Docker will take care of sharing the data between containers.
67+
68+
This is why you can run many hundreds of containers in a single host, because there is less duplication of files they use compared to virtual machines.
69+
2170
## What does a Docker container run?
2271

23-
If you want to run your Flask app in a Docker container, you need to get (or create) a Docker image that has all the dependencies your Flask app uses:
72+
If you want to run your Flask app in a Docker container, you need to get (or create) a Docker image that has all the dependencies your Flask app uses, except from the OS kernel:
2473

2574
- Python
2675
- Dependencies from `requirements.txt`
27-
- Possibly nginx or gunicorn (more on this when we talk about deployment)
76+
- Possibly `nginx` or `gunicorn` (more on this when we talk about deployment)
77+
78+
:::info Aren't there more dependencies?
79+
The keen-eyed among you may be thinking: if all you have is the kernel and nothing else, aren't there more dependencies?
80+
81+
For example, Python _needs_ the C programming language to run. So shouldn't we need C in our container also?
82+
83+
Yes!
84+
85+
When we build our Docker image, we will be building it _on top of_ other, pre-built, existing images. Those images come with the lower-level requirements such as compilers, the C language, and most utilities and programs we need.
86+
:::
2887

2988
Let's take a look at Docker images in the next lecture.
Loading
Loading
Binary file not shown.

docs/docs/04_docker_intro/02_what_is_docker_image/README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ A Docker image is a snapshot of source code, libraries, dependencies, tools, and
44

55
There are many pre-built images that you can use. For example, some come with Ubuntu (a Linux distribution). Others come with Ubuntu and Python already installed. You can also make your own images that already have Flask installed (as well as other dependencies your app needs).
66

7-
Creating a Docker image is straightforward (when you know how!). I'll guide you through how to do this in the next lecture, but bear with me for a second:
7+
:::info Comes with Ubuntu?
8+
In the last lecture I mentioned that Docker containers use the host OS kernel, so why does the container need Ubuntu?
9+
10+
Remember that operating systems are kernel + programs/libraries. Although the container uses the host kernel, we may still need a lot of programs/libraries that Ubuntu ships with. An example might be a C language compiler!
11+
:::
12+
13+
This is how you define a Docker image. I'll guide you through how to do this in the next lecture, but bear with me for a second:
814

915
```dockerfile
1016
FROM python:3.10
@@ -17,9 +23,45 @@ CMD ["flask", "run", "--host", "0.0.0.0"]
1723

1824
This is a `Dockerfile`, a definition of how to create a Docker image. Once you have this file, you can ask Docker to create the Docker image. Then, after creating the Docker image, you can ask Docker to run it as a container.
1925

26+
```
27+
Dockerfile ---build--> docker image ---run--> docker container
28+
```
29+
2030
In this `Dockerfile` you can see the first line: `FROM python:3.10`. This tells Docker to first download the `python:3.10` image (an image someone else already created), and once that image is created, run the following commands.
2131

22-
The following commands are specific to our use case, and do things like install requirements, copy our source code into the image, and tell Docker what command to run when we start a container from this image.
32+
:::info What's in the Python image?
33+
The `python:3.10` image is also built using a `Dockerfile`! You can see the `Dockerfile` for it [here](https://github.com/docker-library/python/blob/master/3.10/buster/Dockerfile).
34+
35+
You can see it comes `FROM` another image. There is usually a chain of these, images built upon other images, until you reach the base image. In this case, the [base image](https://github.com/docker-library/buildpack-deps/blob/master/debian/buster/Dockerfile) is running Debian (a Linux distribution).
36+
37+
<details>
38+
<summary>Where is the base image!?</summary>
39+
<div>
40+
<div>
41+
42+
If you really want to go deep, you will be able to find...
43+
44+
- The [`python3.10:buster`](https://github.com/docker-library/python/blob/master/3.10/buster/Dockerfile) image builds on `buster-scm`
45+
- [`buster-scm`](https://github.com/docker-library/buildpack-deps/blob/master/debian/buster/scm/Dockerfile) builds on `buster-curl`
46+
- [`buster-curl`](https://github.com/docker-library/buildpack-deps/blob/master/debian/buster/curl/Dockerfile) builds on `debian:buster`
47+
- [`debian:buster`](https://github.com/debuerreotype/docker-debian-artifacts/blob/6032f248d825fd35e8b37037b26dc332e4659c64/buster/Dockerfile) looks really weird!
48+
49+
Eventually, the base image has to physically include the files that make up the operating system. In that last image, that's the Debian OS files that the maintainers have deemed necessary for the `buster` image.
50+
51+
</div>
52+
</div>
53+
</details>
54+
55+
So, why the chain?
56+
57+
Three main reasons:
58+
59+
1. So you don't have to write a super long and complex `Dockerfile` which contains everything you need.
60+
2. So pre-published images can be shared online, and all you have to do is download them.
61+
3. So when your own images use the same base image, Docker in your computer only downloads the base image once, saving you a lot of disk space.
62+
:::
63+
64+
Back to our `Dockerfile`. The commands after `FROM...` are specific to our use case, and do things like install requirements, copy our source code into the image, and tell Docker what command to run when we start a container from this image.
2365

2466
This separation between images and containers is interesting because once the image is created you can ship it across the internet and:
2567

0 commit comments

Comments
 (0)