DevLog

Docker for Developers: A Complete Guide

Master Docker fundamentals and learn how to containerize your applications for consistent development and deployment.

Tramdev
25 tháng 1, 2025
DevOps
#Docker#Containers#DevOps#Development
Docker for Developers: A Complete Guide

Docker for Developers: A Complete Guide

Docker has revolutionized how we develop, ship, and run applications. This comprehensive guide will walk you through Docker fundamentals and best practices for developers.

What is Docker?

Docker is a platform for developing, shipping, and running applications inside containers. Containers allow you to package an application with all its dependencies into a standardized unit.

Basic Concepts

Images

Docker images are read-only templates used to create containers. They contain the application code, runtime, libraries, and dependencies.

Containers

Containers are runnable instances of Docker images. They isolate applications from the host system.

Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image.

Creating Your First Dockerfile

# Use an official Node.js runtime as the base image
FROM node:18-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define the command to run the app
CMD ["npm", "start"]

Building and Running

Build the image:

docker build -t my-app .

Run the container:

docker run -p 3000:3000 my-app

Multi-Stage Builds

Use multi-stage builds to optimize image size:

# Build stage
FROM node:18-alpine AS builder

WORKDIR /app
COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

# Production stage
FROM nginx:alpine

COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Docker Compose

Use Docker Compose for multi-container applications:

# docker-compose.yml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db

  db:
    image: postgres:13
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Development Workflow

Development Container

Create a development container with all tools:

FROM node:18-alpine

# Install development tools
RUN apk add --no-cache git curl vim

# Create app user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

WORKDIR /app
USER nextjs

COPY --chown=nextjs:nodejs package*.json ./
RUN npm ci

COPY --chown=nextjs:nodejs . .

EXPOSE 3000
CMD ["npm", "run", "dev"]

Hot Reload

Configure volume mounting for hot reload:

docker run -v $(pwd):/app -p 3000:3000 my-dev-container

Best Practices

Security

  • Use official base images
  • Run containers as non-root user
  • Scan images for vulnerabilities
  • Keep images up to date

Performance

  • Use .dockerignore file
  • Leverage layer caching
  • Minimize image layers
  • Use multi-stage builds

Organization

  • Use descriptive names and tags
  • Clean up unused images and containers
  • Use Docker registries for sharing

Common Issues and Solutions

Port Conflicts

# Check what's using port 3000
lsof -i :3000

# Kill process using the port
kill -9 $(lsof -t -i :3000)

Container Won't Start

# Check container logs
docker logs <container-id>

# Run container interactively
docker run -it --entrypoint /bin/bash my-image

Image Size Too Large

# Check image layers
docker history my-image

# Use alpine images
FROM node:18-alpine

CI/CD Integration

Integrate Docker into your CI/CD pipeline:

# .github/workflows/deploy.yml
name: Deploy
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Build Docker image
      run: docker build -t my-app .

    - name: Run tests
      run: docker run my-app npm test

    - name: Deploy
      run: docker push my-registry/my-app:latest

Docker simplifies development workflows and ensures consistency across different environments. By mastering these concepts and best practices, you'll be able to containerize any application effectively.