Introduction: "It Works on My Machine"
Every developer has said it: "It works on my machine!" But when the app goes to production, it crashes because the server has a different version of Node.js, or a missing library. Docker solves this. It packages your app and its dependencies into a single container that runs consistently everywhere.
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers. A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software.
Core Concept: Image vs Container
- Image: A blueprint. A read-only template. You can build an image from a Dockerfile.
- Container: A running instance of an image.
Step-by-Step Execution
1. Install Docker Desktop
Download from docker.com and install. It includes Docker Engine and CLI.
2. Create a Simple App
Create a folder with a simple `index.html` file. And a `Dockerfile` (no extension).
FROM nginx:alpine
COPY . /usr/share/nginx/html
EXPOSE 80
3. Build the Image
Open your terminal in that folder and run:
docker build -t my-website .
4. Run the Container
docker run -d -p 8080:80 my-website
Open your browser to `http://localhost:8080`. Your site is running inside a container!
Common Mistakes Beginners Make
- Forgetting to add a `.dockerignore` file: This prevents copying unnecessary files like `node_modules`.
- Running containers as root: This is a security risk. Use a non-root user in your Dockerfile.
- Too many layers: Combine commands (e.g., `RUN apt-get update && apt-get install`) to reduce image size.
Practical Exercise (Don't Skip This)
Take a simple HTML/CSS/JS project and create a Dockerfile for it. Build and run it. If you get stuck, email me at digisoneglobal@gmail.com.
Frequently Asked Questions
Q: Is Docker a virtual machine?
A: No. Containers share the host OS kernel. They are much lighter and faster than VMs.
Q: What is Docker Compose?
A: A tool for defining and running multi-container Docker applications using a YAML file.
Tools & Resources Mentioned
- Docker Docs - Official documentation.
- Docker Hub - Pre-built images.
- Play with Docker - Online playground.
Conclusion & Next Steps
Docker is an essential skill for modern developers. It makes deployment predictable and easy. Next, we'll dive into accessibility.