In this blog I will be talking on the “Topic of the Moment” among IT and DEV folks which is Docker.
This blog will be like a Q&A where a curious user(ME) asks questions regarding the docker and gets answers from a person who is an expert.(My thought about my skills after listening to a 1 hr long video on docker).
Okay lets jump right into the world of docker!!
Table of Contents:
- What is docker and how is it different from a virtual machine?
- Installing docker in GNU/Linux OS
- Docker Images vs Containers
- Types of Containers
- Some common commands to ease our life in the world of Docker.
The Curious Kid: So what is docker??
The Expert: Docker is an open source containerization platform. It enables developers to package applications into containers — standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.
In simple words its Like a VM but not exactly a VM
The Curious Kid: So you say its like a VM but not exactly it. Can you differentiate between those two?
The Curious Kid: Yay! I could get it somewhat but it would be better if we proceed a bit hands-on.
The Expert: Okay !! Lets jump right into hands on experience.
2. Installing docker in GNU/Linux OS
The Expert: sudo apt-get install docker.io
Once you install and you think of playing with it :
ALWAYS REMEMBER TO START DOCKER SERVICE!!
The Curious: How to start the docker service??
The Expert: sudo service docker start
sudo systemctl start docker
3. Docker Images vs Containers
The difference is very much similar to that of program and process.
Image is like a program which resides in disk and provides info on architecture ,service etc to be invoked when run.
Containers can be compared to process as they reside in memory.
4. Type of Containers
We can say that the containers can be put onto 3 different categories:
- Containers which spins up does a thing and exit.
- Containers which spins up and provides a service indefinitely
- Containers which has some development tools in it.
The Curious Kid: This sounds so weird and vague can we jump into terminal.
The Expert: Yep ! Lets see how each of these looks in the terminal.
Type 1 : Hello-World in Docker
This container belongs to the 1st category that is when its run it prints some content on to the terminal and exits.
The Curious Kid: Yeah this is getting interesting .But how exactly do we run docker images??
The Expert: sudo docker run <image-name>
For hello world image: sudo docker run hello-world
The Curious Kid: But don’t we need an image in our system to run it??
The Expert: Great observation!! We first need an image to run and this can be pulled from docker hub using the pull command
Pull an image: sudo pull <image-name>
To list available images in the system: sudo docker images
You can also visit docker hub at:https://hub.docker.com/search?type=image
Pull an image and list the available images
Run an image
Image ID in docker:
Docker images can be managed(pull, delete , run etc) by their image ID also.
Example: sudo docker run <image-id>
Type 2 : Nginx server docker image
Running nginx docker image:
Unlike the hello-world this does not exit and this runs or provides service forever unless we stop it using CTRL+C .
We could list the active containers by
sudo docker ps
The Curious Kid: So do we now have an nginx server running on port 80??
The Expert: Once again great question. We don’t have nginx listening on port 80 as Docker images have network ports but we cannot connect to these from outside the container (ie from our local machine).
The Curious Kid: Then how is it even possible to access these services??
The Expert: The workaround for this is to bind the ports in our actual system to the ports of the docker containers. By this we can access the services from our local machines using the ports we specify.
Let me demonstrate it:
For binding ports: sudo docker run -p <port in local machine>:<port in docker container> <image-name>
Example: sudo docker run -p 8080:80 nginx
8080 — port on the local machine
80 — port inside the container
The Curious Kid: Wow!! This is great and now I would like to know how can we change the source code for the website inside the container.
The Expert: For this we need to get a interactive shell inside the container and from there its regular stuffs like using text editors for editing.
sudo docker exec -it <container-id> /bin/bash
exec — executes commands inside the container
-it - spawns a interactive shell
The Curious: This is really useful .
Few moments later!!
OMG!! The files been edited disappears once I kill the container and start it.
Is there anything I could make to make the edits persistent?
The Expert: This can be achieved in a similar way as binding of ports .
We have to bind the directory inside the container with some directory in our local machine. This stores the files in our local machine and we have to use this file every-time inside the container to make changes.
sudo docker run -v <path in our local machine>:<directory inside the container> <image name>
Example: sudo docker run -v /home:/edits nginx
If we have to change the index.html file and make it persistent, then we have to bind the directory in which index.html resides inside the container to some directory in our local machine .
Type 3 .Learn new programming language with docker
The Expert: If setting up some environments to learn new programming language or some other skill has stopped you from pursuing it , then docker is what you need.
Docker images for development environments are huge in number which includes python, ruby, rust, go etc..
We can just pull the image of some language we are interested in learning and open a shell in it and start learning it without any worries on dependencies and installation.
But remember to bind the files you have created to make it persistent!!
sudo docker run -it -v /home/user/python-code:/python-code python /bin/bash
The Curious Kid: One last question. Why do we keep using sudo to run docker commands?
The Expert: This is a great way to end the Q&A I guess.
We use sudo or elevated privilege to run docker commands because it uses the kernel of our OS for its execution and accessing the kernel and its functionalities require higher privileges.
With this we end the Q&A of docker.
I hope this was useful and probably you can use this for deploying your web-apps or any apps in general!
For further detailed content on Docker :
This is the resource which gave me a very clear idea on Docker and I hope this will be the same for you too!