Containers
- What is Docker and why use it?
Docker is a platform for building, running, and sharing containers—lightweight, isolated environments that package an application with its dependencies. It simplifies deployment by ensuring software runs the same way on any machine. - How do I write a Dockerfile?
A Dockerfile is a text file with instructions to build a Docker image. Start with a base image (FROM), copy your code (COPY), install dependencies (RUN), and set the startup command (CMD). - What is the difference between a container and a virtual machine?
Containers share the host OS kernel and isolate processes, while virtual machines run a full guest OS on virtualized hardware. Containers are lighter and faster to start; VMs offer stronger isolation. - How do I run a Docker container in the background?
Use the -d (detached) flag with docker run, e.g., 'docker run -d my-image'. The container runs in the background and prints the container ID. - What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. You describe services, networks, and volumes in a YAML file, then start everything with 'docker compose up'. - How do I reduce Docker image size?
Use smaller base images (like Alpine), combine RUN commands, remove unnecessary files, and use multi-stage builds to keep only what's needed in the final image.