Docker for Beginners: Images, Containers & Compose in 15 Minutes

If you've ever heard "it works on my machine" when code breaks in production, you understand why Docker exists. Docker standardizes software delivery by packaging code, runtime, system tools, and libraries into one neat, runnable unit.

This docker tutorial covers the docker basics you need to get up and running fast. Whether you're a frontend dev, backend engineer, or completely new to DevOps, this guide is your definitive starting point.

Core Concepts Explained

Images vs Containers

The most common point of confusion when getting started with docker is the difference between an image and a container.

  • Docker Image: The blueprint or template. It is an immutable (read-only) file containing the source code, libraries, dependencies, tools, and other files needed for an application to run.
  • Docker Container: The running instance of an image. If an image is a class in programming, a container is the instantiated object. You can run hundreds of containers from a single image.

The Dockerfile

A Dockerfile is a simple text document containing all the commands a user could call on the command line to assemble an image. It's the recipe used to bake the Docker Image.

Docker Compose

While Docker CLI runs single containers, docker-compose is a tool for defining and running multi-container Docker applications. With a single YAML file, you can start a database, a backend API, and a frontend client simultaneously.

Walkthrough: Building and Running Your First App

1. Creating a Dockerfile

Let's look at a real-world Dockerfile example for a simple Node.js web server:

# Use an official, lightweight Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code
COPY . .

# Document that the container listens on port 3000
EXPOSE 3000

# Define the command to run your app
CMD ["node", "server.js"]

2. Build the Image

Navigate to the directory containing your Dockerfile and run:

docker build -t my-node-app .

The -t flag tags the image with a readable name, and the . tells Docker to look for the Dockerfile in the current directory.

3. Run the Container (Port Mapping & Env Vars)

Now, let's run it. We need to map the port inside the container (3000) to a port on our host machine (8080) so we can access it in our browser.

docker run -d -p 8080:3000 --name my_running_app -e NODE_ENV=production my-node-app
  • -d: Runs the container in detached mode (in the background).
  • -p 8080:3000: Maps host port 8080 to container port 3000.
  • --name: Gives the container a custom name instead of a random hash.
  • -e: Passes an environment variable into the container.

4. Adding Volumes

By default, if a container is deleted, all its data goes with it. Volumes solve this by persisting data on the host machine.

docker run -d -p 8080:3000 -v /path/on/host:/usr/src/app/data my-node-app

Multi-Container Apps with Compose

Running a Node.js app alongside a Postgres database? Use a docker-compose.yml file:

version: '3.8'
services:
  web:
    build: .
    ports:
      - "8080:3000"
    environment:
      - DB_HOST=db
    depends_on:
      - db

  db:
    image: postgres:14
    environment:
      - POSTGRES_PASSWORD=secret
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Start everything with one command: docker-compose up -d

Debugging: When Things Go Wrong

When an app crashes inside a container, you can't just open a window and look at it. You need these three commands:

  • Logs: docker logs my_running_app (Add -f to tail the logs live).
  • Exec (Shell Access): docker exec -it my_running_app /bin/sh (Opens an interactive terminal inside the running container).
  • Inspect: docker inspect my_running_app (Returns low-level JSON details about the container's configuration and network IP).

When to use Docker (and when not to)

Use Docker When:

  • You are building Microservices architectures.
  • You want absolute consistency between local dev, staging, and production.
  • You need to quickly spin up complex databases (like Redis or Postgres) locally without installing them globally on your OS.

Don't Use Docker When:

  • You are building a simple static website (just use a CDN or static host).
  • Your application requires intense, low-level access to bare-metal hardware or specific desktop GUI integrations.
  • You are compiling a heavy desktop monolith application that isn't meant for server deployment.

FAQ

What is a docker tutorial for beginners?

A Docker tutorial for beginners covers the core concepts: what an image is, how to start a container, and how to write a simple Dockerfile. It takes you from having zero container knowledge to running your first application in an isolated environment.

What are docker basics I need to know?

The absolute Docker basics involve understanding Images (the blueprint), Containers (the running application), Dockerfiles (the script to build images), and Volumes (how to persist data when containers stop or restart).

How is docker getting started different from virtual machines?

When getting started with Docker, you'll notice it is much faster and lighter than Virtual Machines. While a VM runs a full guest operating system, a Docker container shares the host's OS kernel, isolating only the application and its direct dependencies.

Why use docker for beginners?

Docker solves the "it works on my machine" problem. For beginners, it means you can download a project, run one command, and have the database, backend, and frontend all running identically to how they run in production without messing up your local computer.

How to learn docker tutorial step by step?

Start by installing Docker Desktop. Next, pull a pre-built image like "nginx" and run it. Then, try writing a Dockerfile for a simple script (like Python or Node.js). Finally, learn Docker Compose to run a web app alongside a database simultaneously.