Before diving into Docker, let's cover some key concepts.
It's the solution for the classic problem "It works on my machine!!". The usual issue here remains with environment and dependencies. Docker solves that problem for us. So if your app works fine in your local docker, its bound to run fine anywhere else be it staging or Production.
Docker creates containers of our app, that includes our app and the environment & dependencies required by app to run smoothly.
I know the paragraph below seems lengthy, but bear with me it'll be totally worth it.
Let's consider an example. Imagine we own a tiny little food outlet in our basement. Our food is delicious and words spread really soon. Now people want us at different events. But since our entire setup e.g. wash basin, water supply gas supply e.t.c everything is being used from home, going somewhere else and getting the entire setup and then cooking became quiet a task. And more often than always we are likely to miss something or something else leading to issues.
This keeps ruining our name and hamper our growth.
We decided the fix the issue and came up with an idea. We updated our entire setup and converted it into a food truck. Now anytime we get in an event we don't have to worry about the setup anymore. Chef can focus on the food only and get us the consistent delicious food.
Words start to spread again and now we in demand more than ever. So we created multiple identical food trucks and successfully catered all of them, thus creating tons of wealth.
This is exactly what docker does for us. The same code that works fine on dev machine breaks on production. Reason being change in environment missing dependencies etc. Like dev was using Windows and server is Linux, so file path structure changed. Dev had node installed on machine but server had not and so on. All the things that our code need to run, be it the OS or additional packages or softwares are called dependencies. Docker creates a wrapper(called container) with our app and provides all the dependencies our app needs. Since it already had everything our app needs, our apps can run in isolation without being impacted by the environment they are in. So it'll behave consistent in any environment and ensure that when an app runs at local, it runs the same in production the same.
Docker is not recommended to be used natively on Windows.
Docker is primarily designed to run on Linux, as it utilizes a lot of Linux-specific functionalities under the hood.
It's better to run it in a Linux environment for a smoother experience, which is what we'll be using in production as well.
We don't need a separate machine to run Linux. We can create a virtual machine (VM) and run Linux inside our Windows environment.
WSL is a native Windows solution that provides a Linux-like environment inside Windows.
Hyper-V: A virtualization technology in Windows that creates and manages virtual machines. It can handle full-fledged VMs for WSL2, but it's more lightweight than traditional virtual machines. Consider it a lightweight, Windows-native alternative to Oracle VirtualBox.
Now that we have this basic understanding, here's how it works:
us <------ WSL ------> Linux VM (inside Hyper-V)
wsl --list --online
wsl --install -d <distro_name>
wsl --install
will install the latest ubuntu distributionWin
🪟 key and search ubuntuInstalling, this may take a few minutes...
Let's update the installed linux
Use command sudo apt update
this will update the package index and our system will have the knowledge about the latest versions of the packages available. This will ensure that we don't end up installing and older version of softwares.
And done 🎉🥳, we are now inside our new linux machine💻
Let's start with docker's documentation available here : Docker documentation
The documentation uses
apt-get
, which is an old version of the package, we'll be usingapt
the modern version
DockerHub is a cloud-based registry service to store our images. Consider it as GitHub for images. Here are the steps
Let's complete the setup now
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
Docker GPG key is used to verify the authenticity of the packages being installed through docker
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
This will again refresh the packages from our new repositorysudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
This command adds the Docker APT repository for our specific Ubuntu version to the /etc/apt/sources.list.d/docker.list file
. It ensures Docker packages are sourced from the correct repository and verified with the provided GPG key.docker login
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2
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/
One more thing before we proceed, each time we execute a docker command we need to run it as sudo
user.
Let's add user to docker group
getent group docker
if output is like docker:x:999:
means group existssudo groupadd docker
sudo usermod -aG docker $USER
groups $USER
.
danish : danish adm dialout cdrom floppy sudo audio dip video plugdev netdev docker
sudo reboot
: wait for a few minutes and press enter as mentioned in log to restartdocker ps
and that should work fine