๐Ÿณ Docker Basics Every Beginner Should Know
DEV Community

๐Ÿณ Docker Basics Every Beginner Should Know

What is Docker?

Docker is a containerization platform that allows you to package an application together with all of its dependencies. This ensures the application runs consistently across different environments-whether it's your local machine, a testing server, or production. One of Docker's biggest advantages is the famous phrase: "It works on my machine." โ†’ With Docker, it works on every machine.

Docker Architecture

Before learning the commands, it's helpful to understand how Docker works behind the scenes.

Docker Client

The Docker Client is the command-line interface (CLI) that we use every day. Whenever we run commands like:

  • docker run
  • docker ps
  • docker pull

the Docker Client sends those requests to the Docker Daemon.

Docker Daemon

The Docker Daemon (dockerd) runs in the background. It is responsible for:

  • Building images
  • Running containers
  • Managing images
  • Managing networks
  • Managing volumes

Think of it as the engine that powers Docker.

Docker Image

A Docker Image is a read-only template that contains everything required to run an application:

  • Application code
  • Runtime
  • Libraries
  • Dependencies
  • Configuration

Images are used to create containers.

Docker Container

A Container is a running instance of a Docker image. Containers are:

  • Lightweight
  • Isolated
  • Portable
  • Fast to start

You can create multiple containers from the same image.

Docker Registry

A Docker Registry is where Docker images are stored and shared. The most commonly used public registry is Docker Hub, where thousands of official and community images are available.

What is Containerization?

Containerization means packaging an application together with all of its dependencies so that it runs the same way everywhere. Instead of worrying about different operating systems or missing libraries, Docker provides a consistent environment.

Essential Docker Commands

1. Check Docker Installation

  • Check Docker Version
    docker -v
    Displays the installed Docker version.

  • Show Docker Client & Daemon Version
    docker version
    Shows both the Docker Client and Docker Daemon versions.

  • Display Docker Information
    docker info
    Displays detailed information about your Docker installation.

2. Working with Images

  • Download an Image
    docker pull hello-world:latest
    Downloads the image from Docker Hub.
    Note: If the image already exists locally, Docker won't download it again.

  • View Local Images
    docker images or docker image ls
    Lists all images stored locally.

  • Remove an Image
    docker image rm <image_name_or_id> or docker rmi <image_name_or_id>
    Deletes an image from your local machine.

3. Creating and Running Containers

  • Create a Container
    docker create ubuntu
    Creates a container but does not start it.

  • Start an Existing Container
    docker start <container_id>
    Starts a previously created container.

  • Create and Run a Container
    docker run hello-world
    If the image doesn't exist locally, Docker downloads it first and then creates and starts the container.

  • Run an Interactive Ubuntu Container
    docker run -it ubuntu
    Downloads the Ubuntu image (if needed), starts the container, and opens an interactive terminal where you can execute Linux commands.

  • Run a Container in Detached Mode
    docker run -d nginx
    Runs the container in the background.

4. Managing Containers

  • View Running Containers
    docker ps
    Lists only running containers.

  • View All Containers
    docker ps -a
    Lists both running and stopped containers.

  • Stop a Container
    docker stop <container_id>
    Stops a running container.

  • Restart a Container
    docker restart <container_id>
    Restarts a container.

  • Remove a Container
    docker rm <container_id>
    Deletes a stopped container.

5. Execute Commands Inside a Running Container

Open a Bash shell inside a running container:

docker exec -it <container_id> bash

For example, if you're running Redis:

docker exec -it <container_id> redis-cli

6. Viewing Logs

  • Display Container Logs
    docker logs <container_id>
    Shows logs generated by the container.

  • Follow Logs in Real Time
    docker logs -f <container_id>
    Streams logs as they are generated.

7. Cleaning Up Docker Resources

  • Remove Unused Resources
    docker system prune
    This removes:

    • Stopped containers
    • Unused networks
    • Dangling images
    • Build cache
  • Remove All Unused Images
    docker system prune -a
    Removes all unused images, stopped containers, unused networks, and build cache.

Quick Summary

Command Purpose
docker -v Check Docker version
docker version Show Docker Client & Daemon version
docker info Display Docker information
docker pull Download an image
docker images List local images
docker run Create and run a container
docker create Create a container
docker start Start an existing container
docker ps List running containers
docker ps -a List all containers
docker exec -it Execute commands inside a running container
docker logs View container logs
docker image rm Remove an image
docker rm Remove a container
docker system prune Clean up unused Docker resources

Final Thoughts

Docker is one of the most important tools in modern software development and DevOps. While there are many advanced topics to explore-such as Dockerfiles, Docker Compose, volumes, networking, Kubernetes, and container orchestration-mastering these fundamentals will make your learning journey much smoother. I'm currently learning Docker as part of my DevOps roadmap, and I'll continue sharing practical notes and examples as I progress. If you have suggestions, best practices, or beginner tips, feel free to share them in the comments. Happy Learning!

Comments

No comments yet. Start the discussion.