Containers solve the infamous "it works on my machine" problem that has plagued software development for decades. Before containers, applications would work perfectly in development but fail mysteriously when deployed to different environments due to varying dependencies, configurations, and system differences.
Containers are lightweight, portable packages that encapsulate everything an application needs to run: code, runtime libraries, dependencies, and configuration files. Think of them as standardized shipping containers for software - they ensure applications run consistently across any environment.
Docker, created by Solomon Hykes in 2013, is the leading containerization platform that revolutionized application deployment and management. It provides tools to build, ship, and run containers efficiently.
Docker containers act as isolated environments that share the host operating system's kernel, making them incredibly lightweight compared to traditional virtual machines.
Containers package applications with all dependencies, ensuring consistent behavior from development laptops to production servers. No more environment-specific bugs or configuration drift.
Unlike VMs that require separate operating systems, containers share the host OS kernel. This results in:
- Lower memory usage
- Faster startup times
- Higher density (more containers per host)
- Reduced infrastructure costs
Containers can be started, stopped, and scaled in seconds rather than minutes. This enables:
- Quick response to traffic spikes
- Efficient resource utilization
- Simplified rollbacks and updates
| Aspect | Docker Containers | Virtual Machines |
|---|---|---|
| Resource Usage | Lightweight, shares host OS | Heavy, requires full OS per VM |
| Startup Time | Seconds | Minutes |
| Isolation | Process-level | Hardware-level |
| Performance | Near-native | Some overhead |
| Use Case | Microservices, CI/CD | Legacy apps, strong isolation needs |
Before starting with Docker:
- Linux Fundamentals: Comfortable with command-line navigation and basic Linux commands
- Cloud Computing Basics: Understanding of servers, networking, and deployment concepts
- Virtual Machine Concepts: Knowledge of virtualization and its role in software deployment
- System Administration: Basic understanding of package management and system services
- Understand containers and isolation.
- Learn Docker features and commands.
- Deploy and scale applications with Docker.
sudo apt-get updateUpdates package lists to ensure latest software information
sudo apt-get install ca-certificates curl gnupgInstalls certificate authorities, curl for downloads, and GPG for security verification
sudo install -m 0755 -d /etc/apt/keyringsCreates secure directory for storing Docker's authentication keys
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpgDownloads and installs Docker's official GPG key for package verification
sudo chmod a+r /etc/apt/keyrings/docker.gpgEnsures the GPG key file is readable by all users
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullAdds Docker's official repository to system package sources
sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginInstalls Docker Engine and essential components
sudo systemctl status dockerChecks if Docker service is running properly
sudo usermod -aG docker ubuntuAdds current user to docker group, allowing Docker commands without sudo
Important: Log out and log back in for group changes to take effect.
docker run hello-worldThis command demonstrates Docker's complete workflow:
- Image Check: Docker searches for
hello-worldimage locally - Image Pull: Downloads image from Docker Hub if not found locally
- Container Creation: Creates container instance from image
- Execution: Runs container and displays greeting message
- Cleanup: Container stops after completing its task
docker imagesLists all images stored locally on the system
# Run container in foreground
docker run nginx
# Run container in background (detached)
docker run -d nginx
# Run container with custom name
docker run --name my-nginx nginx
# Run container with port mapping
docker run -p 8080:80 nginx# Show running containers only
docker ps
# Show all containers (running and stopped)
docker ps -a# Stop by container ID
docker stop CONTAINER_ID
# Stop by container name
docker stop my-nginx
# Force stop (kill)
docker kill CONTAINER_ID# Pull latest version
docker pull ubuntu
# Pull specific version
docker pull ubuntu:18.04
# Pull from private registry
docker pull registry.example.com/app:latest# Login to Docker Hub
docker login
# Tag image for push
docker tag local-image username/repository:tag
# Push to registry
docker push username/repository:tag# List all images
docker images
# Remove single image
docker rmi IMAGE_ID
# Remove multiple images
docker rmi IMAGE_ID1 IMAGE_ID2
# Remove unused images
docker image prune- Downloads an image only.
- Does not start a container.
docker pull nginx- Pulls the image if missing.
- Creates and starts a container.
docker run nginxIssue: Permission denied while trying to connect to Docker daemon
Got permission denied while trying to connect to the Docker daemon socket
Solution:
- Add user to docker group:
sudo usermod -aG docker $USER - Log out and log back in
- Restart Docker service:
sudo systemctl restart docker
Issue: Docker service fails to start
Job for docker.service failed because the control process exited with error code
Solution:
- Check service status:
sudo systemctl status docker - Check logs:
sudo journalctl -u docker.service - Restart service:
sudo systemctl restart docker - Enable auto-start:
sudo systemctl enable docker
Issue: Container exits immediately
docker run ubuntu
# Container starts then stops immediately
Solution: Provide a command to keep container running:
# Run with interactive terminal
docker run -it ubuntu bash
# Run with persistent process
docker run -d ubuntu tail -f /dev/nullIssue: Port already in use
Error: bind: address already in use
Solution:
- Check what's using the port:
sudo lsof -i :8080 - Use different port:
docker run -p 8081:80 nginx - Stop conflicting service:
sudo systemctl stop apache2
Issue: Image not found
Unable to find image 'myapp:latest' locally
Solution:
- Check image name spelling
- Verify image exists:
docker search myapp - Build image if it's custom:
docker build -t myapp . - Pull from correct registry:
docker pull registry.com/myapp
Issue: Running out of disk space
No space left on device
Solution:
# Remove stopped containers
docker container prune
# Remove unused images
docker image prune
# Remove unused volumes
docker volume prune
# Remove everything unused
docker system prune -aIssue: Container data not persisting
Data disappears when container is removed
Solution: Use volumes for persistent data:
# Create named volume
docker volume create mydata
# Run with volume mount
docker run -v mydata:/app/data nginxIssue: Cannot access application running in container
Connection refused when accessing localhost:8080
Solution:
- Verify port mapping:
docker ps - Check correct port binding:
docker run -p 8080:80 nginx - Test from inside container:
docker exec -it CONTAINER_ID curl localhost:80
- Always use official images when possible
- Regularly update base images
- Don't run containers as root user
- Use specific image tags instead of
latest - Scan images for vulnerabilities
- Use multi-stage builds to reduce image size
- Minimize layers in Dockerfiles
- Clean up package caches in same layer
- Use
.dockerignoreto exclude unnecessary files
- Set memory and CPU limits for containers
- Monitor container resource usage
- Use health checks for container monitoring
- Implement proper logging strategies
After mastering these basics:
- Learn Dockerfile: Create custom images
- Docker Compose: Manage multi-container applications
- Container Orchestration: Explore Kubernetes
- CI/CD Integration: Automate deployments
- Security Scanning: Implement vulnerability management
- Monitoring: Set up container observability
| Command | Purpose |
|---|---|
docker run |
Create and start container |
docker ps |
List containers |
docker stop |
Stop container |
docker images |
List images |
docker pull |
Download image |
docker push |
Upload image |
docker rmi |
Remove image |
docker exec |
Execute command in running container |
docker logs |
View container logs |
- Docker Official Documentation
- Docker Hub - Public container registry
- Play with Docker - Browser-based learning environment










