Skip to content

title: "What Is Docker? A Complete Beginner’s Guide to Containers" slug: "introduction-to-docker-beginners-guide" description: "Learn what Docker is, how containers work, and why developers use Docker for modern application development. A complete beginner-friendly introduction." keywords: ["Docker", "containers", "Docker beginner guide", "DevOps basics", "what is Docker"] featured_image: "/images/docker/docker-introduction-cover.png" published: true


🚀 What Is Docker?

  • Docker is an open-source containerization platform that packages an application and everything it needs—code, libraries, dependencies, and OS-level files—into a container.
  • Docker is an open source containerizatin platform.It enables developer to package application in container- standardized executable components combining application source code , applcation dependencies and OS libraries required to run application on any platform
  • Docker is a suite of tools for developers to build, share, run and orchestrate containerized apps.
  • Docker is made up of two things Docker CLI + Docker Engine

A container runs the same anywhere:

  • Your laptop
  • Production server
  • CI/CD pipeline
  • Cloud infrastructure

Docker ensures consistent execution without the infamous “works on my machine” problem.


🧱 Docker Components

1. Docker Engine

  • The runtime that builds and executes containers.
  • is a container runtime that runs in almost any environment: Mac and Windows PCs, Linux and Windows servers, the cloud, and on edge devices. Docker Engine is built on top containerd, the leading open-source container runtime, a project of the Cloud Native Computing Foundation (CNCF).

2. Docker CLI

The command-line tool developers use to interact with Docker Engine.

Together, they allow you to create, run, and manage containers effortlessly.


🆚 Docker vs Virtual Machines

Docker uses OS-level virtualization, whereas virtual machines run a full OS for each environment.

Docker Virtual Machine
Lightweight (MBs) Heavy (GBs)
Fast startup (ms) Slow startup (minutes)
Shares host kernel Separate OS per VM
Ideal for microservices Ideal for full OS needs

Here’s a visual comparison:

Docker vs VM


🧩 Why Developers Use Docker

  • Consistent environment across development, staging, and production
  • Faster deployments
  • Lightweight and resource-efficient
  • Simplifies dependency management
  • Works well with CI/CD and cloud

Installation

Docker service error

  • journalctl -u docker :- The command journalctl -u docker is used on Linux systems with systemd to display the logs for the Docker system service. This allows you to monitor the Docker daemon's activity, status, and any potential errors it encounters, which is crucial for troubleshooting
  • https://stackoverflow.com/questions/63878097/docker-not-starting-and-failing-with-error-failed-to-start-docker-service-uni => docker not starting and failing with error , Failed to start docker.service: Unit not found
  • Failed to start docker.service: Unit docker.socket not found. =>
    • The error "Failed to start docker.service: Unit docker.socket not found" indicates that the docker.socket systemd unit file is missing from your system's configuration directories (typically /lib/systemd/system or /etc/systemd/system). The docker.service relies on this socket file for socket activation
    • This problem often occurs if Docker was installed using an unsupported method, an outdated package, or if the systemd unit files were manually modified or corrupted
    • Verify the file's existence: ls /lib/systemd/system/docker.socket /etc/systemd/system/docker.socket
    • Perform a clean reinstallation (Recommended)

🛠️ Troubleshooting Common Docker Errors

❌ 1. “docker-ce-cli not installable”

Likely due to outdated repositories.

Fix:

sudo apt update

sudo apt --fix-broken install

❌ 2. Docker service not found

Error:

Failed to start docker.service: Unit not found

Fix:

sudo systemctl daemon-reload

sudo systemctl enable docker

sudo systemctl start docker

❌ 3. Unmet Dependencies

Fix:

sudo apt --fix-broken install

❌ 4. Checking Docker logs

journalctl -u docker

📌 Important Docker System Paths

/usr/bin/dockerd

/etc/systemd/system/docker.service

Creating Image using Dockerfile

  • creates a container image, the blueprint for a container, including everything needed to run an application – the application code, binaries, scripts, dependencies, configuration, environment variables, and so on
    # Use the official NGINX image as a base 
            FROM nginx:latest
      # Download and install a dependency - RUN
    #Copy all files from the current directory into NGINX's web root 
            ADD . /usr/share/nginx/html
      // Tell the image what to do when it starts as container - CMD
    

Common Dockerfile Instructions

  • FROM nginx:latest – Sets the base image for your container (here, the latest NGINX image).
  • ADD . /usr/share/nginx/html

    • Copies all files from your project directory (.) into the container’s NGINX HTML folder (/usr/share/nginx/html).
    • This is useful to serve your own HTML/CSS/JS files with NGINX.
  • RUN – run commands while building image

  • CMD – default command executed on container start
  • COPY/ADD – copy files into image
Command Purpose
docker build -t tagname:[tag] . Build a Docker image with the given tag from the current directory
docker images List all images on the local machine
docker image ls List all images (same as docker images)
docker image ls -q List only image IDs (useful for scripting or piping into commands)
docker image rm <image> Remove a specific image by name or ID
docker rmi <image> Remove a specific image (same as docker image rm)
docker image rm $(docker image ls -q) Remove all Docker images on the system

