<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ayushman gupta</title>
    <description>The latest articles on Forem by Ayushman gupta (@ayushman_gupta_7136e36ae6).</description>
    <link>https://forem.com/ayushman_gupta_7136e36ae6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2854794%2Fcf82c69a-20e0-45f2-b87a-23c24141e368.png</url>
      <title>Forem: Ayushman gupta</title>
      <link>https://forem.com/ayushman_gupta_7136e36ae6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ayushman_gupta_7136e36ae6"/>
    <language>en</language>
    <item>
      <title>Docker, Demystified: Building and Optimizing Containers the Right Way</title>
      <dc:creator>Ayushman gupta</dc:creator>
      <pubDate>Tue, 10 Feb 2026 15:23:45 +0000</pubDate>
      <link>https://forem.com/ayushman_gupta_7136e36ae6/docker-demystified-building-and-optimizing-containers-the-right-way-1f3b</link>
      <guid>https://forem.com/ayushman_gupta_7136e36ae6/docker-demystified-building-and-optimizing-containers-the-right-way-1f3b</guid>
      <description>&lt;p&gt;Docker is often introduced as a tool to package applications but many developers struggle once they move beyond basic docker run commands. In this post, I’ll walk through how Docker actually works, how to write an efficient &lt;u&gt;Dockerfile&lt;/u&gt;, and how to debug common issues that appear in real-world projects&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Docker Matters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before Docker, applications often failed with it works on my machine problems. &lt;/li&gt;
&lt;li&gt;Different OS versions, dependencies, and configurations made deployment painful. &lt;/li&gt;
&lt;li&gt;Docker solves this by packaging an application and its dependencies into a container, which runs consistently across environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike virtual machines, containers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Share the host OS kernel&lt;/li&gt;
&lt;li&gt;Start faster&lt;/li&gt;
&lt;li&gt;Use fewer resources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Core Docker Concepts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Image&lt;/strong&gt;: A read-only template containing the app and its dependencies&lt;br&gt;
&lt;strong&gt;Container&lt;/strong&gt;: A running instance of an image&lt;br&gt;
&lt;strong&gt;Dockerfile&lt;/strong&gt;: Instructions to build an image&lt;br&gt;
&lt;strong&gt;Layer&lt;/strong&gt;: Each Dockerfile instruction creates a cached layer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dockerizing a Simple Node.js App&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing a Minimal Dockerfile
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install --production

COPY . .

EXPOSE 3000
CMD ["node", "index.js"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Common Docker Mistake: Huge Image Sizes
&lt;/h2&gt;

&lt;p&gt;Many beginners copy everything before installing dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY . .
RUN npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This breaks caching and bloats images&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-Stage Builds
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app .
CMD ["node", "index.js"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps only what’s needed in the final image&lt;/p&gt;

&lt;p&gt;** Debugging a Real Docker Issue**&lt;br&gt;
Symptom:Container builds successfully but crashes immediately on startup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker logs &amp;lt;container-id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;&lt;br&gt;
Used explicit defaults and validated required variables at startup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!process.env.PORT) {
  throw new Error("PORT not set");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This made failures obvious and easier to debug&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verifying the Container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t demo-app .
docker run -p 3000:3000 demo-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visiting &lt;u&gt;localhost:3000&lt;/u&gt; confirms the container is working correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker images are built in layers — ordering matters&lt;/li&gt;
&lt;li&gt;Smaller images mean faster builds and deployments&lt;/li&gt;
&lt;li&gt;Debugging containers requires logs, not guesswork&lt;/li&gt;
&lt;li&gt;Good Dockerfiles are optimized, readable, and predictable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Docker is more than a deployment tool — it’s a way to make software reproducible and reliable. By understanding how images, layers, and containers work internally, developers can avoid common pitfalls and build systems that scale cleanly.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>devops</category>
      <category>docker</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
