docker: containerize app
Basic understanding of Linux commands
Familiarity with command line interface
Basic knowledge of application development
A computer with at least 4GB RAM
Table of Contents
Introduction to Docker
Why Docker?
Docker Architecture
Installation Guide
Basic Docker Commands
Containerizing a Node.js Application
Docker Compose
Best Practices
Common Issues and Solutions
Practice Questions
Introduction to Docker
Docker is a platform that enables you to package applications and their dependencies into standardized units called containers. These containers can run consistently across different environments, solving the "it works on my machine" problem.
What is a Container?
A container is a lightweight, standalone package that includes everything needed to run a piece of software:
Application code
Runtime environment
System tools
System libraries
Settings
Why Docker?
Advantages
Consistency: Works the same way across all environments
Isolation: Applications run in isolated environments
Portability: Run anywhere Docker is installed
Scalability: Easy to scale applications
Resource Efficiency: Lightweight compared to virtual machines
Use Cases
Microservices architecture
Continuous Integration/Deployment
Development and testing
Application isolation
Multi-tenant applications
Docker Architecture
Key Components
Docker Engine
Docker daemon
REST API
Docker CLI
Docker Images
Read-only templates
Used to create containers
Stored in registries
Docker Containers
Running instances of images
Isolated environments
Can be started, stopped, moved, or deleted
Docker Registry
Repository for Docker images
Docker Hub (public)
Private registries
Installation Guide
For Ubuntu
# Update package index
sudo apt-get update
# Install prerequisites
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Set up stable repository
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
For Windows
Download Docker Desktop from docker.com
Run the installer
Follow the installation wizard
For macOS
# Using Homebrew
brew install docker
# Or download Docker Desktop from docker.com
Basic Docker Commands
- Working with Images
# List images
docker images
# Pull an image
docker pull node:18
# Remove an image
docker rmi node:18
# Build an image
docker build -t myapp:1.0 .
- Working with Containers
# List running containers
docker ps
# List all containers
docker ps -a
# Run a container
docker run -d -p 3000:3000 myapp
# Stop a container
docker stop <container_id>
# Remove a container
docker rm <container_id>
# View container logs
docker logs <container_id>
Containerizing a Node.js Application
Step 1: Create a Node.js Application
// app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Docker!');
});
app.listen(3000, () => {
console.log('App running on port 3000');
});
Step 2: Create a Dockerfile
# Use 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 application
CMD ["npm", "start"]
Step 3: Build and Run
# Build the image
docker build -t myapp:1.0 .
# Run the container
docker run -d -p 3000:3000 myapp:1.0
Docker Compose
Example: Node.js App with MongoDB
version: '3'
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
- DB_HOST=db
db:
image: mongo:latest
volumes:
- mongodb_data:/data/db
volumes:
mongodb_data:
- Image Optimization Use multi-stage builds Minimize layers Remove unnecessary files Use .dockerignore
- Security Don't run as root Scan images for vulnerabilities Use official base images Keep images updated
- Performance Use appropriate base images Implement caching Optimize Dockerfile Use volume mounts Common Issues and Solutions
- Container Won't Start Problem: Container exits immediately after starting Solution:
# Check container logs
docker logs <container_id>
# Run container in interactive mode
docker run -it myapp:1.0 /bin/sh
- Port Conflicts Problem: Port already in use Solution:
# Check if port is in use
sudo lsof -i :3000
# Use different port
docker run -d -p 3001:3000 myapp:1.0
- Port Conflicts Problem: Port already in use Solution:
# Check if port is in use
sudo lsof -i :3000
# Use different port
docker run -d -p 3001:3000 myapp:1.0
- Permission Issues Problem: Permission denied when accessing files Solution:
# Change file permissions
chmod -R 755 /app
# Use correct user in Dockerfile
USER node
Practice Questions
What is the difference between a Docker image and a container?
How do you share data between a Docker container and the host machine?
Explain the purpose of Docker Compose.
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
How do you implement health checks in Docker?
Additional Resources
Official Docker Documentation
Docker Hub
Docker GitHub Repository
Docker Community Forums
Conclusion
Docker has revolutionized the way we develop, ship, and run applications. By following this guide and practicing with the examples, you'll be well on your way to mastering Docker containerization.
Remember to:
Start with simple containers
Follow best practices
Keep security in mind
Practice regularly Happy containerizing! π³
π Ready to kickstart your tech career?
π [Apply to 10000Coders]
π [Learn Web Development for Free]
π [See how we helped 2500+ students get jobs]
Top comments (0)