Welcome to Docker for Dummies!
This is going to be an idk-how-many-part series
where I'll try to explain docker in as simple a way as possible. I'm also new to the world of containerization so the purpose of this series is to try to teach this technology while learning it myself.
If you are new to the tech industry or have already been a part of this world, you might've probably come across the term Docker
now and again, but what exactly is this docker? Are we building a ship or something?
The wonderful world of Docker
Imagine you made a delicious meal and want to share it with your partner but then you realize that you are in a LDR and it won't be possible due to the distance. But what if there was a way to pack your dish, along with all the ingredients and tools you used, into a box that keeps it fresh and ready to eat anywhere, anytime? Well, there isn't one for food but there's one for software that does something like that and that's? You guessed it right Docker
Docker is like a magical box that packages your application, along with everything it needs to run, into a neat, portable container. This container can run on any computer, anywhere, without any worries about compatibility or missing ingredients. Cool, right?
In a more technical term - Docker is a platform that uses containerization to allow developers to package applications along with all their dependencies into a container. This ensures that the application can run consistently across different computing environments.
Why Should You Care About Docker?
There are a lot of reasons to use docker. I'll explain some below:
Consistency: Suppose your team is building an application and you build a feature in your local system and everything is working as expected. But then your team member tries to run the same feature in their local set-up and it crashes and you suddenly find yourself amidst a decade-old problem in software engineering
Docker ensures that your application works the same way
everywhere. No more "it works on my machine" problems but rather
"it works on my container" problem đź«Isolated Environments: If you are building 3 different applications that require 3 different versions of node, trying to set it up in your local machine, switching between them, and trying to make them work would be a hassle that I would only wish upon my enemies not that I have any enemiesđź‘€. But with docker, you can just switch between the containers and develop everything parallelly without much difficulty.
Portability: Whether you’re developing on your laptop or deploying to a cloud server, Docker containers can run anywhere.
Efficiency: Containers are lightweight and start up in seconds, making them perfect for development and deployment.
These are just a few of the benefits. Docker is a gift that keeps on giving.
Virtual Machines vs Containers
It might be easy to confuse virtual machines with container but they are completely different technologies and solve different problems. Here's a picture to differentiate them
Containers use the host OS's kernel and share it among all containers, making them more lightweight and efficient. VMs emulate an entire physical machine, including its operating system (OS), making it possible to run multiple OS instances on a single physical machine.
Getting Started with Docker
Ready to dip your toes into the Docker waters? Let’s start with a quick example that will get you up and running in no time.
Step 1: Install Docker
First things first, you’ll need to install Docker. Head over to the Docker website and download Docker Desktop for your operating system (Windows, macOS, or Linux). Follow the installation instructions provided.
Step 2: Run Your First Container
Now, let's run your first container. We are going to run hello-world
container. Open your terminal (Command Prompt on Windows, Terminal on macOS and Linux) and you'll run the following command:
docker run hello-world
If everything is working fine you should see something like this on your screen
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:d1b0b5888fbb59111dbf2b3ed698489c41046cb9d6d61743e37ef8d9f3dda06f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Let's break down what that command did, the command pulled an image hello-world
from the docker hub and ran it in your host machine. If the image is already present in your system the command will skip the pulling part and just run the existing image.
Now, if you run the command again it will run the previously pulled image.
docker run hello-world
Image and containers
Think of a container as a ready-to-eat meal that you can simply heat up and consume. An image, on the other hand, is the recipe or ingredients for that meal.
So just like how you need a recipe and ingredients to make a meal, you need an image and a container runtime (Docker engine) to create a container. The image provides all the necessary instructions and dependencies for the container to run, just like a recipe provides the steps and ingredients to make a meal.
In short, an image is like a blueprint or template, while a container is an instance of that blueprint or template.
Some useful commands
You can try out these commands and see what the output
# lists all the running Docker containers on your system
docker ps
# lists all Docker containers on your system, including those that are currently running, as well as those that have stopped or exited.
docker ps -a
# lists all the Docker images stored on your system, showing details such as the repository name, tag, image ID, creation date, and size
docker images
You can check the official documentation for other commands and try them out.
This is it for this part. In the next part, we will be writing a dockerfile for a small web app and it should be exciting.
Until then take care! Keep learning!
Top comments (0)