Docker  

πŸš€ Docker Workflow with Important Commands

πŸ‘‰ Here’s a typical flow you would follow when working with Docker:

1️⃣ Write a Dockerfile

Defines how to build your image.

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

2️⃣ Build Docker image

docker build -t my-app .

πŸ’‘ This creates an image named my-app using the current directory .

3️⃣ List Docker images

docker images

4️⃣ Run a container

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

πŸ’‘ -d = detached mode, -p maps port

5️⃣ List running containers

docker ps

To see all containers (including stopped):

docker ps -a

6️⃣ Stop/start/remove the container

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

7️⃣ Remove an image

docker rmi my-app

8️⃣ See container logs

docker logs my-running-app

9️⃣ Enter into a running container

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

1️⃣0️⃣ Docker Compose

docker compose up --build

Stop

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?

  • Image: A snapshot/template to create containers (blueprint)

  • Container: A running instance of an image (actual app running)

βœ… 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?

  • docker build → Build image

  • docker run → Run the container

  • docker ps → List containers

  • docker stop → Stop the container

  • docker exec → Run commands inside the container

  • docker images → List images

  • docker rmi → Remove image

🟑 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.

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?

docker run -e MY_VAR=value my-app

or in Dockerfile

ENV MY_VAR=value

πŸ”΄ Advanced/Pro

βœ… Q 10. How do you optimize Docker images?

  • Use smaller base images (e.g., alpine)

  • Combine RUN commands to reduce layers

  • Use .dockerignore to exclude unnecessary files

  • Multi-stage builds to separate build tools from runtime

βœ… 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?

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).

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?

docker system prune

Removes unused containers, networks, and images.

⚑ Summary of Docker Workflow

# 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