Docker for Beginners: Containers Explained Simply, with Your First Dockerfile
You build a project on your laptop. It works. Your friend downloads it and gets ten errors: wrong Node version, missing database, different operating system. Multiply that by a team of 50 and a few servers, and you understand why companies adopted Docker.
What Docker does
Docker packages your application plus everything it needs (runtime, libraries, system tools, configuration) into a container. The container runs the same way on your laptop, a colleague's laptop, a test server and the cloud.
Analogy: a shipping container. The ship doesn't care whether it carries mangoes or machine parts; every container has the same shape and fits the same cranes. Docker containers have the same "shape" for servers.
Containers vs virtual machines
| Virtual machine | Container | |
|---|---|---|
| Includes | A full operating system | Just the app and its dependencies, sharing the host's kernel |
| Size | Gigabytes | Often megabytes |
| Start time | Minutes | Seconds |
| Use | Running different operating systems | Packaging and running apps consistently |
Key terms
- Image: a read-only blueprint for your app (like a class)
- Container: a running instance of an image (like an object)
- Dockerfile: instructions to build an image
- Registry: where images are stored and shared (Docker Hub, GitHub Container Registry, cloud registries)
- Volume: storage that survives when a container is removed (for databases)
- Port mapping: connecting a port on your machine to a port in the container
Your first Dockerfile (Node.js app)
For a simple Express app with package.json and index.js:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Then:
docker build -t my-api .
docker run -p 3000:3000 my-api
Open http://localhost:3000. Your app is running inside a container.
Add a .dockerignore file with node_modules and .env so they aren't copied into the image.
Commands you'll use often
| Command | What it does |
|---|---|
docker build -t name . | Build an image from a Dockerfile |
docker run -p 3000:3000 name | Run a container |
docker ps / docker ps -a | List running / all containers |
docker logs <id> | See a container's output |
docker exec -it <id> sh | Open a shell inside a container |
docker stop <id> | Stop a container |
docker images | List images |
docker compose up -d | Start everything in compose.yaml |
Docker Compose: running an app with a database
Real apps have several parts. A compose.yaml for a MERN-style backend:
services:
api:
build: .
ports:
- "3000:3000"
environment:
- MONGO_URL=mongodb://db:27017/app
depends_on:
- db
db:
image: mongo:7
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
Run docker compose up -d and both start together, connected on a private network.
Good habits
- Use small, official base images (for example
-alpineor-slimvariants) - Never put passwords or keys in the Dockerfile; use environment variables or secrets
- One main process per container
- Pin versions (
node:20-alpine, notnode:latest) so builds are repeatable - Commit your Dockerfile and compose file to Git (Git and GitHub for beginners)
Where Docker fits in a DevOps career
- Linux and networking basics
- Git and a programming or scripting language
- Docker ← you are here
- CI/CD: build and push images automatically (GitHub Actions, Jenkins)
- Cloud: deploy containers on AWS, Azure or GCP
- Kubernetes: run many containers reliably at scale
- Monitoring and infrastructure as code
Full path: DevOps roadmap for beginners. Not sure between web development and DevOps? Read MERN vs DevOps.
Learn it with us
Our live online DevOps course for Shivamogga students covers Docker hands-on, from your first Dockerfile to deploying containers through a CI/CD pipeline. See all courses.
Wbzard Labs
Want this done for your business?
We help founders in Bangalore and Shivamogga launch with a website, social media, SEO and ads that work together. Tell us about your business and get a free launch plan.

