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)