DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on • Edited on

DOCKER REVISION

Your notes are good for interview revision. Here is a corrected and more interview-friendly version.


Docker Notes (Interview Revision)

1. Docker

  • Docker is an open-source containerization platform.
  • It is used to build, package, and run applications in containers.
  • Why is Docker lightweight?

    • Docker shares the host operating system's kernel.
    • Unlike Virtual Machines, Docker does not require a separate guest OS, so it uses less CPU, RAM, and storage.

2. Dockerfile

  • A Dockerfile is a text file that contains instructions and commands to build a Docker image.
  • Common instructions:

    • FROM
    • WORKDIR
    • COPY
    • RUN
    • EXPOSE
    • CMD
    • ENTRYPOINT

3. Docker Image

  • A Docker image is a read-only template.
  • It contains the application, dependencies, libraries, and configuration.
  • Images are used to create Docker containers.

4. Docker Container

  • A Docker container is a running instance of a Docker image.
  • It is isolated from the host and other containers.
  • Multiple containers can be created from the same image.

5. Docker Volume

  • Docker volumes are used to persist container data.
  • By default, if a container is removed, its data is lost.
  • Volumes store data outside the container, so the data remains even if the container is deleted.

6. Docker Swarm

  • Docker Swarm is Docker's native container orchestration tool.
  • It is used to:

    • Manage multiple containers
    • Scale applications
    • Provide load balancing
    • High availability
  • Similar purpose as Kubernetes, but Kubernetes is more powerful and widely used.


7. Docker Registry

  • A Docker Registry stores Docker images.
  • It allows developers to push and pull images.
  • Examples:

    • Docker Hub
    • Nexus Repository
    • JFrog Artifactory
    • Amazon ECR

Most Used Docker Commands

docker images                  # List all images

docker ps                      # Running containers

docker ps -a                   # All containers

docker logs <container-id>     # View container logs

docker start <container-id>    # Start container

docker stop <container-id>     # Stop container

docker restart <container-id>  # Restart container

docker exec -it <container-id> bash
# Enter running container

docker rm <container-id>       # Remove container

docker rmi <image-id>          # Remove image

docker top <container-id>      # Running processes inside container

docker inspect <container-id>  # Detailed information

docker pull <image-name>       # Download image

docker push <image-name>       # Push image to registry
Enter fullscreen mode Exit fullscreen mode

Docker Architecture

                +----------------------+
                |   Docker CLI         |
                +----------+-----------+
                           |
                           v
                +----------------------+
                | Docker Engine (API)  |
                +----------+-----------+
                           |
          +----------------+----------------+
          |                                 |
          v                                 v
+--------------------+          +----------------------+
| Docker Images      |          | Docker Containers    |
| (Read-only)        | -------> | (Running Instance)   |
+--------------------+          +----------------------+
                           |
                           v
                  +------------------+
                  | Docker Volumes   |
                  | (Persistent Data)|
                  +------------------+
Enter fullscreen mode Exit fullscreen mode

Docker Engine Components

  • Docker CLI – Used to execute Docker commands.
  • Docker API – Accepts requests from the CLI.
  • containerd – Manages the lifecycle of containers (create, start, stop, delete).

Interview Points (Remember These)

  1. Docker is lightweight because it shares the host OS kernel.
  2. Dockerfile → Docker Image → Docker Container (this flow is asked very often).
  3. Images are read-only, while containers are running instances.
  4. Volumes provide persistent storage even after containers are removed.
  5. Docker Swarm is Docker's orchestration tool, whereas Kubernetes is the industry standard.

This is enough for answering most Docker interview questions for Java, Spring Boot, and DevOps roles.

============================

Bahut achha question hai. Docker Volume database nahi hota, balki storage hota hai. Isko AWS se relate karke samajho.

Without Volume

Docker Container
----------------------
App
MySQL
Database Files
----------------------
Enter fullscreen mode Exit fullscreen mode

Suppose MySQL container me data save hua.

INSERT INTO employee VALUES(1,'Rahul');
Enter fullscreen mode Exit fullscreen mode

Ab agar tumne container delete kar diya:

docker rm mysql-container
Enter fullscreen mode Exit fullscreen mode

❌ Pura data bhi delete ho jayega.


With Docker Volume

          Docker Volume
      (/var/lib/docker/volumes)
             |
             |
     +------------------+
     | Database Files   |
     +------------------+
             ^
             |
      MySQL Container
Enter fullscreen mode Exit fullscreen mode

Ab MySQL ka data volume me store hoga.

Container delete ho jaye:

