explainx.ainewsletter3.5k
TrendingNewsPathwaysSkills
Pricing
explainx.ai

Upskill in AI — 16 free pathways, live workshops & bootcamps, and 50+ courses from practitioners. Plus the skills, tools, and MCP servers to practice on.

follow us

custom AI agents

[email protected]

get started

Find your pathTake Free Evaluation

learn

pathways — start freeworkshopsbootcampscoursescertificationsmock testsexplainx universitycorporate traininglearn skills & mcp

discover

skillsmcp serversexplainx mcptoolsagentsllmsdesignsagi trackerranks

company

aboutvisionmissionteaminstructorscommunityhackathonscareers

content

daily AI newsstate of AI — live resultsblogreleasespromptsgeneratorsresource librarydemofor LLMs

solutions

all solutionsdeveloper upskillingmarketing upskillingproduct manager upskillingleadership upskilling

More from us

InfloqInfluencer marketingBgBlurPrivacy-first blurOlly SocialSocial AI copilotCeptoryVideo intelligenceBgRemoverBackground removal

newsletter · weekly

Get AI news, tools, and insights in your inbox.

supportprivacytermsdata rightssubmission guidelines

© 2026 AISOLO Technologies Pvt Ltd

On this page

  • Three concepts you need before anything else
  • Step 1: Install Docker Desktop
  • Step 2: Understand the Dockerfile
  • Step 3: Build your first image
  • Step 4: Run your container
  • Step 5: Run in the background (detached mode)
  • Essential Docker commands
  • Step 6: Add a .dockerignore file
  • Step 7: Build a Python image (same pattern, different base)
  • What changes between runs vs rebuilds
  • What to learn next
← Back to blog

explainx / blog

What is Docker? How to Build Your First Docker Image (Beginner Guide 2026)

Docker explained from scratch: what containers are, how to install Docker Desktop, and how to build and run your first Docker image — step by step with real commands. No prior experience needed.

Jun 27, 2026·5 min read·Yash Thakker
DockerBeginner GuideContainersDevOpsDeveloper Tools
go deep
What is Docker? How to Build Your First Docker Image (Beginner Guide 2026)

Docker lets you package an application and everything it needs to run — code, runtime, libraries, settings — into a single portable unit called a container. That container runs the same way on any machine that has Docker installed.

This guide explains the core concepts, walks you through installing Docker, and gets you to a running container with your own Dockerfile.

What Docker is and how to build and run your first container from scratch.

Three concepts you need before anything else

Image — a read-only template that describes what your container will contain: the OS layer, the runtime (Node, Python, etc.), your code, and any configuration.

Container — a running instance of an image. Like a process that was spawned from the image blueprint.

Dockerfile — a text file with instructions that tell Docker how to build an image. Step by step: start from this base image, copy these files, install these packages, run this command.

The relationship:

snippet
Dockerfile → (build) → Image → (run) → Container

Step 1: Install Docker Desktop

Docker Desktop is the easiest way to get Docker running on your machine. It includes Docker engine, CLI, and a GUI dashboard.

macOS

  1. Go to docker.com/products/docker-desktop
  2. Download the macOS version for your chip — Apple Silicon (M1/M2/M3/M4) or Intel
  3. Open the .dmg, drag Docker to Applications
  4. Open Docker from Applications — it runs in the menu bar

Windows

  1. Go to docker.com/products/docker-desktop
  2. Download the Windows installer
  3. Run the installer — it will enable WSL 2 (Windows Subsystem for Linux) if not already on
  4. Restart if prompted
  5. Open Docker Desktop from the Start menu

Verify installation

Open your terminal (Terminal on Mac, PowerShell or Command Prompt on Windows):

bash
docker --version

You should see something like Docker version 27.x.x. Also check:

bash
docker run hello-world

Docker will pull the hello-world image and run it. If you see "Hello from Docker!", everything works.


Step 2: Understand the Dockerfile

A Dockerfile is a recipe. Here is the most common pattern:

dockerfile
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

Line by line:

InstructionWhat it does
FROM node:20-alpineStart from the official Node.js 20 image (Alpine Linux variant — small and fast)
WORKDIR /appSet the working directory inside the container to /app
COPY package*.json ./Copy your package.json into the container
RUN npm installRun this command during the image build — installs dependencies
COPY . .Copy all your project files into the container
EXPOSE 3000Tell Docker your app listens on port 3000
CMD ["node", "index.js"]The command that runs when the container starts

Step 3: Build your first image

Create a project folder and navigate into it:

bash
mkdir my-docker-app
cd my-docker-app

