Docker for Beginners: Complete Getting Started Guide

<h2>What is Docker?</h2>

<p>Docker is a platform for developing, shipping, and running applications in containers. Containers package your application with all its dependencies, ensuring it works consistently across different environments.</p>

<h2>Why Use Docker?</h2>

<ul>

<li><strong>Consistency:</strong> Same environment in development, testing, and production</li>

<li><strong>Isolation:</strong> Applications don't interfere with each other</li>

<li><strong>Portability:</strong> Run anywhere Docker is installed</li>

<li><strong>Efficiency:</strong> Lightweight compared to virtual machines</li>

</ul>

<h2>Installing Docker</h2>

<h3>On Windows</h3>

<ol>

<li>Download Docker Desktop from docker.com</li>

<li>Run the installer</li>

<li>Restart your computer</li>

<li>Verify installation: <code>docker --version</code></li>

</ol>

<h3>On macOS</h3>

<ol>

<li>Download Docker Desktop for Mac</li>

<li>Drag to Applications folder</li>

<li>Launch Docker</li>

<li>Verify: <code>docker --version</code></li>

</ol>

<h2>Key Concepts</h2>

<p><strong>Image:</strong> A read-only template with instructions for creating a container</p>

<p><strong>Container:</strong> A running instance of an image</p>

<p><strong>Dockerfile:</strong> A text file with instructions to build an image</p>

<p><strong>Registry:</strong> A storage for Docker images (e.g., Docker Hub)</p>

<h2>Your First Container</h2>

<p>Run the classic "Hello World" container:</p>

<pre><code>docker run hello-world</code></pre>

<p>This command:</p>

<ol>

<li>Pulls the hello-world image from Docker Hub</li>

<li>Creates a container from the image</li>

<li>Runs the container</li>

<li>Displays output</li>

</ol>

<h2>Running a Web Server</h2>

<p>Let's run an Nginx web server:</p>

<pre><code>docker run -d -p 8080:80 nginx</code></pre>

<p>Flags explained:</p>

<ul>

<li><code>-d</code>: Run in detached mode (background)</li>

<li><code>-p 8080:80</code>: Map port 8080 on host to port 80 in container</li>

</ul>

<p>Visit http://localhost:8080 to see Nginx running!</p>

<h2>Creating a Dockerfile</h2>

<p>Create a <code>Dockerfile</code> for a Node.js application:</p>

<pre><code>FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]</code></pre>

<h2>Building Your Image</h2>

<pre><code>docker build -t my-app .</code></pre>

<h2>Running Your Container</h2>

<pre><code>docker run -p 3000:3000 my-app</code></pre>

<h2>Common Commands</h2>

<ul>

<li><code>docker ps</code>: List running containers</li>

<li><code>docker ps -a</code>: List all containers</li>

<li><code>docker images</code>: List images</li>

<li><code>docker stop [container]</code>: Stop a container</li>

<li><code>docker rm [container]</code>: Remove a container</li>

<li><code>docker rmi [image]</code>: Remove an image</li>

</ul>

<h2>Conclusion</h2>

<p>Docker simplifies application deployment and ensures consistency across environments. Start with these basics, then explore Docker Compose for multi-container applications.</p>