Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Operating Systems»Linux»How to Optimize Dockerfile for a Lean, Secure Production

    How to Optimize Dockerfile for a Lean, Secure Production

    April 3, 2025

    Hi friends! If you’re using Docker, you know it’s like a magic box for your app—it runs the same everywhere, from your laptop to a big server. But the file that makes this box, the Dockerfile, needs some care. If it’s not done right, your app can become slow, heavy, or even unsafe when real users start using it in production. Don’t worry, I’ll show you how to make it small, fast, and secure in simple steps. Plus, I’ll give an example you can try!

    Why Bother Optimizing?

    In production, your app should be quick to start, use less space, and stay safe from hackers. A bad Dockerfile can make your container fat with extra files or risky to run. Let’s fix that, step by step, in a way anyone can understand.

    1. Pick a Small Base Image

    Every Dockerfile starts with a base image—like the foundation of a house. Big images like Ubuntu have too much stuff we don’t need. Instead, use something small like Alpine Linux. It’s tiny but does the job well.
    General Uses:

    
    FROM ubuntu:latest
    
    

    Better Option:

    
    FROM alpine:latest
    
    

    Alpine is just 5 MB—Ubuntu is over 100 MB! Smaller means faster and safer.

    2. Use Multi-Stage Builds to Cut Junk

    When you build an app, you need tools—like a carpenter needs a hammer. But once the app is ready, you don’t need those tools running it. Multi-stage builds let you use tools in one step, then throw them away for the final container. This keeps it light.

    For example, with a Node.js app, you build it first, then copy only the final files to a small image. No extra baggage!

    3. Don’t Run as Root

    By default, Docker runs as “root”—like giving full keys to your house. If a hacker gets in, they control everything. Better to use a normal user. It’s like locking extra doors for safety. Here’s how you can do it:

    • Create a User: Add a new user in your Dockerfile with a command like RUN adduser -D myuser. The -D means no password, so it’s simple.
    • Switch to That User: Use USER myuser before your app runs. This tells Docker to stop using root and use your new user instead.
    • Fix File Permissions: If your app needs to read or write files, make sure your user owns them. Add RUN chown -R myuser /app after copying files.
    • Test It: Build and run your container, then check with docker exec -it [container_name] whoami. It should say “myuser,” not “root.”

    Doing this keeps your app safer—like not leaving your house keys under the mat!

    4. Speed Up Builds with Smart Order

    Docker builds in layers. If you put things that change a lot—like your code—at the end, it reuses earlier steps and saves time. So, install dependencies first, then copy your app code.

    5. Fix Versions for No Surprises

    If you write FROM node:latest, the image might update and break your app later. Use a fixed version like node:18-alpine. It’s like sticking to one recipe—no sudden changes!

    Example: Optimizing a Node.js App

    Let’s take a simple Node.js app with two files: package.json (for dependencies) and index.js (the app). Here’s a basic Dockerfile:

    
    FROM node:latest
    COPY . /app
    WORKDIR /app
    RUN npm install
    CMD ["node", "index.js"]
    
    

    Problems? It’s big, keeps extra tools, runs as root, and copies everything—even useless files.
    Now, here’s the optimized version:

    
    # Step 1: Build the app
    FROM node:18-alpine AS builder
    WORKDIR /app
    COPY package.json .
    RUN npm install
    COPY index.js .
    
    # Step 2: Create the production image
    FROM node:18-alpine
    WORKDIR /app
    
    # Create a non-root user
    RUN adduser -D myuser
    
    # Copy files from builder stage
    COPY --from=builder /app/node_modules ./node_modules
    COPY --from=builder /app/index.js .
    
    # Fix permissions for the new user
    RUN chown -R myuser /app
    
    # Switch to the non-root user
    USER myuser
    
    # Run the app
    CMD ["node", "index.js"]
    
    

    What’s good here?

    • Uses node:18-alpine—small and fixed image version.
    • Multi-stage build keeps only the app, no unnecessary tools.
    • Installs dependencies first for faster builds.
    • Runs as myuser, not root—safer!

    Extra Tips for Production

    1. Check Health: Add this to see if your app is alive:
      
      HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
      
      

      (Change the URL to your app’s.)

    2. Scan It: Use docker scan to find security holes.
    3. Hide Secrets: Don’t write passwords here—use environment variables.

    You can also use tools like Trivy – recommended by the DevSecOps. To know more about it visit: https://tecadmin.net/getting-started-with-trivy/

    Wrapping Up

    A good Dockerfile makes your app fast, light, and safe. Use small images, cut extra stuff, avoid root, and keep things predictable. Your production server—and your users—will love it. Have questions? Let me know, I’m happy to help!

    The post How to Optimize Dockerfile for a Lean, Secure Production appeared first on TecAdmin.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMozilla Thunderbird Pro: un client email open source che evolve in una piattaforma completa
    Next Article shotgun is a minimal screenshot utility for X11

    Related Posts

    News & Updates

    A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

    July 22, 2025
    News & Updates

    “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Amazon DynamoDB data modeling for Multi-tenancy – Part 3

    Databases

    CVE-2025-43569 – Substance3D Stager Out-of-Bounds Write Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CIOs are taking over the Fortune 100, IT’s all about AI

    Artificial Intelligence

    CVE-2025-3673 – Apache HTTP Server Remote Code Execution Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-20693 – Intel Wireless LAN STA Driver Out-of-Bounds Read Information Disclosure Vulnerability

    July 8, 2025

    CVE ID : CVE-2025-20693

    Published : July 8, 2025, 3:15 a.m. | 3 hours, 36 minutes ago

    Description : In wlan STA driver, there is a possible out of bounds read due to an incorrect bounds check. This could lead to remote (proximal/adjacent) information disclosure with no additional execution privileges needed. User interaction is not needed for exploitation. Patch ID: ALPS09812521; Issue ID: MSV-3421.

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-47816 – GNU PSPP XML Processing Out-of-Bounds Read Vulnerability

    May 10, 2025

    Can these $100 Android phones replace my flagship? The result after weeks of testing

    June 26, 2025

    Windows 11 offers stable and better system drivers, version 25H2 tightens rules

    July 19, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.