Create a simple index.js file:

bash
cat > index.js << 'EOF'
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from Docker!\n');
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});
EOF

Create a package.json:

bash
cat > package.json << 'EOF'
{
  "name": "my-docker-app",
  "version": "1.0.0",
  "main": "index.js"
}
EOF

Create the Dockerfile (no extension):

bash
cat > Dockerfile << 'EOF'
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]
EOF

Now build the image:

bash
docker build -t my-docker-app .
  • -t my-docker-app — gives the image the name my-docker-app
  • . — tells Docker to look for the Dockerfile in the current directory

You'll see Docker execute each step. The first build takes a minute because it downloads the base image. Subsequent builds are much faster because Docker caches each layer.


Step 4: Run your container

bash
docker run -p 3000:3000 my-docker-app
  • -p 3000:3000 — maps port 3000 on your laptop to port 3000 inside the container

Open your browser and go to http://localhost:3000. You'll see "Hello from Docker!"

Press Ctrl + C to stop the container.


Step 5: Run in the background (detached mode)

The previous command ran the container in your terminal window. To run it in the background:

bash
docker run -d -p 3000:3000 --name my-app my-docker-app
  • -d — detached mode (runs in the background)
  • --name my-app — gives the container a human-readable name

The container is running now without occupying your terminal. Check it:

bash
docker ps

This lists all running containers. You'll see my-app in the list.


Essential Docker commands

bash
# List running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# List all local images
docker images

# Stop a running container
docker stop my-app

# Start a stopped container
docker start my-app

# Remove a container
docker rm my-app

# Remove an image
docker rmi my-docker-app

# View container logs
docker logs my-app

# Follow logs in real time
docker logs -f my-app

# Run a command inside a running container
docker exec -it my-app sh

Step 6: Add a .dockerignore file

Just like .gitignore tells Git which files to ignore, .dockerignore tells Docker which files to exclude from the image build. This keeps images small and prevents accidentally copying secrets.

Create .dockerignore:

snippet
node_modules
.env
.git
*.log
Dockerfile
.dockerignore

Always exclude node_modules — they get installed inside the container during RUN npm install, so you don't need to copy them in from your machine.


Step 7: Build a Python image (same pattern, different base)

The Dockerfile pattern is the same for any language. Here's a Python example:

Create a new folder:

bash
mkdir my-python-app
cd my-python-app

Create app.py:

python
print("Hello from a Python container!")

Create requirements.txt (empty for now):

bash
touch requirements.txt

Create the Dockerfile:

dockerfile
FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Build and run:

bash
docker build -t my-python-app .
docker run my-python-app

You'll see "Hello from a Python container!" printed in your terminal.


What changes between runs vs rebuilds

ScenarioWhat to do
Changed code, same dependenciesdocker build again — the COPY . . layer rebuilds
Added new packagesUpdate package.json or requirements.txt, then docker build again
Just want to restart the appdocker stop my-app && docker start my-app
Want a fresh container from the same imagedocker run again

The layered caching means rebuilds are fast when only your code changes — Docker reuses the cached layers for npm install / pip install.


What to learn next

  • Docker Compose — run multiple containers together (e.g., your app + a database) with a single docker-compose.yml file
  • Docker Hub — publish your images publicly so anyone can pull them with docker pull
  • Volumes — persist data between container restarts
  • Environment variables — pass config to containers with -e KEY=value or a .env file
  • Multi-stage builds — build images with a compiler stage and a smaller runtime stage, reducing final image size

The core skill you've built here — Dockerfile → build → run — is the foundation everything else sits on.

Yash Thakker

Written by

Yash Thakker

Yash is an AI expert with over 300K learners. Join his workshops →

Related posts

Aug 3, 2026

Cloudflare Computer: Agents Need Isolates, Not Just Containers

August 3, 2026: Cloudflare launched an early preview of @cloudflare/computer — a Durable Object workspace that routes agent work across fast isolates and full Linux containers against one filesystem. explainx.ai breaks down the architecture, how it compares to Vercel eve sandboxes, and when to try it.

Jun 27, 2026

Python Basics: How to Install Python and Write Your First Script (2026 Guide)

Install Python, run your first script, understand variables and functions, and set up a virtual environment — the complete beginner Python setup guide for 2026 with real commands and exercises.

Jun 27, 2026

What is Cursor? How to Build Your First HTML Project with AI (2026 Guide)

Cursor explained from scratch: how to install it, how the AI works, and how to build a real HTML page from a plain-English description. Your first AI-assisted project in under 30 minutes.