Back to blog
Mar 11, 2025
5 min read

How to Set Up Docker for Local Web Development

Learn how to install and configure Docker for local web development, containerize applications, and streamline development workflows.

How to Set Up Docker for Local Web Development

Docker is a powerful tool for developers that simplifies setting up and managing development environments. It enables you to containerize applications, ensuring that they run the same way on any machine.

This guide will walk you through installing Docker, setting up containers, and using Docker Compose for a streamlined local development workflow.


1. Why Use Docker for Local Development?

Traditional local development setups require installing dependencies, configuring environments, and managing version conflicts. Docker solves these issues by:

Creating isolated environments → Avoids dependency conflicts. ✔ Simplifying project setup → A single command sets up the environment. ✔ Providing consistent environments → Works identically on any machine. ✔ Easily switching between projects → No need to install multiple versions of services. ✔ Enabling collaboration → Developers can share Docker configurations.

Docker is particularly useful for microservices, full-stack applications, and CI/CD pipelines.


2. Installing Docker

A. Install Docker Desktop

  1. Download Docker Desktop from Docker’s official website.
  2. Install Docker Desktop and restart your system if prompted.
  3. Open a terminal and verify the installation:
docker --version

If Docker is installed correctly, you will see the version number.


3. Understanding Docker Concepts

Before running Docker, it’s important to understand key concepts:

ConceptDescription
ImageA template containing the application and environment.
ContainerA running instance of an image.
DockerfileA script defining how an image is built.
VolumesPersistent storage for containers.
Docker ComposeA tool to manage multi-container applications.

4. Creating Your First Docker Container

Let’s start by running a simple container:

docker run hello-world

This command:

  1. Pulls the hello-world image from Docker Hub.
  2. Runs a container from that image.
  3. Prints a success message.

5. Setting Up a Web Development Environment

A. Creating a Simple Web Server with Docker

Create a new project folder and add a Dockerfile:

# Use an official Node.js image
FROM node:18

# Set the working directory
WORKDIR /app

# Copy project files
COPY . .

# Install dependencies
RUN npm install

# Expose the application port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

Now, build and run the container:

docker build -t my-node-app .
docker run -p 3000:3000 my-node-app

Visit http://localhost:3000/ to see your app running.


6. Using Docker Compose for Multi-Container Development

Docker Compose allows you to run multiple services (e.g., a database and a backend) with a single command.

A. Setting Up Docker Compose

Create a docker-compose.yml file:

version: "3.8"
services:
web:
image: node:18
container_name: web-app
working_dir: /app
volumes:
- .:/app
ports:
- "3000:3000"
command: npm start
db:
image: postgres:15
container_name: database
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
ports:
- "5432:5432"

Now, run both services:

docker-compose up -d

Your web app and PostgreSQL database are now running in Docker.


7. Managing Containers

A. Checking Running Containers

docker ps

B. Stopping a Container

docker stop container_id

C. Removing a Container

docker rm container_id

D. Removing an Image

docker rmi image_id

8. Persisting Data with Docker Volumes

Docker containers are temporary, so data is lost when a container stops. To persist data, use Docker Volumes:

docker volume create mydata
docker run -v mydata:/data ubuntu

This ensures data remains even if the container is removed.


9. Deploying Dockerized Apps

A. Pushing an Image to Docker Hub

  1. Log in to Docker Hub:
docker login
  1. Tag the image:
docker tag my-node-app username/my-node-app
  1. Push the image:
docker push username/my-node-app

B. Deploying to a Cloud Provider

You can deploy your Dockerized app to AWS, DigitalOcean, or Kubernetes.

Example: Deploying to AWS:

aws ecs create-service --cluster my-cluster --service-name my-app --task-definition my-task

10. Troubleshooting Common Docker Issues

IssueSolution
Docker not runningRestart Docker Desktop
Port conflictsStop other services using the same port (lsof -i :3000)
Container won’t startCheck logs: docker logs container_id
Image too largeUse smaller base images (alpine, slim)

Conclusion

Docker simplifies local web development by creating consistent, reproducible environments. By using containers and Docker Compose, you can develop and test applications efficiently.

Key Takeaways

Use Docker to containerize applications for consistency. ✔ Use Docker Compose for multi-service applications. ✔ Persist data using Docker Volumes. ✔ Easily deploy Dockerized apps to the cloud.

Start using Docker today to streamline your development workflow!


Disclaimer

Article written with the help of AI.

This blog post is for informational purposes only and is not affiliated with or endorsed by any mentioned company. References are for discussion, not promotion. Some information may be inaccurate, readers should verify independently before making decisions.