How docker will save you time, and why should you learn it immediately? (1/2)
What is Docker? And why is it popular?
Everywhere on the internet, you find articles about how docker is compared with VM and diagrams like this:
But I'm going to teach Docker just as a tool, so it will be so easier to understand, and we don't dissect its underlying functionality a lot (we do it just a few ๐ ).
Don't install new apps! just pull them!
The most interesting thing about docker for a beginner, is the ability to pull different tools so easily. DockerHub will be like the App Store!
As you see, we can pick up any app that we want,
Actually, we call them images in the world of Docker.
What is an image?
Image is a collection of applications and commands running on a base image and this base image itself is a simplified Linux distro. This Linux doesn't have a GUI and could be any distro such as Ubuntu, Alpine, Bullseye, etc.
For example, you can install almost any version of any database such as Postgres, Redis, etc, by pulling their image and running them as containers. You can have multiple versions of the same application at the same time.
What is a container?
The Image itself can be supposed as an executable file, it is nothing unless you run it, anytime you run it, a container (process) will be made. you can run many same containers using an image.
Getting dirty with Redis!
Let's have a hands-on experience with Redis, Go to this link
In this picture, Tags can be assumed as different versions of images, tag names may include the base image name and its version.
Official images are famous projects that are being made constantly by the docker team.
Also, everyone including you and me, can make an image and push it on Dockerhub as a public or private docker image.
At the top right, you can see the docker pull redis
command, it's the simplest way to pull this image, but we can find more detailed commands at the bottom of that page.
For example, this command will pull the redis
image and run a container called "my-redis-container":
docker run --name my-redis-container -d redis
-d
is used for detaching the process, it will run this container and leave the shell alone for your future use.
But this container might not be useful without exposing the port (to connect with it).
docker run --name my-redis-container -p 6379:6379 -d redis
-p 6379:6379
is a port mapping that maps port 6379 of the container to the 6379 of our localhost
. And you can now access Redis through localhost:6379
What other things can I do?
- Are you a windows user but you like to use Linux without getting into the trouble of VM or Double boot? Run this command:
docker run --name itubu -it ubuntu
This will make a container named itubu
and connect your shell to its shell. (itubu
is just an arbitrary name I made up for the sake of the article! ๐โ๐ผ)
The run
command will look in your local to see if there is any image called ubuntu, then if it doesn't exist, docker will pull it from Dockerhub.
- Do you want to use the Tor network without getting into the trouble of configuring tor? Run this command:
sudo docker run -p 8118:8118 -p 9050:9050 -d dperson/torproxy
dperson/torproxy is an unofficial docker image.
-p 8118:8118
(and -p 9050:9050
) is a port mapping that maps port 8118 of the container to the 8118 of our localhost.
Now you can proxy your requests through the Tor network using these ports.
- Do you want to use a headless browser? Run this command
docker run -p 3000:3000 browserless/chrome
And you'll have a chrome browser on localhost:3000 which could be so helpful for scrapping.
- We spoke about databases before ๐ and there are so many other use cases.
It mostly needs no effort
Docker itself plays an important role in the DevOps world, and without a doubt, it has so many tips and tricks. But in most cases, as a beginner, you will just need a one-line command or a 10-line Dockerfile, to make your project way much better. The advantages of using Docker are explained in a video I added to the end of this article and I'm not covering them.
Other resources
There are so many tutorials out there, but I'm gonna suggest the ones that I enjoyed the most.
As an instruction that shows how containerization will help us:
Containerization explained
Docker and Kubernetes course by Stephan Grider
Final quote
I tried to help you understand that docker is not a challenge, it's a tool to make your life easier, this part included some cool stuff, next part we will cover the basics of writing a Dockerfile and how to make your custom images.