NarvikHub Logo

NARVIKHUB

Tools

Docker Containerization Guide

DevOps

2024-09-06

Docker Containerization: Building, Shipping, and Running Applications

Complete guide to Docker containerization, from basic concepts to production deployment strategies.

DockerContainerizationDevOpsMicroservices

Docker revolutionized application deployment by making it easy to package applications with all their dependencies. This guide covers everything from Docker basics to advanced orchestration and production best practices.

Docker Fundamentals

Core Docker concepts and commands:

# Pull and run an image

docker pull nginx:latest

docker run -d -p 80:80 nginx

# Container management

docker ps # List running containers

docker ps -a # List all containers

docker stop container_id # Stop container

docker rm container_id # Remove container

# Image management

docker images # List images

docker rmi image_id # Remove image

docker build -t myapp . # Build image from Dockerfile

Writing Effective Dockerfiles

Multi-stage Build Example

# Stage 1: Build

FROM node:18-alpine AS builder

WORKDIR /app

COPY package*.json ./

RUN npm ci --only=production

COPY . .

RUN npm run build

# Stage 2: Production

FROM node:18-alpine

WORKDIR /app

COPY --from=builder /app/dist ./dist

COPY --from=builder /app/node_modules ./node_modules

EXPOSE 3000

CMD ["node", "dist/index.js"]

Dockerfile Best Practices

• Use specific base image tags (not :latest)

• Minimize layers by combining RUN commands

• Order instructions from least to most frequently changing

• Use .dockerignore to exclude unnecessary files

• Run as non-root user for security

• Use COPY instead of ADD unless you need tar extraction

Docker Compose

Multi-container application with docker-compose.yml:

version: '3.8'

services:

web:

build: .

ports:

- "3000:3000"

environment:

- NODE_ENV=production

- DB_HOST=postgres

depends_on:

- postgres

- redis

postgres:

image: postgres:15

volumes:

- postgres_data:/var/lib/postgresql/data

environment:

- POSTGRES_PASSWORD=secret

redis:

image: redis:7-alpine

command: redis-server --appendonly yes

volumes:

- redis_data:/data

volumes:

postgres_data:

redis_data:

Container Networking

# Create custom network

docker network create myapp-network

# Run containers on same network

docker run -d --name db --network myapp-network postgres

docker run -d --name app --network myapp-network myapp

# Containers can communicate using container names

# Inside app container: postgres://db:5432/mydb

# List networks

docker network ls

# Inspect network

docker network inspect myapp-network

Volume Management

# Named volumes (managed by Docker)

docker volume create mydata

docker run -v mydata:/app/data myapp

# Bind mounts (host directory)

docker run -v /host/path:/container/path myapp

# Read-only volumes

docker run -v /host/path:/container/path:ro myapp

# Volume management

docker volume ls # List volumes

docker volume inspect mydata # Inspect volume

docker volume prune # Remove unused volumes

Production Best Practices

Security Scanning

Use `docker scan` or tools like Trivy to scan images for vulnerabilities.

Resource Limits

Set memory and CPU limits: `docker run -m 512m --cpus="1.5" myapp`

Health Checks

Implement HEALTHCHECK in Dockerfile to monitor container health.

Logging

Use centralized logging with drivers like json-file, syslog, or fluentd.

Published on 2024-09-06 • Category: DevOps

← Back to Blog

NarvikHub

Free online developer tools and utilities for encoding, formatting, generating, and analyzing data. No registration required - all tools work directly in your browser.

Built for developers, by developers. Privacy-focused and open source.

Popular Tools

Base64 Encoder/DecoderJSON FormatterURL Encoder/DecoderHTML FormatterHash GeneratorUUID Generator

Blog Articles

Base64 Encoding GuideURL Encoding Deep DiveUnderstanding JWT TokensRegular Expressions GuideView All Articles →

Developer Tools & Utilities

Base64 Encoder/DecoderJSON FormatterURL Encoder/DecoderHTML FormatterHash GeneratorUUID GeneratorQR Code GeneratorJWT DecoderTimestamp ConverterRegex TesterText Diff CheckerHex ConverterImage Base64 ConverterASN.1 DecoderCharles Keygen

Free online tools for Base64 encoding, JSON formatting, URL encoding, hash generation, UUID creation, QR codes, JWT decoding, timestamp conversion, regex testing, and more.

Privacy PolicyTerms of ServiceContact

© 2024 NarvikHub. All rights reserved.