Docker hub

  • docker pull nginx => Pull the nginx image from Docker Hub
  • docker push username/imagename:tag => to push image to Docker Hub
  • is a registry service provided by Docker for finding and sharing container images with your team or the public. Docker Hub is similar in functionality to GitHub.

Running Image

  • docker run = create container + start container (with terminal attached).

docker run basically performs two steps under the hood:

1. docker create — Prepare the container

  • Creates a container from the image by adding a writable layer on top of the image’s read-only layers.
  • Loads the image metadata (ENTRYPOINT, CMD, env vars, ports, etc.).
  • Sets up the container’s filesystem, networking, and configuration.
  • The container exists but is not running yet.

2. docker start -a — Run the container

  • Starts the container’s main process using the previously prepared configuration.
  • -a attaches your terminal to the container’s stdin/stdout/stderr so you can see the program’s output in real time.
  • export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nIMAGE\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n" => Define a custom output format for docker ps
  • sudo docker ps --format="$FORMAT"
Command / Option Purpose
docker --version Show the installed Docker version
docker build -t nginx:latest.
docker build -t <image_name>:<tag> <path_to_dockerfile_directory>
Build a Docker image with a specified tag from the current directory.
<path_to_dockerfile_directory> → Path where the Dockerfile is located, often . for the current directory.
docker images List all images on the local machine
docker image ls List all images (same as docker images)
docker image ls -q List only image IDs (useful for scripting/piping)
docker image rm <image> Remove a specific image by name or ID
docker rmi <image> Remove a specific image (same as docker image rm)
docker image rm $(docker image ls -q) Remove all Docker images
sudo docker ps --format="$FORMAT" Display running containers using the custom format
docker run image_name:tag Run an image (default tag is latest if not provided). If the image is not local, Docker pulls it automatically
docker run --name custom-name -d -p 8080:80 nginx Run container with a name, in detached mode, and map port 8080 → 80
docker run -e port=1234 Set an environment variable inside the container
-e KEY=value Pass environment variables to the container
-it Run container in interactive mode (opens shell + TTY)
-d Run container in detached mode (background mode)
CTRL + P Q Exit an -it shell without stopping the container
docker inspect <container> View all container configuration and metadata
docker logs <container> View logs generated by a container
## Docker container
Command / Option Purpose
------------------------------------------------------ -----------------------------------------------------------------------------------------
docker run ubuntu Runs Ubuntu container. Exits immediately because Docker containers are meant to run processes, not full OS.
docker run ubuntu sleep 5 Runs Ubuntu container and executes sleep 5; container exits after 5 seconds
docker start -a <container_id> Start a stopped container. The original command used during docker run will execute again
docker container ls or docker ps List all running containers
docker container ls -a or docker ps -a List all containers (running and stopped)
docker container ls -q or docker ps -q List IDs of all running containers
docker container ls -aq or docker ps -aq List IDs of all containers (running and stopped)

Running Containers

Command / Option Purpose
docker run -d --name <name> -p <host_port>:<container_port> <image> Start a container in detached mode, map host port to container port, and optionally name it

Interacting with Containers

Command / Option Purpose
docker exec -it <container> <command> Open an interactive terminal session inside a running container (-i = interactive, -t = TTY)
/bin/sh or /bin/bash Shell used inside the container when running docker exec -it
docker attach <container_name> Attach your terminal to a running container to see its output and interact with it
Ctrl + P then Ctrl + Q Detach from an interactive container without stopping it

Stopping Containers

Command / Option Purpose
docker stop <container_id> Stop a container gracefully, allowing cleanup (default timeout 10s)
docker kill <container_id> Kill a container immediately, without allowing cleanup

Removing Containers

Command / Option Purpose
docker rm <container_name|ID> Remove a stopped container
docker rm $(docker ps -aq) or docker container prune Remove all stopped containers. Will fail if any container is running
docker rm -f $(docker ps -aq) Force remove all containers, including running ones docker ps -aq → lists all container IDs
docker rm -f → force removal
Concept / Command Purpose / Notes
Docker Image vs Container Image = template for creating an environment. Container = a running instance of an image.
Exposing Ports -p 8080:80 maps host port 8080 → container port 80.
Multiple ports You can map multiple ports to a single container.
Port conflicts Cannot map the same host port to multiple containers. Example error:
Bind for 0.0.0.0:8080 failed: port is already allocated
Naming Containers --name website assigns a custom name to a container.
Name conflicts Cannot use the same name for multiple containers. Example error:
Conflict. The container name "/website" is already in use...
Example sudo docker run -d -p 8080:80 --name website nginx
Example with port mapping sudo docker run -d -p 8080:80 nginx

21. Cleaning Up

```bash

docker system prune

docker image prune

docker container prune

docker volume prune

  • docker system prune This removes: Stopped containers Unused networks Dangling images Build cache

Common Debugging Commands

docker stats

docker top

docker history