Getting Started with Docker

Docker is a powerful platform that enables developers to create, deploy, and manage applications inside lightweight, portable containers. Containers ensure that applications run consistently across different environments. In this tutorial, we’ll walk through Docker basics.

Step 1: Install Docker

Install Docker on your system by following these steps:

  • Windows/Mac:
    1. Download Docker Desktop from the official Docker website.
    2. Run the installer and follow the setup wizard.
    3. Start Docker Desktop and ensure it is running.
  • Linux:
    1. Update your package index:
      sudo apt update
    2. Install Docker:
      sudo apt install docker.io
    3. Start and enable Docker:
      sudo systemctl start docker
          sudo systemctl enable docker

Step 2: Verify Docker Installation

Run the following command to check if Docker is installed correctly:

docker --version

You should see the installed Docker version.

Step 3: Running Your First Container

Run the hello-world container to verify Docker is working:

docker run hello-world

This command downloads and runs a small test container, printing a welcome message.

Step 4: Working with Docker Images

Docker images are templates used to create containers. Common commands include:

  • Search for an image:
    docker search ubuntu
  • Pull an image:
    docker pull ubuntu
  • List downloaded images:
    docker images

Step 5: Creating and Running Containers

Use an image to create and run a container:

docker run -it ubuntu

This command starts a container interactively with the Ubuntu image. You can now run commands inside the container.

Step 6: Managing Containers

Common container management commands include:

  • List running containers:
    docker ps
  • List all containers (including stopped ones):
    docker ps -a
  • Stop a container:
    docker stop container_id
  • Remove a container:
    docker rm container_id

Step 7: Creating a Dockerfile

A Dockerfile automates the creation of custom images. Example:

# Use an official Python runtime as a parent image
    FROM python:3.9-slim

    # Set the working directory
    WORKDIR /app

    # Copy the current directory contents into the container
    COPY . /app

    # Install any needed packages
    RUN pip install -r requirements.txt

    # Run the application
    CMD ["python", "app.py"]

Step 8: Building and Running a Custom Image

Build an image from a Dockerfile:

docker build -t my-python-app .

Run a container from the custom image:

docker run my-python-app

Step 9: Docker Compose

Docker Compose simplifies multi-container applications:

version: '3'
    services:
      web:
        image: nginx
        ports:
          - "80:80"
      app:
        build: .
        depends_on:
          - db
      db:
        image: postgres

Next Steps

Explore Docker Hub for pre-built images, experiment with Docker Compose, and learn about orchestration tools like Kubernetes. Docker is a foundational tool for modern DevOps practices.