|
| 1 | +## What is Docker? |
| 2 | +Docker - it's a development tool, that packs all of the working stuff such as code, environment, libraries and dependencies to a portable container. |
| 3 | + |
| 4 | +## Why Docker makes life easier? |
| 5 | +### Before |
| 6 | +Let's see how the development process were before the Docker was introduced. |
| 7 | + |
| 8 | +Developers should have installed a needed software and tools for building application into their own OS. Typically, installation process of all this stuff was complex and depends on a specific OS. |
| 9 | + |
| 10 | +Imagine our company has an app. To build it you need to get executable from file from C++ source code. With the code itself Developers team have to create a textual guideline about how to make an executable on a server. |
| 11 | + |
| 12 | +And let's say that our app also uses a Postgre database too. With that Devs also would create another textual guideline on how to configure and run db on the server. |
| 13 | + |
| 14 | +With that, OPS (Operational Team) starts to configure server. But, it can be very complicating and frustrating because something can show up upon the installation process or Devs can simply forgot to mention some details because of human factor. Imagine doing that every time something changed. |
| 15 | + |
| 16 | + |
| 17 | +### Nowadays |
| 18 | +With containers you don't have to install all of this stuff into our machine. |
| 19 | + |
| 20 | +Docker gives you an isolated environment and with that you can have all of your dependencies and configurations just in the Docker container and simply start to use it by one command (same for all OS). |
| 21 | + |
| 22 | +> Docker standardizes process of running any service on any local dev environment |
| 23 | +
|
| 24 | +> Easy to run different versions of same app without any conflicts |
| 25 | +
|
| 26 | +Today developers just pack all of the dependencies to a container and deliver it to the OPS team. With that, they only have to configure a Docker runtime on a server and run the container. |
| 27 | + |
| 28 | +There is less room for issues with this setup. |
| 29 | + |
| 30 | + |
| 31 | +## Docker vs Virtual Machine |
| 32 | +> Docker is a virtualization tool just like a Virtual Machine. |
| 33 | +
|
| 34 | +In order to understand how Docker run it's containers, we should know how an OS is made up. |
| 35 | + |
| 36 | +OS (Linux, MacOS, Windows) has a 2 main layers - OS kernel and OS Application Level. |
| 37 | + |
| 38 | + Application layer |
| 39 | + |
| 40 | + OS kernel |
| 41 | + |
| 42 | + Hardware |
| 43 | + |
| 44 | +* Kernel is at the core of every operating system |
| 45 | + |
| 46 | +* Kernel interacts between hardware & software components |
| 47 | + |
| 48 | +So, as we now by this moment, Docker and Virtual Machines is both virtualization tools. |
| 49 | + |
| 50 | +**What parts of the OS do they virtualize?** |
| 51 | + |
| 52 | +*Docker virtualizes an Application layer* and uses host's kernel. |
| 53 | + |
| 54 | +* Contains the OS application layer |
| 55 | + |
| 56 | +* Services and apps installed on top of that layer |
| 57 | + |
| 58 | + |
| 59 | +*VM virtualizes an OS kernel and an Application layer* - basically a complete operating system. |
| 60 | + |
| 61 | +Which means when you download a VM it uses it's own kernel. |
| 62 | + |
| 63 | +### Pros/Cons |
| 64 | + |
| 65 | +#### Docker Pros |
| 66 | + |
| 67 | +* Docker images smaller than VMs (couple of **MB** vs couple of **GB**). |
| 68 | + |
| 69 | +* Containers take seconds to start, while VM has to start it's kernel every time on boot up |
| 70 | + |
| 71 | +#### Docker Cons |
| 72 | +* VM is compatible with all OS but you can't do that with Docker. At least directly. |
| 73 | + |
| 74 | +Docker works with linux kernel. |
| 75 | + |
| 76 | +Docker Desktop it's an app for running linux-based containers on a MacOS and Windows by using a Hypervisor layer to create a lightweight linux kernel. |
| 77 | + |
| 78 | +<!--#TODO: learn about Hypervisor and virtualization--> |
| 79 | + |
| 80 | +### Docker Images vs Docker Containers |
| 81 | +Docker allows to package the application along with it's environment to artifacts. |
| 82 | + |
| 83 | +Artifacts like a regular files can be easily shared and moved. |
| 84 | + |
| 85 | +**Docker image** is an executable application artifact. But different with from C++ or Java executable is the environment, any services like npm, node and a source code. |
| 86 | + |
| 87 | +But what is a container then? |
| 88 | + |
| 89 | +To run the Docker Image (basically an app) in our computer, preconfigured app should run somewhere. An instance of a Docker Image is running on a **Docker Container**. |
| 90 | + |
| 91 | +From one image you can run multiple containers. |
| 92 | + |
| 93 | +`docker images` - List all Docker images |
| 94 | + |
| 95 | +`docker ps` - List running containers |
| 96 | + |
| 97 | +We get containers when we run a Docker Image. But how do we get these images? |
| 98 | + |
| 99 | +### Docker Registries |
| 100 | +**Docker Registries** - a storage and distribution for Docker images. |
| 101 | + |
| 102 | +Official images available from applications like Redis, Mongo, Postgres etc. |
| 103 | + |
| 104 | +Official images are maintained by the software authors in collaboration with the Docker community. |
| 105 | + |
| 106 | +Docker hosts one of the biggest Docker Registry, called **Docker Hub**. |
| 107 | + |
| 108 | +Docker tags are used to identify images by name (tag = version). |
| 109 | + |
| 110 | +### How to get an Image |
| 111 | +1. Locate image, for example nginx, a simple webserver. |
| 112 | + |
| 113 | +2. Select the tag. |
| 114 | + |
| 115 | +3. Execute `docker pull nginx:1.23` |
| 116 | + |
| 117 | +After this Docker Client (our Docker CLI) says to Docker Service to pull the Image from Docker Hub. |
| 118 | + |
| 119 | +4. Execute `docker images` |
| 120 | + |
| 121 | + |
| 122 | +### Run an Image |
| 123 | +Simply execute `docker run nginx:1.23`. |
| 124 | + |
| 125 | +Running `docker ps` we can get some info about running containers. |
| 126 | + |
| 127 | +To run container without block a terminal `docker run -d nginx:1.23`. |
| 128 | + |
| 129 | +To see log of a running container if we run our application in the background `docker logs {containerID}` |
| 130 | + |
| 131 | +* Docker pulls Images automatically when running an Images |
| 132 | + |
| 133 | +For example, we can simply do without any pull: `docker run redis`. |
| 134 | + |
| 135 | + |
| 136 | +### Port Binding |
| 137 | +How do we get access to a container? We can't right now. |
| 138 | + |
| 139 | +Application inside container runs in an isolated Docker network. |
| 140 | + |
| 141 | +We need to expose the container port to the host (the machine the container runs on). |
| 142 | + |
| 143 | +Each application has some standard port. nginx is port 80. Now we can't access it through browser like this: *localhost:80*. |
| 144 | + |
| 145 | +`docker stop {container}` - to stop a container |
| 146 | + |
| 147 | +Port forwarding: |
| 148 | + |
| 149 | +`docker run -d -p {host_port}:{container_port} {image}:{tag}` |
| 150 | + |
| 151 | +`docker run -d -p 9000:80 nginx:1.23` |
| 152 | + |
| 153 | + |
| 154 | +Now with -p 9000:80 we expose docker container to our local network. |
| 155 | + |
| 156 | +*localhost:9000* - code 200. |
| 157 | + |
| 158 | +For our host machine we can choose any port. But it's standard to use the same port on your host as container is using. |
| 159 | + |
| 160 | + |
| 161 | +### Start and stop containers |
| 162 | + |
0 commit comments