What is Docker?
Docker is a container technology: A tool for creating and managing containers.
Docker is an open-source containerization platform by which you can pack your application and all its dependencies into a standardized unit called a container.
What's a container in
software development and why might we want to use it?
Container: A standardized unit of software. A package of code and dependencies to run that code. Containers are light in weight which makes them portable, and they are isolated from the underlying infrastructure and from each other container. You can run the docker image as a docker container in any machine where docker is installed without depending on the operating system. The same container always yields the exact same application and execution behavior! No matter where or by whom it might be executed.
Key Components of Docker
The following are the some of the key components of Docker:
- Docker Engine: It is a core part of docker, that handles the creation and management of containers.
- Docker Image: It is a read-only template that is used for creating containers, containing the application code and dependencies.
- Docker Hub: It is a cloud-based repository that is used for finding and sharing the container images.
- Docker file: It is a script that containing instructions to build a docker image.
- Docker Registry: It is a storage distribution system for docker images, where you can store the images in both public and private modes.
Why Containers?
Why would we want independent, standardized “application packages”?
- We often have different development and production environments. We want to build and test in the same environment as we later run our app in production.
We want to have the exact same environment for development and production à This ensures that it works exactly as tested.
- Different development environments within a team/company. Every team member should have the exact same environment when working on the same project.
It should be easy to share a common development environment/ setup with (new) employees and colleagues.
- Clashing tools/versions between different projects. When switching between projects, tools used in project A should not clash with the tools used in project B.
We don’t want to uninstall and re-install local dependencies and runtime all the time.
Solution: Virtual Machines/ Virtual Operating Systems
The problems solved by
Docker and containers makes sense hopefully and it makes sense that we might be
looking or a solution like Docker and containers but if you've been in the
software development area for some time, you might also think to yourselves why
Docker and why containers? Isn't that a problem? So that reproducible
environment thing, isn't that a problem which we can solve with virtual
machines?
Virtual
machines with virtual operating systems encapsulated in their own shell
independent from our host operating system.
With virtual
machines, we have our host operating system, Windows or macOS or Linux and then
on top of that, we install the virtual machine. So, a computer inside of our
computer.
Well, this
works but there are a couple of problems.
- One of the biggest problems is the virtual operating system and in general, the overhead we have with multiple virtual machines.
- Every virtual machine is really like a standalone computer, a standalone machine running on top of our machine. And therefore, if we have multiple such machines especially, we have a lot of wasted space and resources because every time a brand-new computer must be set up inside of our machine and that, of course, eats up memory, CPU and of course, also space on our hard drive.
- And that really can become a problem if you have more and more virtual machines on your system.
- Because if you have a lot of things, which are always the same and still duplicated, especially the operating system. You might be using Linux in all your virtual machines and still it's installed separately in every machine. And that, of course, wastes a lot of space.
- Pros:
- Separated environment
- Environment-specific configurations are possible
- Environment configuration can be shared and reproduced reliably
- Con:
- Redundant duplication, waste of space
- Performance can be slow, boot times can be long
- Reproducing on another computer/server is possible but may still be tricky.
So, virtual machines solve the problem but not in a perfect way, and that's why we have Docker and containers.
- It's important to understand that containers are the key concept here.
- Docker is just the defacto standard tool for creating and managing them.
With containers, we still have our host operating system, Windows, macOS, Linux, whatever it is but then we don't install a couple of machines in the machine.
Instead, we utilize built-in container support, which our operating system has or emulated container support, something Docker will take care about that this works. And then we run a tool called the Docker Engine on top of that.
Based on that Docker Engine, which now runs on our system, which is just one tool, one lightweight small tool being installed there, we can spin up containers. And these containers contain our code and the crucial tools and runtimes our code needs.
Now, the other great
thing about containers is that you can configure and describe them with a
configuration file and you can then share that file with others so that they
can recreate the container or you can also build the container into something, which
is called an image, which you need to do anyways and then you can share that
image with others to ensure that everyone's able to launch that same container
which you have on your system on their systems.
Containers vs Virtual Machines
- Docker Containers
- Low impact on OS, very fast, minimal disk space usage
- Sharing, re-building and distribution is easy
- Encapsulate apps/ environments instead of “whole machines”
- Virtual Machines
- Bigger impact on OS, slower, higher disk space usage
- Sharing, re-building and distribution can be challenging
- Encapsulate “whole machines” instead of just apps/ environments
Docker Setup