Guide: A Beginner's Guide to Docker

Understand containers, images, and Dockerfiles. Learn how Docker simplifies application deployment and development workflows.

By Upingi Team / Published on November 05, 2024

Introduction: What is Docker & Why Use It?

Unlike traditional Virtual Machines (VMs) which virtualize hardware, Docker uses containerization to virtualize the operating system. This means containers package an application with all its dependencies (libraries, code, runtime) but share the host OS kernel, making them much more lightweight and faster than VMs. Docker is a platform that allows you to easily build these packages (images), share them, and run them as containers anywhere.

Key benefits include: Consistency ("It works on my machine" problems vanish), Faster Deployment (spin up containers in seconds), Resource Efficiency (more apps per server), and Isolation (apps run independently).

Docker logo and container visualization

This guide is for developers, operations engineers, or anyone involved in building and deploying software who wants a foundational understanding of Docker's core concepts and how to get started.

Chapter 1: Core Docker Concepts

Understanding these terms is key:

  • Docker Engine: The underlying client-server application that builds and runs containers.
  • Image: A read-only template containing instructions for creating a container (like a snapshot or blueprint). Includes application code, libraries, runtime, etc. Analogy: a recipe.
  • Container: A runnable instance of an image. It's the live, running application package. Analogy: the cake baked from the recipe.
  • Dockerfile: A text file with instructions Docker uses to build an image automatically.
  • Volume: A mechanism for persisting data generated by and used by Docker containers, existing outside the container's writable layer.
  • Registry (e.g., Docker Hub): A repository for storing and distributing Docker images.

Diagram showing relationship between Dockerfile, Image, and Container

Chapter 2: Installing Docker

The easiest way to get started on your local machine is by installing Docker Desktop. Download it from the official Docker website for your operating system:

After installation, open your terminal or command prompt and verify Docker is running correctly with these commands:

docker --version
docker run hello-world

The `hello-world` command downloads a test image and runs it in a container. If successful, it confirms your setup is working.

Chapter 3: Working with Images & Containers

Here are some fundamental commands:

  • `docker pull [image_name]`: Downloads an image from a registry (Docker Hub by default).
  • `docker images`: Lists images stored locally.
  • `docker run [options] [image_name]`: Creates and starts a new container from an image.
  • `docker ps`: Lists currently running containers.
  • `docker ps -a`: Lists all containers (running and stopped).
  • `docker stop [container_id_or_name]`: Stops a running container.
  • `docker rm [container_id_or_name]`: Removes a stopped container.
  • `docker rmi [image_id_or_name]`: Removes a local image.

Common `docker run` options include: `-d` to run the container in the background (detached), `-p host_port:container_port` to map a port from your host machine to the container, and `--name descriptive_name` to assign a memorable name to the container.

# Example: Run nginx container
docker pull nginx
docker run --name my-nginx -d -p 8080:80 nginx

Chapter 4: Creating Your First Dockerfile

A Dockerfile is a script containing instructions to build a custom Docker image. Key instructions include: `FROM` (specifies the base image, e.g., `python:3.9-slim`), `WORKDIR` (sets the working directory inside the image), `COPY` (copies files from your host into the image), `RUN` (executes commands like installing packages during the image build process), `EXPOSE` (informs Docker that the container listens on specified network ports at runtime), and `CMD` (provides the default command to run when the container starts).

Example `Dockerfile` for a simple Python app:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]

To build an image from a Dockerfile in the current directory, use: `docker build -t your_app_name .` (The `-t` flags tags the image with a name).

Chapter 5: Data Persistence with Volumes

By default, any data written inside a container's filesystem is lost when the container is removed (it's ephemeral). To persist data like databases or user uploads, use Docker Volumes. Volumes are managed by Docker and exist separately from the container lifecycle. You can create a named volume (`docker volume create my-data`) and mount it into a container using the `-v` or `--mount` flag: `docker run -v my-data:/path/in/container ...` This links the volume `my-data` to the specified path inside the container, ensuring data written there persists even if the container is deleted.

Conclusion: Your Docker Journey Begins

You've learned the fundamentals: what Docker is, the difference between images and containers, how to build images using a Dockerfile, and how to persist data with volumes. This foundation allows you to leverage Docker's benefits for consistent development and simplified deployment. The next steps often involve exploring Docker Compose for managing multi-container applications, understanding Docker networking, and potentially learning container orchestration tools like Kubernetes for large-scale deployments.