DEV Community

Hasan Konya
Hasan Konya

Posted on

πŸš€ Docker Beginner's Guide

πŸ“Œ 1. Introduction: What is Docker?

Docker and Container Concept

Docker is a platform that allows applications to run in isolated environments called containers. Containers include all the dependencies required for the application to function and ensure seamless operation across different platforms.

Advantages of Containers:

  • Platform-independent.
  • Fast startup and shutdown.
  • Lightweight compared to virtual machines.
  • Easy to replicate and distribute.

πŸ›  2. Getting Started with Dockerfile

A Dockerfile is a script used to create Docker images. It includes instructions on which base image to use, how dependencies should be installed, and how the application should be run.

πŸ“ Basic Dockerfile Example (Node.js Application)

# Using the official Node.js image as the base
FROM node:18-alpine

# Set the working directory
WORKDIR /app

# Copy dependencies and install them
COPY package.json .
RUN npm install

# Copy application files
COPY . .

# Start the application
CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

πŸ” Key Components of Dockerfile

  • FROM: Specifies the base image.
  • WORKDIR: Sets the working directory.
  • COPY: Copies files into the container.
  • RUN: Executes a command in the terminal (e.g., npm install).
  • CMD: Defines the command to run when the container starts.

πŸ— How to Build a Docker Image

To build a Docker image, run the following command:

docker build -t my-node-app .
Enter fullscreen mode Exit fullscreen mode

This command creates an image named my-node-app using the Dockerfile in the current directory.


πŸš€ 3. Managing Multiple Services with Docker Compose

Docker Compose is a tool for managing multiple services at once. For example, we can run a Node.js application alongside a PostgreSQL database.

πŸ“„ Example docker-compose.yml File

version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db

  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydatabase
Enter fullscreen mode Exit fullscreen mode

▢️ Running the Application with Compose

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

This command starts both the application and the database in the background (-d flag).


🌍 4. Running and Managing Docker Containers

Key commands for running, stopping, and managing Docker containers:

πŸ“Œ Listing Running and All Containers

docker ps        # Lists running containers
docker ps -a     # Lists all containers (running + stopped)
Enter fullscreen mode Exit fullscreen mode

πŸ›‘ Starting and Stopping a Container

docker start <container_id>
docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Restarting a Container

docker restart <container_id>
Enter fullscreen mode Exit fullscreen mode

πŸ—‘ Removing a Container

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

🧹 Cleaning Up Unused Containers and Images

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

πŸ“ Viewing Container Logs

docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode

πŸ” Debugging Inside a Container

docker exec -it <container_id> sh
Enter fullscreen mode Exit fullscreen mode

This command allows you to access the shell inside a running container.

🌐 Managing Container Networks

docker network ls          # Lists existing networks
docker network create my_network  # Creates a new network
docker network connect my_network <container_id>  # Connects a container to a network
docker network disconnect my_network <container_id>  # Removes a container from a network
Enter fullscreen mode Exit fullscreen mode

πŸš€ Conclusion and Summary

βœ… Docker is a container technology that simplifies software development and accelerates deployment.

βœ… Dockerfile enables applications to run in an isolated environment.

βœ… Docker Compose is a powerful tool for managing multiple services and is especially useful for microservices architectures.

βœ… Docker containers are flexible, easily scalable, and portable.

βœ… Learning basic container management commands allows developers to efficiently handle their applications.

πŸ’‘ Tip: Enhance your Docker workflow by exploring CI/CD integrations for automated deployments! πŸš€

Top comments (0)

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay