Mastering Docker: Essential Commands & Practical Examples

Mastering Docker: Essential Commands & Practical Examples

Author: Abdulkader Safi

Position: Software Engineer

Docker has revolutionized how developers build, test, and deploy applications by containerizing code into lightweight, portable packages. Whether you're a beginner or an experienced developer, understanding Docker's core concepts and commands can streamline your workflow. In this blog, we’ll explore essential Docker commands, create a practical example of hosting a Next.js application, and demonstrate how to set up a PostgreSQL database using Docker.


🛠️ What is Docker?

Docker is a platform that packages applications with their dependencies into containers. Containers are isolated environments that run consistently across different systems, eliminating "it works on my machine" issues.


🔧 Top Docker Commands You Need to Know

  1. docker run [IMAGE]
    Launches a container from an existing image.

    docker run hello-world
    
  2. docker images
    Lists all Docker images on your system.

    docker images
    
  3. docker ps
    Displays running containers.

    docker ps
    
  4. docker build [OPTIONS] [PATH]
    Builds a Docker image from a Dockerfile.

    docker build -t my-next-app .
    
  5. docker-compose up
    Orchestrate multi-container apps (e.g., a Next.js app + PostgreSQL).

  6. docker stop [CONTAINER_ID]
    Stops a running container.

  7. docker rm [CONTAINER_ID]
    Removes a stopped container.

  8. docker rmi [IMAGE_ID]
    Removes an unused image.


🧱 Example 1: Hosting a Next.js Application with Docker

Step 1: Create a Simple Next.js App

Start by creating a basic Next.js project:

npx create-next-app@latest my-next-app
cd my-next-app

Update pages/index.js to display a simple message:

export default function Home() {
  return <h1>Hello from Next.js!</h1>
}

Step 2: Build a Docker Image

Create a Dockerfile in your project root:

# Use the official Node.js image as a parent
FROM node:18

# Create workspace directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose port for Next.js (3000)
EXPOSE 3000

# Command to run the app
CMD ["npm", "run", "dev"]

Step 3: Run the Container

Build and run your Docker image:

docker build -t my-next-app .
docker run -d -p 3000:3000 my-next-app

Verify: Access http://localhost:3000 in your browser to see the app!


🐢 Example 2: Hosting PostgreSQL with Docker

Step 1: Use the Official PostgreSQL Image

Run a PostgreSQL container using docker run:

docker run --name my-postgres \
  -e POSTGRES_USER=admin \
  -e POSTGRES_PASSWORD=123456 \
  -e POSTGRES_DB=mydb \
  -p 5432:5432 \
  -d postgres

Explanation:

  • --name: Assigns a name to the container.
  • -e: Sets environment variables for PostgreSQL credentials.
  • -p: Maps the container’s port 5432 to your host’s port.
  • -d: Runs the container in detached mode.

Step 2: Connect to PostgreSQL

Use psql from the container shell to test the connection:

docker exec -it my-postgres psql -U admin -d mydb

Tip: To persist data, use a volume:

docker run --name my-postgres \
  -e POSTGRES_USER=admin \
  -e POSTGRES_PASSWORD=123456 \
  -e POSTGRES_DB=mydb \
  -p 5432:5432 \
  -v /path/to/host/data:/var/lib/postgresql/data \
  -d postgres

🚀 Docker Compose Example: Next.js + PostgreSQL

Use docker-compose.yml to run both services together:

version: '3'
services:
  nextjs:
    build: ./next-app
    ports:
      - "3000:3000"
  postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: 123456
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Run with:

docker-compose up -d

📚 Resources to Deepen Your Docker Knowledge

  1. Docker Official Documentation
  2. Docker Hub (Images/Registry)
  3. Docker Compose Guide
  4. PostgreSQL Docker Image Docs

📌 Final Thoughts

Docker simplifies development and deployment workflows by standardizing environments. Whether you're building a static site with Next.js or setting up a PostgreSQL database, Docker's flexibility ensures your app runs consistently everywhere. Start small with these examples and expand into more complex projects!

Happy containerizing! 🎉


Related Blogs

Laravel vs. Spring Boot: A Balanced Comparison for Developers Considering a Framework Switch

Laravel vs. Spring Boot: A Balanced Comparison for Developers Considering a Framework Switch

If you're familiar with Laravel but have started exploring Spring Boot, you’re not alone. Both frameworks are powerful tools in their own right, but they cater to different needs, ecosystems, and use cases. As someone who’s used Laravel for its speed and developer-friendly design, you might be wondering: Is Spring Boot worth learning? Let’s break down the key differences in development environment, scalability, speed, cost, and runtime to help you decide.

May 28, 2025 Learn More...
Bootstrap vs. Tailwind CSS: A Comparison for Modern Web Development

Bootstrap vs. Tailwind CSS: A Comparison for Modern Web Development

When building a website, choosing the right framework is critical. Two of the most popular options are Bootstrap and Tailwind CSS, both CSS frameworks with distinct philosophies, strengths, and use cases. While Bootstrap has been a staple for years, Tailwind CSS is gaining traction as developers seek more control and flexibility. Let’s break down the key differences between these two frameworks to help you decide which is better for your project.

May 26, 2025 Learn More...
Beyond "Click Here": A Guide to Different Types of Links You Need to Know

Beyond "Click Here": A Guide to Different Types of Links You Need to Know

They're the backbone of the internet, connecting us to information, products, and experiences. But did you know there's more than one kind of link? Understanding the different types of links isn’t just for web developers; it's valuable for anyone involved in online marketing, content creation, or even just navigating the web effectively. Let's break down some of the most common types you’ll encounter.

May 30, 2025 Learn More...
© Abdulkader Safi