Docker basic to advance
There are multiple ways to deploy an OS:
- Bare Metal
Bare metal is a computer system without a base operating system (OS). It is a computer’s hardware assembly, structure, and components that are installed with either the firmware or basic input/output system (BIOS) software utility or no software at all.
- On Cloud
On Cloud, we request cloud providers such as AWS & they provision requested infra (CPU, Ram, Storage) on our behalf and provide it to us.
- In a virtualized Environment (Virtualisation)
Virtualization is the act of creating a virtual version of something at the same abstraction level, including virtual computer hardware platforms, storage devices, and computer network resources.
Suppose we have an OS that includes Ram, Rom, CPU, and Networking then its virtualized ENV will contain the same [Ram, Rom, CPU, and Networking] in it.
- In the Form of a Container (Containerisation)
It’s something that we use to hold stuff.
In household terms, a container is something that holds something in them. Ah! Correct, we can hold vegetables/water, etc. in the container.
Similarly, in the case of software, a container is another form of virtualization that holds the application code & all its dependencies. In other words, Containers are packages of software that contain all of the necessary elements to run it in any environment.
Why Containers when we have Virtualisation?
Containers are often compared to virtual machines (VMs). Containers also package applications in a similar way that virtual machines do & offer more benefits to us.
- As we know virtualization runs on top of Hardware whereas containerization runs on top of the operating system which provides tons of advantages.
- Containers are much lightweight units than VMs., At a point where VMs are in Gbs in size containers last Mbs in size.
- Each VMs needs a full-blown OS to start but in the case of Docker, they use the host OS.
There are a lot of products available for virtualization such as:
Podman, crio, Docker, etc.
In this series, we are discussing more on Docker.
Docker Terminologies & Concepts:
What is Docker?
Docker is a product from Docker Inc. which is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers.
what is Docker Image?
An image is a file that represents a packaged application with all the dependencies needed to run correctly. In other words, you can say it's an OS just like Ubuntu that we install in our system via a DVD.
What is a Docker registry?
A Docker registry is a storage and distribution system for named Docker images. The same image might have multiple different versions, identified by their tags. A Docker registry is organized into Docker repositories, where a repository holds all the versions of a specific image.
Docker Installation:
It is easy to install docker in the system follow the links attached to download docker.
Docker basic commands:
- docker run -it ubuntu
Start a new container & it will take you inside the docker terminal
2. docker run -dit ubuntu
To run a container in detach mode (Not give terminal instantly)
3. docker ps
List all containers running
4. docker images
list all images present in the system
5. docker ps -a
List all containers (Stopped + running )
6. docker stop container_id
To Stop a running container
7. docker kill container_id
Forcefully Stop a running container
8. docker exec -it container_id bash
To go inside any docker container.
9. docker rm container_id
To delete a stopped container
10. docker rm container_id -f
To forcefully delete a running container
What will happen when I run the command “ docker run -it ubuntu ”?
Get DevKishan Joshi’s stories in your inbox
Join Medium for free to get updates from this writer.
This will first search the specified image in the local system & if found launch the container with the image & if the image is not available then it by default pulls that image from the Docker hub.
Docker Hub is a service provided by Docker Inc for finding and sharing container images with your team.
How industry uses Docker?
It's obvious that in the IT industry, we do almost things via automation & for automation of course commands are not the right way to initialize & deploy things.
Also Docker we use to deploy applications, So how to put our application inside a Docker Image & run it on multiple environments?
here is the answer: Dockerfile
What is Dockerfile?
As we know we use containers to deploy our applications. The Dockerfile is a text file that contains the instructions needed to create a new container. In Dockerfile we specify our application code & how to initialize code, how to download the respective libs, what type of image to use, and how to run that code inside an environment.
- Create a Dockerfile for the Application:
Here We are creating Dockerfile for a python (Flask) application:
Find Demo code: GitHub
Create a file with the name “Dockerfile”, in the root directory of the application code. It is a specific name & we cannot create files with other names.
FROM python:3
LABEL created_by="dev"
WORKDIR /usr/share/myapp
COPY ./ /usr/share/myapp
RUN pip3 install -r requirements.txt
EXPOSE 8080
CMD [ "python3", "app.py"]here, /usr/share/myapp is working directory of application & all code of application will be placed here.
Most commonly used tags in a Dockerfile:
FROM: Specifies the base image to use for the image being built.RUN: Executes a command during the build process.CMD: Specifies the default command to run when the container is started.ENV: Sets an environment variable in the container.COPY: This copies files or directories from the host machine only into the container.ADD: Add is similar to copy with addition it allows A URL instead of a local file/directory & extracts tar from the source directory into the destination.EXPOSE: Exposes a specific port or ports to be used by the container.LABEL: Adds metadata to the image in the form of key-value pairs.USER: Specifies the user to use when running the container.WORKDIR: As the name says, it sets the working directory for the container.
2. Build a docker image:
docker build -t {image_name}:{Version_name} Path_of_Dockerfile
docker build -t myapp:v1 .
3. Run Docker container:
docker run -dit -p 8083:8080 myapp:v1
This will run a container with the specific image & the following application is deployed. here, I have used the -p option to publish the container’s port(s) to the host, without this we cannot expose our application to the public world.
Here 8083 is the port we want to expose on the host machine & similarly 8080 is the application port where the application is running inside docker.
For more Dockerfile Keyword Explanations visit: the link
Storage in Docker
There are 2 types of storage in docker:
- Ephemeral Storage
Ephemeral storage is the volatile temporary storage attached to the docker container which is only present during the running life of the instance. In the case that the container is stopped or terminated or underlying hardware faces an issue, any data stored on ephemeral storage would be lost.
2. Persistent Storage
Persistent storage is any data storage device that retains data after power to that device is shut off. It is also sometimes referred to as nonvolatile storage.
How to attach this persistent storage to a Docker container?
There are 2 ways:
- Attach a base directory data to the docker container
docker run -dit -p 85:80 -v /Users/dev/test/data:/usr/local/apache2/htdocs httpd
2. Docker volumes
docker run -dit -p 81:80 -v data:/usr/local/apache2/htdocs httpd
Some Commands:
- To list all volumes:
docker volume ls
2. Delete the volume
docker volume rm volume_id
3. To delete all volumes created in the system forcefully
docker volume rm $(docker volume ls -q ) -f
As you might have gone through both blogs I have a task:
Find out the command to delete all containers created in one go & let me know in the comment section.
Read about docker-compose an essential tool to deploy multitier applications:
Thanks for your Time! Follow & Clap 👏 to keep me Motivated.









