Table of contents
- What is Docker?
- Why Docker?
- Basic terms for Docker (to begin with)
- Getting hands-on with Docker
- Installing docker
- Stopping a docker container
- Getting an image from DockerHub / Run a container
- Removing unnecessary containers
- Inspecting the image
- Docker port
- Display Container(s) resource usage
- List processes running inside a container
- Execute a command in a running container
- Save the docker image in a tar file (exportable)
- Load a docker image from a .tar file/package
- Happy Learning ;)
When "Why" is clear, then "How" is easy!
I also said this in my blog for day8 - the beginning of the OG of Version Control System - Git. Now, let's take a dive in the world of Containerization with Docker. This time I wanna explain it like I am explaining to a ten.
What is Docker?
Q) Hold on, what is an 'application virtualized in a container'?
Q) What are the "tools" that aid the above 'virtual containers'?
The answer to above questions are the answer to the 'why Docker?'
Why Docker?
A small story that made me unsee the need of Contianers...
I had an advanced course on Computer Networks at my university. For the labs, we were required to install a software which would run only on ubuntu16 or some older versions of linux. Back then I was using Debian11 and didn't want to switch or work on a virtualbox. So I wanted to see if there can be another root folder of linux with the older ubuntu16 in it!πΎ
Researching over the same, I came across the concept of chroot, short for "change-root". This command allows you to create
a separate environment within your system
, without interfering with the rest of your system. Thus you can seamlessly test, debug or experiment around some heart-breaking piece of software. Come-on your machine gotta be your heart-beat πRead more on chroot π
Now, coming back to modern days, consider a scenario :
A development team has to demo a software on the client machine "remotely". They gave the client a full set of instructions and software dependencies and setup written in a text file so that the client can create such an environment and run it.
But, it doesn't always work so smoothly, especially in a world where electronics miraculously do the expected work after restarting or they don't otherwise.π³
The client still hasn't been able to start the software on his machine, but the development team says - "It works on my machineπ"
Consider the development team giving their client a whole package - a container, with all the necessary environment and dependencies setup in it; where the client can get the application running as quickly as possible with minimum headache...
This exactly is what Docker does! Read the definition again -
Docker is an open source software platform used to create, deploy and manage virtualized application containers on a common operating system (OS) with the aid of certain tools.
The tools that 'aid' will be learnt in the later days I suppose.
Basic terms for Docker (to begin with)
Docker Image :
A standalone, virtual and executable "package" that can run particular software configured inside it.
Contains the code, dependencies, libraries and the required environment setup to run a software.
Can run on any machine irrespective of it's architecture and system configuration except for maybe some basic hardware requirements like RAM, free disk space, etc.
It runs inside a container and it is also called a Docker container image.
Docker Container :
It can be thought of as a virtual enclosement to run an image.
It provides isolated environment in order to achieve the separation between the host system environment and the environment contained within the image.
Can be used to run microservices or even small operating systems.
It talks to the kernel and the kernel in-turn provides the hardware resources required to support the software in the image. The image never talks to the kernel.
It can also be said to be a special kind of process as it has a special kind of file system provided by the docker image.
DockerHub :
DockerHub to Docker is like GitHub/GitLab to Git.
It's a web-based storage platform for public/private access to docker images.
Visit dockerhub.
Getting hands-on with Docker
Installing docker
Open a terminal and type the following commands (assuming debian-family for simplicity as mentioned in an earlier post) :
sudo apt update; sudo apt install docker
Check if it's the latest version
docker version
Check if it's up and running with the following :
sudo systemctl status docker
If you get the green fields as they are here, everything's good. If not, try the following commands and again run the above command.
sudo systemctl enable docker.service docker.socket sudo systemctl start docker.service docker.socket
Golden rule:
If you ever get stuck at any command in docker with what flag to use or what command of docker to use, follow the known command with a "--help".
for e.g.
docker run --help
,docker --help
,docker images --help
, etc.man docker run
,man docker images
, also works π but not for all of them.
It's always good to learn how to literally stop what you started which you don't know can cause a mess - like braking is taught first while learning to ride a vehicle, isn't it?
Stopping a docker container
Enlist the running containers
docker ps # if empty, no container is running
To stop a particular container, enter the first few characters of its container-ID preferrably first 3 or 4 chars, else copy the container ID.
docker stop <container_ID1> <container_ID2> ...
If the above doesn't work, this one should.
sudo docker rm -f <container_ID>
Getting an image from DockerHub / Run a container
To avoid wasting huge amount of data, I selected a small-sized minimal Linux-based operating system - Alpine Linux. Let's work with that image.
Pulling the image from DockerHub to your local machine. Two ways to do this :
docker run
: first checks if the image exists locally, if not found, then it pulls from dockerhub.docker pull
: pulls the image from dockerhub no matter what!Thus, docker run is better for obvious reasons.
docker run -it alpine # run in interactive mode
The interactive mode using "-it" flag to
docker run
is when you want to execute certain commands while the container is in a running state, i.e. when you want to interact with the container.
Removing unnecessary containers
If a particular container is not required, you gotta remove it totally to avoid it taking space on your disk.
docker rm <container_ID1> <container_ID2> ...
Removing all the unnecessary/stopped containers at once
docker rm $(docker ps -aq) docker rm -f $(docker ps -aq) #if the above doesn't work
Inspecting the image
Get the list of images on your machine using the command
docker images
.Run the following command to get the low-level information about the image like container, image, volume, network, node, service or task - all rendered in a JSON format.
docker inspect --type=image <image_ID>
Get a list of all containers on your machine :
docker ps # lists all running containers docker ps -a # lists running as well as the stopped containers
If you want to inspect a container, use:
docker inspect <container_ID>
Docker port
List port mappings or a specific mappings for the container.
As an example, I have run an nginx docker image in the detached mode (flag "-d") i.e. it is run as a background process such that even if the parent process(the terminal/shell session is killed, it will continue working).
docker run -d -p 80:80 nginx
This means to publish the port 80 of the host to port 80 of the container.
Display Container(s) resource usage
Use the following command :
docker stats
List processes running inside a container
Use the following command :
docker top <container_ID>
Note that you can use the first 3 characters of a container_ID to refer it.
Execute a command in a running container
Use the following command to do so
docker exec <container_ID> <command>
For e.g. you might wanna check the contents of a docker container using ls when the container is running in background.
docker exec <container_ID> ls
Save the docker image in a tar file (exportable)
Use the following command :
docker save -o <filename>.tar <image_Name>
Load a docker image from a .tar file/package
Use the following command :
docker load -i <image.tar>