👉 Here’s a typical flow you would follow when working with Docker:

1️⃣ Write a Dockerfile

Defines how to build your image.

Dockerfile
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "index.js"]

2️⃣ Build Docker image

Dockerfile
docker build -t my-app .

💡 This creates an image named my-app using the current directory .

3️⃣ List Docker images

Dockerfile
docker images

4️⃣ Run a container

Dockerfile
docker run -d -p 3000:3000 --name my-running-app my-app

💡 -d = detached mode, -p maps port

5️⃣ List running containers

Dockerfile
docker ps

To see all containers (including stopped):

Dockerfile
docker ps -a

6️⃣ Stop/start/remove the container

Dockerfile
docker stop my-running-app
docker start my-running-app
docker rm my-running-app

7️⃣ Remove an image

Dockerfile
docker rmi my-app

8️⃣ See container logs

Dockerfile
docker logs my-running-app

9️⃣ Enter into a running container

Dockerfile
docker exec -it my-running-app /bin/bash

1️⃣0️⃣ Docker Compose

Dockerfile
docker compose up --build

Stop

Dockerfile
docker compose down

📝 Interview Questions & Answers — Basic ➡ Pro

🟢 Basic

✅ Q 1. What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications using containers. Containers package code, dependencies, and environment so apps run reliably across different systems.

✅ Q 2. Difference between container and virtual machine

Container Virtual Machine
Lightweight, shares OS kernel Heavyweight, full OS inside
Starts in milliseconds Takes minutes
Less resource usage High resource usage

✅ Q 3. What is an image vs a container?

✅ Q 4. What is the purpose of a Dockerfile?

A Dockerfile contains instructions to build a Docker image — like base image, copy files, install packages, and start commands.

✅ Q 5. Common Docker commands?

🟡 Intermediate

✅ Q 6. What is docker-compose?

A tool to define and run multi-container Docker applications using docker-compose.yml. Lets you spin up entire systems with one command.

✅ Q 7. How do volumes work in Docker?

Volumes are used to persist data generated by and used by Docker containers, so data isn’t lost when containers stop or are deleted.

Dockerfile
docker run -v myvolume:/app/data my-app

✅ Q 8. What is the difference between CMD and ENTRYPOINT?

CMD ENTRYPOINT
Provides a default command but can be overridden Always runs and can’t be overridden easily
Example: CMD ["node", "index.js"] Example: ENTRYPOINT ["node", "index.js"]

✅ Q 9. How do you pass environment variables to a container?

Dockerfile
docker run -e MY_VAR=value my-app

or in Dockerfile

Dockerfile
ENV MY_VAR=value

🔴 Advanced/Pro

✅ Q 10. How do you optimize Docker images?

✅ Q 11. What is the difference between bind mount and volume?

Bind mount Volume
Maps the host path to the container Docker-managed location
The host controls data Docker controls data
Good for development Good for production data persistence

✅ Q 12. How do you make a multi-stage Docker build?

Dockerfile
FROM node:18 AS build
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html

✅ Q 13. How do you handle networking between containers?

Docker containers in the same network can access each other by container name (service name in Compose).

Dockerfile
services:
  app:
    build: .
    networks:
      - mynet
  db:
    image: mongo
    networks:
      - mynet

networks:
  mynet:

✅ Q 14. What happens when you docker stop vs docker kill?

docker stop docker kill
Sends SIGTERM, graceful stop Sends SIGKILL, force stop

✅ Q 15. How to clean up the Docker system?

Dockerfile
docker system prune

Removes unused containers, networks, and images.

⚡ Summary of Docker Workflow

Dockerfile
# Build image
docker build -t my-app .

# Run container
docker run -d -p 3000:3000 my-app

# Check containers
docker ps

# Stop container
docker stop <container_id>

# Remove container/image
docker rm <container_id>
docker rmi <image_id>

# Use docker compose
docker compose up --build
docker compose down