Docker Fundamentals: From Zero to Deployment
A comprehensive beginner's guide to Docker. Learn containers, images, and how to deploy your first application.
Prerequisites
- ✓Basic command line knowledge
- ✓Familiarity with software applications
Docker Fundamentals: From Zero to Deployment
Docker has revolutionized how we build, ship, and run applications. In this tutorial, you'll learn Docker from the ground up and deploy your first containerized application.
What is Docker?
Docker is a platform that packages applications and their dependencies into containers - lightweight, standalone units that run consistently across different environments.
Why Use Docker?
- Consistency - "Works on my machine" becomes "works everywhere"
- Isolation - Each container runs independently
- Efficiency - Lighter than virtual machines
- Portability - Deploy anywhere Docker runs
Prerequisites Check
Before we start, make sure you have:
✅ Docker installed (Install Docker)
✅ A terminal/command prompt
✅ A text editor
Verify Docker is installed:
docker --version
# Should output: Docker version 24.x.x
Step 1: Understanding Containers
Think of containers as lightweight virtual machines that share the host OS kernel.
Container vs Virtual Machine:
- VM: Includes entire OS, heavy (GBs), slow to start
- Container: Shares OS kernel, lightweight (MBs), starts instantly
Step 2: Your First Container
Let's run a simple container:
docker run hello-world
What happened?
- Docker checked for the
hello-worldimage locally - Didn't find it, so downloaded from Docker Hub
- Created a container from the image
- Ran the container
- Container printed a message and exited
Step 3: Running an Interactive Container
Let's run Ubuntu in a container:
docker run -it ubuntu bash
Flags explained:
-i= Interactive (keep STDIN open)-t= Allocate a terminalbash= Command to run
You're now inside an Ubuntu container! Try:
# Check the OS
cat /etc/os-release
# Exit the container
exit
Step 4: Creating Your First Dockerfile
A Dockerfile is a recipe for building Docker images.
Create a file named Dockerfile:
# Start from Node.js base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Start command
CMD ["npm", "start"]
Dockerfile Instructions Explained
FROM- Base image to start fromWORKDIR- Set the working directoryCOPY- Copy files from host to containerRUN- Execute commands during buildEXPOSE- Document which port the app usesCMD- Default command when container starts
Step 5: Building an Image
Create a simple Node.js app:
package.json:
{
"name": "docker-demo",
"version": "1.0.0",
"scripts": {
"start": "node server.js"
}
}
server.js:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello from Docker!\n");
});
server.listen(3000, "0.0.0.0", () => {
console.log("Server running on port 3000");
});
Build the image:
docker build -t my-node-app .
-t= Tag (name) for the image.= Build context (current directory)
Step 6: Running Your Container
Run your newly built image:
docker run -p 8080:3000 my-node-app
-p 8080:3000= Map port 8080 on host to port 3000 in container
Visit http://localhost:8080 - you'll see "Hello from Docker!"
Step 7: Essential Docker Commands
List running containers
docker ps
List all containers (including stopped)
docker ps -a
Stop a container
docker stop <container-id>
Remove a container
docker rm <container-id>
List images
docker images
Remove an image
docker rmi <image-name>
View container logs
docker logs <container-id>
Execute command in running container
docker exec -it <container-id> bash
Step 8: Docker Compose (Bonus)
Docker Compose manages multi-container applications.
Create docker-compose.yml:
version: "3.8"
services:
app:
build: .
ports:
- "8080:3000"
environment:
- NODE_ENV=production
Run with:
docker-compose up
Best Practices
- Use official base images - More secure and maintained
- Minimize layers - Combine RUN commands when possible
- Use .dockerignore - Exclude unnecessary files
- Don't run as root - Create a user in your Dockerfile
- Keep images small - Use alpine variants when possible
Common Issues & Solutions
Issue: Port already in use
# Find process using the port
lsof -i :8080
# Kill the process or use different port
docker run -p 8081:3000 my-node-app
Issue: Permission denied
# Add your user to docker group (Linux)
sudo usermod -aG docker $USER
Next Steps
Now that you understand Docker basics, explore:
- Docker volumes - Persist data
- Docker networks - Container communication
- Multi-stage builds - Optimize image size
- Docker Hub - Share your images
- Kubernetes - Orchestrate containers at scale
Summary
You've learned:
- ✅ What Docker is and why it's useful
- ✅ How to run containers
- ✅ Creating Dockerfiles
- ✅ Building and running custom images
- ✅ Essential Docker commands
- ✅ Docker Compose basics
Congratulations! You're now ready to dockerize your applications! 🐳