Docker is a containerization platform that allows developers to package applications along with their dependencies into a single unit called a container.
Why use Docker?
✅ Ensures consistent environments across different machines.
✅ Lightweight & faster than virtual machines.
✅ Easy scaling of applications in microservices architectures.
What is the difference between Docker and a Virtual Machine (VM)?
Feature
Docker
Virtual Machine
Isolation
Uses containers to isolate apps
Uses hypervisor to run separate OS instances
Performance
Faster, lightweight
Slower, resource-intensive
Startup Time
Milliseconds
Minutes
Use Case
Ideal for microservices
Best for full OS emulation
What is a Docker image?
A Docker image is a read-only template containing everything needed to run an application, including:
Source code
Libraries & dependencies
Configuration files
A container is created from a Docker image using the docker run command.
What is a Docker container?
A Docker container is a running instance of a Docker image. It is:
✅ Lightweight (shares OS kernel)
✅ Isolated (has its own filesystem, network, and process space)
✅ Portable (can run on any system with Docker installed)
How do you create and run a Docker container?
To run a container from an image:
docker run -d --name myapp nginx
-d: Run in detached mode (background).
--name myapp: Name the container myapp.
nginx: Use the nginx image.
What is the purpose of the Dockerfile?
A Dockerfile is a script that contains instructions to build a Docker image.
Example Dockerfile:
FROM node:16WORKDIR /appCOPY . .RUN npm installCMD ["node", "app.js"]
FROM: Base image.
WORKDIR: Set working directory.
COPY: Copy files.
RUN: Execute commands (install dependencies).
CMD: Define the default command to run.
What are Docker volumes?
Docker volumes store persistent data outside a container's filesystem.
Types:
Anonymous Volumes: docker run -v /data nginx
Named Volumes: docker volume create mydata
Bind Mounts: docker run -v /host/path:/container/path nginx
How do you list running Docker containers?
Use the command:
docker ps
To list all containers (including stopped ones):
docker ps -a
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container applications.