Docker Local Development Guide

Run MySQL, Redis, PostgreSQL, Nginx, and more on localhost using Docker and Docker Compose to unify your local development environment.


Docker is the most commonly used environment solution for modern local development, isolating dependencies through containers. Databases, caches, and reverse proxies can be mapped to localhost ports for integration with Node, PHP, and Python applications on the host machine.

Typical localhost mappings

ServiceContainer PortHost Access
MySQL3306localhost:3306 (TCP)
PostgreSQL5432localhost:5432
Redis6379localhost:6379
Nginx80http://localhost:8080
Node App3000http://localhost:3000

The mapping format is -p host_port:container_port, for example, -p 8080:80 means that accessing http://localhost:8080 in the browser routes to port 80 of the Nginx container.

Docker Compose Example

docker-compose.yml:

services:
  db:
    image: mysql:8
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: app
  redis:
    image: redis:7
    ports:
      - '6379:6379'
  web:
    build: .
    ports:
      - '3000:3000'
    depends_on:
      - db
      - redis

To start:

docker compose up -d

The application connection string uses the host perspective: set the database host to localhost (or 127.0.0.1) and the port to the mapped port.

Comparison with XAMPP

ItemDockerXAMPP
IsolationContainer level, no interference between projectsGlobal installation, prone to version conflicts
Startupdocker compose upControl panel Start
Use CaseFull stack, microservices, unified team environmentQuick PHP/MySQL setup

Common Commands

docker ps                    # Running containers
docker compose logs -f web   # View logs
docker compose down          # Stop and remove containers
docker exec -it <id> bash    # Enter the container

Frequently Asked Questions

Port already in use
Change the host port on the left side of ports, for example, 3307:3306; or stop any existing MySQL/XAMPP services on your machine.

Slow file mounting on Mac/Windows
For projects with many small files, consider placing node_modules in a named volume or using Dev Containers.

Accessing the host from within the container
On Linux, you can use host.docker.internal (also supported by Docker Desktop) to access APIs running on the host machine.

Summary

Docker maps services to various ports on localhost, providing a modern alternative to “installing a bunch of databases on the local machine”; when combined with Compose, it can easily replicate the team’s production environment.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run