Currently Empty: $0.00
Blog
How to Use Docker for Web Development

Using Docker for web development fundamentally changes how you set up and manage your environment, providing consistency, isolation, and portability. Docker allows you to package your application and all its dependencies (libraries, databases, configurations) into a standardized unit called a container.
This eliminates the “it works on my machine” problem, streamlines onboarding, and ensures your local setup perfectly mirrors your production environment.
Here is a complete guide on how to use Docker effectively for web development:
1. Understand Core Docker Concepts
Before diving into setup, grasp these three essential components:
- Dockerfile: A simple text file containing instructions to build a Docker image. It specifies the base operating system, code, dependencies, and execution commands.
- Image: A lightweight, stand-alone, executable package that includes everything needed to run a piece of software. It’s the blueprint.
- Container: A runnable instance of an image. It’s an isolated process running on your host operating system, complete with its own files, network, and process space.
2. Setting Up Your Application Environment
You’ll typically use two files to define your Docker setup: the Dockerfile and docker-compose.yml.
A. The Dockerfile (Building the Image)
For a typical web application (e.g., Node.js, Python/Django, or PHP/Laravel), the Dockerfile outlines the steps to create the application image.
| Step | Example (Node.js) | Purpose |
| Base Image | FROM node:18-alpine | Starts with a minimal OS containing Node.js. |
| Working Directory | WORKDIR /app | Sets the default directory inside the container. |
| Copy Dependencies | COPY package*.json ./ | Copies dependency manifests first (for caching). |
| Install Dependencies | RUN npm install | Installs dependencies. |
| Copy Application Code | COPY . . | Copies the entire application source code. |
| Expose Port | EXPOSE 3000 | Informs Docker the container listens on this port. |
| Startup Command | CMD ["npm", "start"] | Defines the command to run when the container starts. |
B. Use Docker Compose for Multi-Container Apps
Most web applications require more than one service (e.g., a web server, a database, and maybe Redis). Docker Compose lets you define and run multi-container applications using a single YAML file (docker-compose.yml).
This file defines the services, networks, and volumes (for persistent data) needed for your entire environment.
YAML
version: '3.8'
services:
# 1. Web Application Service (e.g., Node.js or Django)
web:
build: . # Build using the Dockerfile in the current directory
ports:
- "8080:3000" # Map host port 8080 to container port 3000
volumes:
- .:/app # Mount the current directory for live code changes
depends_on:
- db # Ensures the database starts before the web app
# 2. Database Service
db:
image: postgres:14-alpine # Use a pre-built PostgreSQL image
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data # Persistent storage for data
volumes:
postgres_data:
3. Essential Development Practices
These practices maximize the benefits of using Docker during local development.
A. Volume Mounting for Live Development
The volumes: - .:/app line in docker-compose.yml is critical for development. It mounts your local project directory into the container’s working directory.
- Benefit: Any code change you make on your host machine is immediately reflected inside the running container, allowing you to use hot-reloading features of your framework (like Node.js’s Nodemon or Django’s runserver) without having to rebuild the image or restart the container.
B. Separate Development and Production Configurations
Use multiple Compose files to handle environment differences:
docker-compose.yml: Your base configuration (used for local development).docker-compose.prod.yml: Overrides the base file with production settings (e.g., pulling a pre-built image instead of building locally, using different environment variables, and removing volume mounts for code).- To run production:
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
- To run production:
C. Containerized Debugging
While developing, it’s often necessary to execute commands inside a running container (e.g., running database migrations or inspecting files).
- Use
docker compose exec [service-name] [command]to run commands in the context of a running service.- Example:
docker compose exec web npm test(runs tests inside the web service container) - Example:
docker compose exec db psql -U user mydb(opens a PostgreSQL client inside the db container)
- Example:
4. Commands for Your Daily Workflow
Mastering a few simple commands streamlines your daily development cycle.
| Command | Purpose |
docker compose build | Builds or rebuilds the services defined in your docker-compose.yml. |
docker compose up | Creates and starts the containers defined in your docker-compose.yml. |
docker compose up -d | Runs the containers in detached mode (in the background). |
docker compose down | Stops and removes containers, networks, and volumes created by up. |
docker compose ps | Lists the running services/containers. |
docker compose logs -f | Follows (streams) the output logs from all services. |
Summary: The Benefits for Web Development
By adopting Docker and Docker Compose, you gain:
- Isolation: No more conflicts between dependencies of different projects on your host machine.
- Consistency: Everyone on the team (and the production server) runs the exact same environment.
- Portability: Your entire application stack (code, database, tools) can be moved and run on any machine that supports Docker.

