<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elizabeth W on Medium]]></title>
        <description><![CDATA[Stories by Elizabeth W on Medium]]></description>
        <link>https://medium.com/@xithira20?source=rss-2ac813bee6d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PMPucHP-vytEB3SsuHxlGA.jpeg</url>
            <title>Stories by Elizabeth W on Medium</title>
            <link>https://medium.com/@xithira20?source=rss-2ac813bee6d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 14:37:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xithira20/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker Best Practices & Optimization for Production]]></title>
            <link>https://medium.com/@xithira20/docker-best-practices-optimization-for-production-6c6c7182bb0b?source=rss-2ac813bee6d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c6c7182bb0b</guid>
            <category><![CDATA[devops-tool]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[docker-image]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Elizabeth W]]></dc:creator>
            <pubDate>Tue, 27 Jan 2026 16:33:45 GMT</pubDate>
            <atom:updated>2026-01-27T16:33:45.419Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F6kwCJm4GK_RHl6p" /></figure><p>A basic Docker setup for development typically involves creating a simple Dockerfile that builds and runs your application. While this approach works fine for local development and testing environments, it falls short when deploying to production.</p><h4><strong>Basic Development Setup:</strong></h4><p>Dockerfile content</p><pre>FROM node:18<br>WORKDIR /app<br>COPY . .<br>RUN npm install<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><p>This basic approach has several critical issues for production:</p><p>❌ Large image size (includes unnecessary development dependencies)</p><p>❌ Runs as root user (major security vulnerability)</p><p>❌ No layer caching optimization (slow rebuild times)</p><p>❌ Contains development tools and source code in final image</p><p>❌ No security scanning or hardening</p><p><strong>In production environments, you need to address:</strong></p><ul><li><strong>Security</strong>: Minimize attack surface, run as non-root, scan for vulnerabilities</li><li><strong>Performance</strong>: Optimize build times, reduce image size, efficient caching</li><li><strong>Reliability</strong>: Health checks, proper error handling, resource constraints</li><li><strong>Maintainability</strong>: Version pinning, clear documentation, reproducible builds</li></ul><p>This guide documents the journey from basic Docker setup to production-ready containerization, implementing industry best practices one step at a time.</p><h3>1. Multi-Stage Builds</h3><p>Multi-stage builds allow you to use multiple FROM statements in your Dockerfile. Each stage can serve a different purpose, and you can copy artifacts from one stage to another, leaving behind everything you don&#39;t need in the final image.</p><p>Multi-stage builds allow you to separate the build environment from the runtime environment, resulting in significantly smaller images. For React apps, this means your final image doesn’t include build tools, source code, or development dependencies.</p><p><strong>Impact:</strong></p><ul><li>Reduces image size by 60–80% on average</li><li>Faster deployment and pull times</li><li>Reduced attack surface (fewer tools = fewer vulnerabilities)</li><li>Separation of concerns between build and runtime</li></ul><h3>Implementation</h3><p><strong>Before (Single-Stage):</strong></p><pre>FROM node:18<br>WORKDIR /app<br>COPY . .<br>RUN npm install<br>RUN npm run build<br>EXPOSE 3000<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><p><strong>Image size:</strong> ~1.57GB</p><p><strong>After (Multi-Stage):</strong></p><pre># Build stage<br>FROM node:18-alpine AS build<br>WORKDIR /app<br>COPY package*.json ./<br>RUN npm ci --only=production<br>COPY . .<br>RUN npm run build</pre><pre># Production stage<br>FROM nginx:1.25-alpine<br>COPY --from=build /app/build /usr/share/nginx/html<br>EXPOSE 80<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><p><strong>Image size:</strong> ~94MB (94% size reduction)</p><h3>Best Practices</h3><ul><li>Name your stages with AS &lt;stage-name&gt; for clarity</li><li>Use slim or Alpine base images where possible</li><li>Only copy necessary artifacts between stages</li><li>Keep build dependencies in build stage only</li></ul><h3>2. Layer Caching Optimization</h3><p>Docker builds images by executing each instruction in a Dockerfile sequentially, creating a distinct layer for each step. Docker checks whether these instructions and the corresponding files have changed: If unchanged, it reuses the cached layer.</p><p>When a layer changes, all layers that come after it are also affected and need to be rebuilt. Proper layer ordering can reduce build times significantly.</p><p><strong>How Docker Caching Works:</strong></p><ol><li>Docker compares each instruction and its context</li><li>If nothing changed → reuse cached layer (instant)</li><li>If something changed → rebuild this layer + all subsequent layers</li><li>Dependencies change less frequently than source code</li></ol><h3>Implementation</h3><p><strong>Poor Caching (Rebuild everything on code change):</strong></p><pre>FROM node:18-alpine<br>WORKDIR /app</pre><pre># Copies everything first - invalidates cache on ANY file change<br>COPY . .<br>RUN npm install<br>RUN npm run build</pre><p><strong>Optimized Caching:</strong></p><pre>FROM node:18-alpine AS build<br>WORKDIR /app</pre><pre># Copy dependency files first (changes infrequently)<br>COPY package*.json ./<br>RUN npm ci</pre><pre># Copy source code last (changes frequently)<br>COPY . .<br>RUN npm run build</pre><h3>Advanced Caching with BuildKit</h3><p><strong>BuildKit</strong> is Docker’s next-generation build engine that replaces the old, slow, linear Docker builder. Think of it as <strong>Docker 2.0 for building images</strong> — <strong>5–10x faster builds</strong> with smarter caching.</p><p>Enable BuildKit for improved caching features:</p><pre>export DOCKER_BUILDKIT=1<br>docker build .</pre><p><strong>Cache Mounts for Package Managers:</strong></p><pre>FROM node:18-alpine AS build<br>WORKDIR /app</pre><pre>COPY package*.json ./</pre><pre># Use cache mount to persist npm cache across builds<br>RUN --mount=type=cache,target=/root/.npm \<br>    npm ci --prefer-offline #uses cached packages first</pre><pre>COPY . .<br>RUN npm run build</pre><p>This creates a persistent cache volume that mounts to npm’s cache directory and persists across builds until you delete it.</p><p><strong>Classic Docker — </strong>Layer invalidates if COPY . changes, Re-downloads all packages</p><p><strong>BuildKit Cache Mounts — </strong>npm cache survives source changes, Reuses 95% cached packages</p><h3>Layer Ordering Best Practices</h3><p>Putting the commands in your Dockerfile into a logical order is a great place to start.</p><p><strong>Optimal Order:</strong></p><ol><li>Base image selection</li><li>System dependencies installation</li><li>Package manager files (package.json)</li><li>Dependency installation</li><li>Application source code</li><li>Build commands</li><li>Configuration files</li></ol><h3>3. Using .dockerignore</h3><p>Similar in function to Git’s .gitignore, the .dockerignore file tells the Docker daemon which files and directories to exclude from the build context sent during a docker build command.</p><p>By carefully managing this context, you significantly speed up build times and prevent sensitive information from being unintentionally included in your image.</p><p><strong>Without .dockerignore:</strong></p><ul><li>Entire project directory sent to Docker daemon (including node_modules, .git, etc.)</li><li>Slower builds (transferring unnecessary files)</li><li>Potential security risks (accidentally including secrets)</li><li>Larger build context = more memory usage</li></ul><h3>Implementation</h3><p>Create .dockerignore in your project root:</p><pre># Dependencies<br>node_modules<br>npm-debug.log<br>yarn-error.log</pre><pre># Build outputs<br>build<br>dist<br>.next<br>out</pre><pre># Testing<br>coverage<br>.nyc_output<br>*.test.js<br>*.spec.js<br>__tests__</pre><pre># Version control<br>.git<br>.gitignore<br>.gitattributes</pre><pre># Environment files<br>.env<br>.env.local<br>.env.*.local</pre><pre># IDE<br>.vscode<br>.idea<br>*.swp<br>*.swo<br>*.swn<br>.DS_Store</pre><pre># Documentation<br>README.md<br>docs<br>*.md</pre><pre># CI/CD<br>.github<br>.gitlab-ci.yml<br>.travis.yml</pre><pre># Docker<br>Dockerfile<br>docker-compose.yml<br>.dockerignore</pre><h3>Best Practices</h3><ul><li>Mirror your .gitignore but tailor for Docker</li><li>Exclude all development files</li><li>Always exclude node_modules (install fresh in container)</li><li>Exclude documentation files</li><li>Exclude CI/CD configurations</li></ul><h3>4. Security: Running as Non-Root User</h3><p>Running containers with the least privilege necessary is a fundamental principle in container security. By using a non-root user, you adhere to the principle of least privilege, limiting the potential impact of security breaches.</p><p>Running containers as a non-root user substantially decreases the risk that container -&gt; host privilege escalation could occur.</p><p><strong>Security Risks of Running as Root:</strong></p><ul><li>If attacker compromises container, they have root access</li><li>Can modify system files and configurations</li><li>Easier privilege escalation to host system</li><li>Violates principle of least privilege</li><li>Fails security compliance requirements (CIS benchmarks, OWASP)</li></ul><h3>Implementation</h3><p><strong>Insecure (Default root user):</strong></p><pre>FROM nginx:alpine<br>COPY build /usr/share/nginx/html<br>EXPOSE 80<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><p><strong>Secure (Non-root user):</strong></p><pre>FROM nginx:alpine</pre><pre># Create non-root user/group with UID &gt; 10000<br>RUN addgroup -g 10001 -S appgroup &amp;&amp; \<br>    adduser -u 10000 -S appuser -G appgroup</pre><pre># Change ownership of required directories<br>RUN chown -R appuser:appgroup /usr/share/nginx/html /var/cache/nginx /var/run /var/log/nginx</pre><pre># Switch to non-root user<br>USER appuser</pre><pre>COPY --chown=appuser:appgroup build /usr/share/nginx/html<br>EXPOSE 8080<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><h3>Best Practices</h3><p>UIDs below 10,000 are a security risk on several systems, because if someone does manage to escalate privileges outside the Docker container their Docker container UID may overlap with a more privileged system user’s UID granting them additional permissions.</p><ul><li><strong>Use UIDs &gt;= 10000</strong> to avoid conflicts with system</li><li><strong>Set ownership</strong> with --chown flag when copying files</li><li><strong>Adjust permissions</strong> for directories your app needs to write to</li><li><strong>Change ports</strong> if needed (non-root users can’t bind to ports &lt; 1024)</li><li><strong>Test thoroughly</strong> to ensure app still functions correctly</li></ul><h3>5. Image Optimization &amp; Size Reduction</h3><p>Reducing the final image size by choosing appropriate base images, removing unnecessary files, and optimizing the build process.</p><ul><li><strong>Faster deployments</strong>: Smaller images = faster downloads and container starts</li><li><strong>Lower costs</strong>: Less storage and bandwidth usage</li><li><strong>Security</strong>: Fewer packages = smaller attack surface</li><li><strong>Better performance</strong>: Less to load into memory</li></ul><h3>Base Image Selection</h3><p><strong>Image Size Comparison:</strong></p><pre>node:18             → 993MB<br>node:18-slim        → 242MB  (75% smaller)<br>node:18-alpine      → 174MB  (82% smaller)<br>nginx:latest        → 187MB<br>nginx:alpine        → 41MB   (78% smaller)</pre><p><strong>Choose the Right Base:</strong></p><pre># Full image (includes build tools, docs, etc.)<br>FROM node:18</pre><pre># Slim image (removes non-essential packages)<br>FROM node:18-slim</pre><pre># Alpine image (minimal Linux distribution)<br>FROM node:18-alpine</pre><h3>Alpine Considerations</h3><p>Even though Alpine is lightweight, there are some known issues with performance for some technologies.</p><p><strong>When to use Alpine:</strong></p><ul><li>Simple Node.js apps</li><li>Static site serving</li><li>Microservices</li></ul><p><strong>When to avoid Alpine:</strong></p><ul><li>Apps with native dependencies (Python packages with C extensions)</li><li>Performance-critical applications</li><li>Complex build processes</li></ul><h3>Cleanup During Build</h3><pre>FROM node:18-alpine AS build<br>WORKDIR /app</pre><pre>COPY package*.json ./<br>RUN npm ci --only=production &amp;&amp; \<br>    # Clean npm cache<br>    npm cache clean --force &amp;&amp; \<br>    # Remove unnecessary files<br>    rm -rf /tmp/* /var/tmp/*</pre><pre>COPY . .<br>RUN npm run build</pre><pre># Production stage - only copy what&#39;s needed<br>FROM nginx:alpine<br>COPY --from=build /app/build /usr/share/nginx/html</pre><h3>Combining RUN Commands</h3><pre># Multiple layers (inefficient)<br>RUN apt-get update<br>RUN apt-get install -y package1<br>RUN apt-get install -y package2<br>RUN apt-get clean</pre><pre># Single layer (efficient)<br>RUN apt-get update &amp;&amp; \<br>    apt-get install -y package1 package2 &amp;&amp; \<br>    apt-get clean &amp;&amp; \<br>    rm -rf /var/lib/apt/lists/*</pre><h3>6. Version Pinning &amp; Reproducible Builds</h3><p>Using specific version tags for your base images instead of relying on the :latest tag. The :latest tag is mutable, meaning it can point to different versions of an image over time.</p><p>Pinning to a specific version ensures that your builds are deterministic and reproducible, eliminating a common source of “it works on my machine” issues.</p><h3>Implementation</h3><p><strong>Unpredictable Builds:</strong></p><pre>FROM node:latest<br>FROM nginx:latest</pre><p><strong>Predictable Builds:</strong></p><pre># Specific version tag<br>FROM node:18.19.0-alpine3.19<br>FROM nginx:1.25.3-alpine</pre><p><strong>Maximum Reproducibility (SHA256 digest):</strong></p><pre># Version + SHA256 digest<br>FROM node:18.19.0-alpine3.19@sha256:435dcad253bb5b7f347ebc69c8cc52de7c912eb7241098b920f2fc2d7843183d</pre><h3>Finding Image Digests</h3><p><strong>Image Digest</strong> = SHA256 cryptographic hash of your Docker image content. It’s the immutable fingerprint that guarantees exactly the same image every time.</p><pre># Get digest of an image<br>docker pull node:18-alpine<br>docker inspect node:18-alpine --format=&#39;{{.RepoDigests}}&#39;</pre><pre># Or use docker buildx <br>docker buildx imagetools inspect node:18-alpine</pre><h3>Version Pinning Strategy</h3><ol><li><strong>Development</strong>: Use major.minor tags (e.g., node:18)</li><li><strong>Staging</strong>: Use major.minor.patch tags (e.g., node:18.19.0)</li><li><strong>Production</strong>: Use tags + SHA256 digest</li></ol><h3>Package Dependencies</h3><p><strong>package.json:</strong></p><pre>{<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,     // ❌ Allows minor updates<br>    &quot;react&quot;: &quot;18.2.0&quot;,      // ✅ Locked version<br>  }<br>}</pre><p><strong>Use package-lock.json or yarn.lock:</strong></p><pre># Ensures exact versions installed<br>COPY package*.json ./<br>RUN npm ci  # ci = clean install (uses lockfile)</pre><h3>7. Health Checks</h3><p>Health checks allow Docker to test if a container is working properly. This is essential for orchestration systems (Kubernetes, Docker Swarm) to know when to restart or replace containers.</p><p>Without healthchecks, Docker can’t detect service failures. Implementing healthchecks ensures stability and security.</p><p><strong>Benefits:</strong></p><ul><li>Automatic container restarts on failure</li><li>Better load balancing</li><li>Graceful deployments with zero downtime</li><li>Early problem detection</li></ul><h3>Implementation</h3><p><strong>Dockerfile Health Check:</strong></p><pre>FROM nginx:alpine</pre><pre>COPY build /usr/share/nginx/html</pre><pre># Health check every 30 seconds<br>HEALTHCHECK --interval=30s \<br>            --timeout=10s \<br>            --start-period=5s \<br>            --retries=3 \<br>  CMD wget --no-verbose --tries=1 --spider <a href="http://localhost/">http://localhost/</a> || exit 1</pre><pre>EXPOSE 80<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><h3>Docker Compose Health Check</h3><pre>version: &#39;3.8&#39;</pre><pre>services:<br>  web:<br>    build: .<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http://localhost:3000/health&quot;]<br>      interval: 30s<br>      timeout: 10s<br>      retries: 3<br>      start_period: 40s</pre><h3>Health Check Best Practices</h3><ul><li>Keep checks lightweight (&lt; 100ms)</li><li>Check actual app logic, not just container running</li><li>Set appropriate intervals (30s is common)</li><li>Allow sufficient start-period for app initialization</li><li>Return 0 for success, 1 for failure</li></ul><h3>8. Security Scanning &amp; Image Hardening</h3><p>Scanning Docker images for known vulnerabilities and implementing security hardening techniques to minimize attack surface.</p><p>Inspecting the source code for any stylistic or programmatic error that can cause issues is called linting. Linting can help ensure that the Dockerfiles comply with the right practice.</p><h3>Vulnerability Scanning Tools</h3><p><strong>1. Docker Scout (Built-in):</strong></p><pre># Enable Docker Scout<br>docker scout quickview</pre><pre># Scan an image<br>docker scout cves myapp:latest</pre><pre># Get recommendations<br>docker scout recommendations myapp:latest</pre><p><strong>2. Trivy (Popular open-source):</strong></p><pre># Install Trivy<br>brew install aquasecurity/trivy/trivy</pre><pre># Scan image<br>trivy image myapp:latest</pre><pre># Scan with severity filter<br>trivy image --severity HIGH,CRITICAL myapp:latest</pre><pre># Generate report<br>trivy image --format json --output report.json myapp:latest</pre><h3>Security Hardening Techniques</h3><p><strong>1. Read-Only Filesystem:</strong></p><pre>FROM nginx:alpine</pre><pre>COPY build /usr/share/nginx/html</pre><pre># Make filesystem read-only except specific directories<br>VOLUME [&quot;/var/cache/nginx&quot;, &quot;/var/run&quot;]</pre><pre>USER nginx</pre><p><strong>Run with read-only flag:</strong></p><pre>docker run --read-only \<br>  -v /tmp/cache:/var/cache/nginx \<br>  -v /tmp/run:/var/run \<br>  myapp:latest</pre><p><strong>2. Drop Unnecessary Capabilities:</strong></p><pre># Drop all capabilities and add only what&#39;s needed<br>docker run --cap-drop=ALL \<br>  --cap-add=NET_BIND_SERVICE \<br>  myapp:latest</pre><p><strong>3. Resource Limits:</strong></p><pre># Limit CPU and memory<br>docker run --memory=&quot;512m&quot; \<br>  --cpus=&quot;1.0&quot; \<br>  myapp:latest</pre><p><strong>docker-compose.yml:</strong></p><pre>services:<br>  web:<br>    image: myapp:latest<br>    deploy: # swarm<br>      resources: # compose<br>        limits:<br>          cpus: &#39;1.0&#39;<br>          memory: 512M<br>        reservations:<br>          cpus: &#39;0.5&#39;<br>          memory: 256M</pre><p><strong>4. Security Options:</strong></p><pre>services:<br>  web:<br>    image: myapp:latest<br>    security_opt:<br>      - no-new-privileges:true<br>      - apparmor:docker-default<br>    cap_drop:<br>      - ALL<br>    cap_add:<br>      - NET_BIND_SERVICE<br>    read_only: true</pre><h3>Dockerfile Linting</h3><p><strong>Use Hadolint:</strong></p><pre># Install hadolint<br>brew install hadolint</pre><pre># Lint Dockerfile<br>hadolint Dockerfile</pre><pre># With specific rules<br>hadolint --ignore DL3018 --ignore DL3008 Dockerfile</pre><p><strong>Common Issues Detected:</strong></p><ul><li>Missing version pinning</li><li>Using latest tag</li><li>Unnecessary privileges</li><li>Poor layer caching</li><li>Security vulnerabilities</li></ul><h3>Common Pitfalls and Solutions</h3><h3>Pitfall 1: Slow Build Times</h3><p><strong>Problem:</strong></p><pre>FROM node:18<br>COPY . .              # Copies everything - poor caching<br>RUN npm install       # Reinstalls deps on every code change<br>RUN npm run build</pre><p><strong>Solution:</strong></p><pre>FROM node:18<br>COPY package*.json ./  # Copy deps file first<br>RUN npm ci             # Install deps (cached)<br>COPY . .               # Copy code last<br>RUN npm run build      # Only rebuilds when code changes</pre><h3>Pitfall 2: Large Image Sizes</h3><p><strong>Problem:</strong> 1.2GB image with unnecessary files</p><p><strong>Solution:</strong></p><ul><li>Use multi-stage builds</li><li>Use Alpine base images</li><li>Add .dockerignore file</li><li>Clean up in same RUN command</li></ul><h3>Pitfall 3: Security Vulnerabilities</h3><p><strong>Problem:</strong></p><ul><li>Running as root</li><li>Using :latest tags</li><li>No vulnerability scanning</li></ul><p><strong>Solution:</strong></p><ul><li>Create and use non-root user</li><li>Pin specific versions</li><li>Scan with Trivy,Snyk or other available tools</li><li>Implement health checks</li></ul><h3>Pitfall 4: Container Crashes in Production</h3><p><strong>Problem:</strong></p><ul><li>No health checks</li><li>No resource limits</li><li>No restart policy</li></ul><p><strong>Solution:</strong></p><pre>services:<br>  app:<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http://localhost/health&quot;]<br>    deploy:<br>      resources:<br>        limits:<br>          memory: 512M<br>      restart_policy:<br>        condition: on-failure</pre><h3>Checklist: Production-Ready Docker Setup</h3><h3>Build Optimization</h3><p>Multi-stage build implemented</p><ul><li>[ ] Layer caching optimized</li><li>[ ] .dockerignore file configured</li><li>[ ] Minimal base images (Alpine/slim)</li><li>[ ] Build artifacts cleaned up</li><li>[ ] Image size &lt; 100MB (for typical React app)</li></ul><h3>Security</h3><ul><li>[ ] Running as non-root user (UID &gt;= 10000)</li><li>[ ] Version pinning for base images</li><li>[ ] No secrets in image or ENV vars</li><li>[ ] Vulnerability scanning integrated</li><li>[ ] Read-only filesystem where possible</li><li>[ ] Unnecessary capabilities dropped</li><li>[ ] Security headers configured</li></ul><h3>Reliability</h3><ul><li>[ ] Health checks implemented</li><li>[ ] Resource limits defined</li><li>[ ] Restart policy configured</li><li>[ ] Proper error handling</li><li>[ ] Graceful shutdown implemented</li></ul><h3>Monitoring</h3><ul><li>[ ] Structured logging implemented</li><li>[ ] Log aggregation configured</li><li>[ ] Metrics exposed</li><li>[ ] Monitoring dashboard setup</li><li>[ ] Alerts configured</li></ul><h3>CI/CD</h3><ul><li>[ ] Automated builds on commit</li><li>[ ] Automated security scanning</li><li>[ ] Automated testing</li><li>[ ] Image tagged properly</li><li>[ ] Rollback strategy defined</li></ul><h3>Documentation</h3><ul><li>[ ] Dockerfile documented</li><li>[ ] Build instructions clear</li><li>[ ] Environment variables documented</li><li>[ ] Deployment guide created</li></ul><h3>Testing Your Docker Setup</h3><h3>Build and Run Locally</h3><pre># Build image<br>docker build -t myapp:test .</pre><pre># Check image size<br>docker images myapp:test</pre><pre># Scan for vulnerabilities<br>trivy image myapp:test</pre><pre># Run container<br>docker run -d -p 8080:8080 --name myapp-test myapp:test</pre><pre># Check health<br>docker ps<br>curl <a href="http://localhost:8080/health">http://localhost:8080/health</a></pre><pre># View logs<br>docker logs myapp-test</pre><pre># Check running user<br>docker exec myapp-test whoami  # Should not be root</pre><pre># Cleanup<br>docker stop myapp-test<br>docker rm myapp-test</pre><h3>Performance Testing</h3><pre># Test build time<br>time docker build -t myapp:test .</pre><pre># Test with cache<br>time docker build -t myapp:test .</pre><pre># Test startup time<br>time docker run --rm myapp:test</pre><pre># Memory usage<br>docker stats myapp-test</pre><h3>Conclusion</h3><p>Moving from a basic Docker setup to production-ready containerization requires implementing multiple best practices:</p><ol><li><strong>Multi-stage builds</strong> — Reduce image size</li><li><strong>Layer caching</strong> — Speed up builds</li><li><strong>.dockerignore</strong> — Faster builds, better security</li><li><strong>Non-root user</strong> — Eliminate privilege escalation risks</li><li><strong>Image optimization</strong> — Smaller, faster deployments</li><li><strong>Version pinning</strong> — Reproducible, predictable builds</li><li><strong>Health checks</strong> — Automatic failure detection</li><li><strong>Security scanning</strong> — Catch vulnerabilities early</li><li><strong>Resource limits</strong> — Prevent resource exhaustion</li><li><strong>Monitoring</strong> — Visibility into container behavior</li></ol><p>Each optimization compounds on the others, resulting in a Docker setup that is:</p><ul><li><strong>Secure</strong>: Minimized attack surface, non-root execution</li><li><strong>Fast</strong>: Quick builds and deployments</li><li><strong>Reliable</strong>: Self-healing with health checks</li><li><strong>Maintainable</strong>: Clear documentation and best practices</li></ul><p>The journey from development to production may seem daunting, but implementing these practices incrementally makes the process manageable and the results are worth the effort.</p><h3>Additional Resources</h3><h3>Official Documentation</h3><ul><li><a href="https://docs.docker.com/build/building/best-practices/">Docker Best Practices</a></li><li><a href="https://docs.docker.com/engine/security/">Docker Security</a></li><li><a href="https://docs.docker.com/build/building/multi-stage/">Multi-Stage Builds</a></li></ul><h3>Tools</h3><ul><li><a href="https://github.com/aquasecurity/trivy">Trivy</a> — Vulnerability scanner</li><li><a href="https://github.com/hadolint/hadolint">Hadolint</a> — Dockerfile linter</li><li><a href="https://github.com/slimtoolkit/slim">Docker Slim</a> — Image optimizer</li><li><a href="https://github.com/wagoodman/dive">Dive</a> — Image layer analyzer</li></ul><h3>Security References</h3><ul><li><a href="https://www.cisecurity.org/benchmark/docker">CIS Docker Benchmark</a></li><li><a href="https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html">OWASP Docker Security Cheat Sheet</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c6c7182bb0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>