DEV Community

Muhammad Sufiyan Baig
Muhammad Sufiyan Baig

Posted on

Docker for Beginners: Containerizing Your First Application

What is Docker?

Docker is a platform that enables users to package applications into lightweight, independent containers known as Docker containers. Unlike traditional virtual machines (VMs), Docker containers share the host system’s kernel, ensuring better speed, resource efficiency, and consistency across different environments.


Prerequisites

Before you begin, ensure you have the following installed:

  1. Docker Desktop (for macOS/Windows) or Docker Engine (for Linux). Install it from Docker's official website.
  2. A simple application. In this guide, we will use a basic Node.js "Hello World" server.

Step 1: Create a Sample Application

Create a basic Node.js app with the following files:

1. package.json

{
  "name": "docker-demo",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  }
}
Enter fullscreen mode Exit fullscreen mode

2. server.js

const express = require('express');  
const app = express();  
const port = 3000;  

app.get('/', (req, res) => {  
  res.send('Hello from Docker!');  
});  

app.listen(port, () => {  
  console.log(`App running on http://localhost:${port}`);  
});  
Enter fullscreen mode Exit fullscreen mode

Step 2: Write a Dockerfile

Create a Dockerfile in your project’s root directory:

FROM node:18-alpine  

WORKDIR /app  

COPY package*.json ./  

RUN npm install  

COPY . .  

EXPOSE 3000  

CMD ["npm", "start"]  
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • FROM: Uses Node.js (Alpine Linux variant) as the base image.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies necessary files into the container.
  • RUN: Installs dependencies.
  • EXPOSE: Declares the port but does not automatically map it.
  • CMD: Defines the command to start the application.

Step 3: Build the Docker Image

Navigate to your project directory and run:

docker build -t my-first-docker-app .  
Enter fullscreen mode Exit fullscreen mode
  • -t my-first-docker-app: Tags the image with a name.
  • .: Specifies the current directory as the build context.

Step 4: Run the Container

Start the container with the following command:

docker run -p 3000:3000 my-first-docker-app  
Enter fullscreen mode Exit fullscreen mode
  • -p 3000:3000: Maps port 3000 of your local machine to port 3000 inside the container.
  • Open http://localhost:3000 in your browser to see the message: “Hello from Docker!”

Best Practices for Beginners

  • Use a .dockerignore file: Prevent unnecessary files (e.g., node_modules, .env) from being copied.
  • Optimize Image Size: Prefer smaller base images like alpine variants.
  • Avoid Running as Root: Add a non-root user for better security.
  • Clean Up Unused Images/Containers:
  docker rm <container_id>  # Remove stopped containers
  docker rmi <image_id>      # Remove unused images
Enter fullscreen mode Exit fullscreen mode

Conclusion

You have successfully containerized your first application using Docker. Docker simplifies dependency management and ensures application consistency across different environments.

To further explore Docker, try experimenting with Docker Compose for multi-container applications or explore the vast Docker ecosystem.

Happy Containerizing! 🐳

Top comments (0)