docker rm mysql-container
Enter fullscreen mode Exit fullscreen mode

✔️ Data volume me safe rahega.

Naya container banaoge aur same volume attach kar doge:

docker run -v mysql-data:/var/lib/mysql mysql
Enter fullscreen mode Exit fullscreen mode

To purana sara database wapas mil jayega.


AWS Example

Docker AWS Equivalent
Docker Volume EBS (mostly)
Container EC2 Instance
Image AMI
Docker Registry ECR / Docker Hub

Volume EBS jaisa hai.

  • EC2 = delete ho sakta hai
  • EBS = data bach jata hai
  • Waise hi

    • Container = delete ho sakta hai
    • Volume = data bach jata hai

RAM hai kya?

Nahi.

RAM temporary memory hoti hai.

Docker Volume disk storage hai (SSD/HDD), RAM nahi.

RAM (Temporary)
        ↓
Application Running
        ↓
Write Data
        ↓
Docker Volume (Disk)
Enter fullscreen mode Exit fullscreen mode

Database aur Volume ka relation

MySQL/PostgreSQL jaise database apna data files me likhte hain.

Example:

MySQL
   |
   | writes data
   v
/var/lib/mysql
   |
   v
Docker Volume
Enter fullscreen mode Exit fullscreen mode

Volume sirf storage provide karta hai.

Database us storage me apni files save karta hai.


Interview Line (Yaad rakhna)

Docker Volume is a persistent storage mechanism. It is not a database. It stores application or database files outside the container so that data remains available even if the container is stopped or deleted. It is similar to an AWS EBS volume attached to an EC2 instance.

==================================
Short answer:

Jab tak tum delete nahi karte, tab tak data save rahta hai. Iska koi fixed time limit (30 days, 1 year, etc.) nahi hota.

Storage Data kitne din rahega? Kab delete hoga?
Docker Volume Unlimited Jab volume delete (docker volume rm) karoge ya disk fail ho jaye
AWS EBS Unlimited Jab EBS volume delete karoge (ya EC2 terminate ke time "Delete on termination" enabled ho)
AWS EFS Unlimited Jab EFS file system delete karoge

Docker Volume

  • Data host machine ki disk me store hota hai.
  • Container 1 saal baad bhi delete ho jaye, agar volume delete nahi hua hai to data rahega.
  • Agar host machine ki disk crash ho gayi aur backup nahi tha, to data bhi chala sakta hai.

AWS EBS

  • Ye EC2 ke liye block storage hai.
  • EC2 stop/start karne se data nahi jata.
  • EC2 terminate karne par bhi data bach sakta hai agar "Delete on termination" disabled ho.
  • Snapshots lekar data ko aur bhi safely store kar sakte ho.

AWS EFS

  • Ye network file system hai.
  • Multiple EC2 instances ek hi time par use kar sakte hain.
  • Data tab tak rahega jab tak tum EFS delete nahi karte.
  • High durability ke liye AWS automatically multiple Availability Zones me data replicate karta hai.

Interview Answer

Docker Volume, AWS EBS, and AWS EFS do not have an expiry period. Data remains stored until it is explicitly deleted. Docker Volume stores data on the host machine, EBS stores persistent block storage for EC2, and EFS provides shared persistent file storage that can be accessed by multiple EC2 instances.

Simple answer:

Na RAM hota hai, na ROM.

Ye persistent disk storage hota hai (SSD/HDD).

Comparison:

Memory Data Save? Fast? Example
RAM ❌ No (Power off → Data lost) Very Fast Running application memory
ROM ✔️ Yes Read-only BIOS/Firmware
Disk Storage (SSD/HDD) ✔️ Yes Slower than RAM Docker Volume, EBS, EFS

Docker Volume

  • ❌ RAM nahi
  • ❌ ROM nahi
  • ✔️ Host machine ki disk (SSD/HDD) par stored hota hai.

AWS EBS

  • ✔️ Persistent block storage (SSD/HDD based).

AWS EFS

  • ✔️ Persistent network file storage.

Easy Analogy

  • RAM = Table (jis par abhi kaam kar rahe ho)
  • Disk (Docker Volume/EBS/EFS) = Almirah (kaam khatam hone ke baad files yahan rakhte ho)
  • ROM = Printed instruction manual (normally sirf read karte ho, change nahi karte)

Interview Line

Docker Volume, AWS EBS, and AWS EFS are persistent disk storage systems. They are neither RAM nor ROM. They store data on SSD/HDD so the data remains available even after the application or container stops.

===========================================

# 🐳 DOCKER CONCEPTS

---

## Installation Commands

