π 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?
β
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