Content
- How resources are allocated to a container
- How a container is created from an Image
- What happens behind the scenes when you build a Docker file
Prerequisites
- Should know basic docker commands
- Should know how to write Docker files
- Should have docker installed in your machine
How resources are allocated to a container?
Resources are allocated to a container by following two rules designed for Linux
- Namespacing: Isolating resources per process (or group of processes) like Hard drive, Network, Hostnames, Interprocess communication etc.
- Control Groups(cgroups): Limit amount of resources used per process like Memory, CPU usage, Hard drive I/O, Network Bandwidth etc.
These two rules belong to Linux so even if you are running container in you windows or mac machines it automatically creates a Linux based virtual machine on top of your OS and then apply these two rules.
How a container is created from an Image?
When you run docker pull, it downloads the image from docker hub into your machine and that downloaded image is used to create a container.
Image has two sections
- File system snapshot : which has all the files that come attached with the image
- Startup command : This commands runs when you start your container.
When you build a container from an image it takes a copy of FS snapshot from the image and place it into a small portion of your Hard Drive. Every image has a startup command in it which tells the container what command to run when you first start a container.
So, now we know how a container is created from an image, we'll move on to the next question which is what happens behind the scene when you build a Docker file.
What happens behind the scenes when you build a Docker file?
Whenever you build an image out of Docker file, it runs certain steps before giving you the final image. You might be having a question in your mind that what actually happens behind the scenes? Here is what happens when you build a Docker file, we'll take an example of a simple Docker file
Step-1 : FROM alpine
- It downloads the latest alpine image form docker hub if not present in your build cache.
Step-2 : RUN apk add --update redis
- It first looks back at step-1 and runs a temporary container out of the "alpine" image which we just downloaded in previous step.
- The command "apk add --update redis" is executed inside the running temporary container as the primary running process and installs redis in the container.
- Takes a snapshot of the file system with redis installed.
- Creates a new temporary image of the file system.
- Removes the temporary container.
Step-3 : CMD ["redis-server"]
- Now looks back at step-2, creates a new temporary container out of the image which we got in step-2 with redis installed.
- Sets the default/startup command of that container as CMD["redis-server"]
- Takes the snapshot of this container.
- Builds the final image out of the temporary container with CMD ["redis-server"] as the startup command and redis installed.
- Removes the temporary container.
Thanks for reading this blog! Any Suggestions and questions are welcomed in the comment section.