In Ubuntu 16.04 or any latest version, Docker installation commands:

```bash
sudo apt-get -f install
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
sudo apt-get update
apt-cache search docker-ce
sudo apt-get install docker-ce
Enter fullscreen mode Exit fullscreen mode

Sample output:

docker-ce - Docker: the open-source application container engine
Enter fullscreen mode Exit fullscreen mode

Docker Container Commands

Docker container is a running instance of an image. A container combined only libraries and settings required to make the application work. It is a lightweight and portable encapsulated environment for an application.

Run Docker Container:

docker run hello-world
docker run -it centos
docker ps
docker ps -a
docker inspect <containerid>
docker stop <containerid>
docker rm <containerid>
Enter fullscreen mode Exit fullscreen mode

Docker Images

An image is an inert, immutable file that’s essentially a snapshot of a container.

docker images
docker pull centos
docker rmi centos
Enter fullscreen mode Exit fullscreen mode

Docker Data Volumes

In Docker, data volumes are useful for managing data with Docker containers and host machines.

Create Data Volume Container

docker create -v /data --name data_container centos
Enter fullscreen mode Exit fullscreen mode

Use Data Volume in New Container

docker run -it --volumes-from data_container centos /bin/bash
cd /data
echo "Hello Docker" > file.txt
Enter fullscreen mode Exit fullscreen mode

Verify Files on Data Volume

docker run -it --volumes-from data_container centos /bin/bash
Enter fullscreen mode Exit fullscreen mode

Share Host Volume with Container

docker run -it -v /var/www:/data centos
Enter fullscreen mode Exit fullscreen mode

Working with Dockerfile

docker build -t image_name .
docker build -t image_name -f /path/to/Dockerfile .
Enter fullscreen mode Exit fullscreen mode

Dockerfile Directives

FROM ubuntu

LABEL maintainer="kiran"
LABEL vendor="org"
LABEL com.example.version="0.0.1"

RUN apt-get update && apt-get install -y \
automake \
build-essential \
curl

COPY html/* /var/www/html/
COPY *.conf /etc/apache2/sites-available/

WORKDIR /opt

CMD ["apachectl", "-D", "FOREGROUND"]

EXPOSE 80
EXPOSE 443

ENV PATH=$PATH:/usr/local/pgsql/bin/ \
PG_MAJOR=9.6.0

VOLUME ["/data"]
Enter fullscreen mode Exit fullscreen mode

Manage Ports in Docker

docker run -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode

Docker POCs

Running MySQL as Docker

docker pull mysql/mysql-server
docker pull mysql/mysql-server:5.5
docker run --name=mysql1 -d mysql/mysql-server
docker run --name=mysql1 -d mysql/mysql-server:5.5
docker logs mysql1
docker run --name=mysql1 -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server
docker run --name=mysql1 -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server:5.5
docker logs mysql1 2>&1 | grep GENERATED
docker exec -it mysql1 mysql -u root -p
docker ps -a
Enter fullscreen mode Exit fullscreen mode
ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin@123';
CREATE USER 'newUser'@'%' IDENTIFIED BY 'newPass123';
ALTER USER 'newUser'@'%' IDENTIFIED WITH mysql_native_password BY 'newPass123';
GRANT ALL PRIVILEGES ON *.* TO 'newUser'@'%' WITH GRANT OPTION;
Enter fullscreen mode Exit fullscreen mode

Run Tomcat as Docker

docker run -it --rm tomcat:8.0
docker run -it --rm -p 8888:8080 tomcat:8.0
Enter fullscreen mode Exit fullscreen mode

Dockerfile for WAR:

FROM tomcat:8.0.43-jre8
ADD webdemodocker.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD chmod +x /usr/local/tomcat/bin/catalina.sh
CMD ["catalina.sh", "run"]
Enter fullscreen mode Exit fullscreen mode
sudo docker build -t webdemo .
sudo docker run -i -t -d -p 8080:8080 webdemo
Enter fullscreen mode Exit fullscreen mode

Dockerfile for Nginx

FROM nginx
COPY index.html /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

index.html:

This is hello world to Nginx
Enter fullscreen mode Exit fullscreen mode
docker build -t mynginx .
docker run -it -p 8080:80 mynginx
docker run -it -p 8081:80 mynginx
Enter fullscreen mode Exit fullscreen mode

Spring Boot Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} springbootapp.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","springbootapp.jar"]
EXPOSE 8080
Enter fullscreen mode Exit fullscreen mode
sudo docker build -t myspringbootapp --build-arg JAR_FILE="springbootapp.jar" .
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

===========================================

Top comments (0)