Docker and REST API: Building a Local Development Environment with Mock Services, Proxy, and Hot Reload for Microservices
Introduction: Why Local Microservices Development Is a Pain
When a project transitions from a monolith to a microservices architecture, the local development environment becomes a serious challenge. Each service has its own dependencies, runtime versions, database configurations, and external REST APIs that aren't available on a developer's laptop. The classic pain points look like this:
- Version conflicts — one service on Go 1.21, another on Go 1.23, PHP 8.2 alongside PHP 8.4.
- External dependencies — payment gateways, SMS providers, and third-party REST APIs are unavailable in dev mode or charge per use.
- Reproducibility — "works on my machine, fails in CI" becomes the team's mantra.
- Feedback speed — rebuilding images on every code change kills productivity.
Docker solves most of these problems, but only when properly organized. In this article, we'll build a fully functional local environment for microservices with a mock REST API, reverse proxy, hot reload, and test data management.
Docker Compose as the Foundation of a Local Environment: Best Practices 2026
Docker Compose remains the de facto standard for orchestrating local environments. By 2026, the Compose v2 specification has stabilized, and the key practices are as follows.
Override Files and Profiles
Split your configuration into a base file and environment-specific overrides. The docker-compose.yml file describes the general topology, while docker-compose.override.yml is automatically applied in dev mode.
# docker-compose.yml — base configuration
services:
api-gateway:
image: myapp/api-gateway:${TAG:-latest}
networks:
- internal
labels:
- "traefik.enable=true"
- "traefik.http.routers.gateway.rule=Host(`api.localhost`)"
user-service:
image: myapp/user-service:${TAG:-latest}
networks:
- internal
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:-devpassword}
POSTGRES_DB: users
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
networks:
- internal
networks:
internal:
driver: bridge
# docker-compose.override.yml — dev-specific configuration
services:
user-service:
build:
context: ./user-service
dockerfile: Dockerfile.dev
volumes:
- ./user-service:/app
environment:
- APP_ENV=development
- LOG_LEVEL=debug
postgres:
ports:
- "5432:5432" # expose port in dev only
volumes:
- ./data/postgres:/var/lib/postgresql/data
Profiles let you start optional services on demand. For example, monitoring tools or admin panels aren't needed all the time:
# Start core services
docker compose up -d
# Start with the monitoring profile
docker compose --profile monitoring up -d
# In docker-compose.yml:
services:
pgadmin:
image: dpage/pgadmin4
profiles: ["monitoring"]
environment:
PGADMIN_DEFAULT_EMAIL: admin@localhost
PGADMIN_DEFAULT_PASSWORD: admin
Mock Services for External REST APIs: WireMock and Mockoon in Docker
Instead of hitting real external APIs during development, use mock servers. This eliminates network dependency, speeds up tests, and lets you simulate edge cases — timeouts, 5xx errors, and unusual responses.
WireMock in Docker Compose
WireMock is a powerful tool for simulating REST APIs with support for request matching rules, delays, and scenarios.
# docker-compose.override.yml — adding WireMock
services:
wiremock:
image: wiremock/wiremock:3.4.2
command: --port 8080 --verbose
volumes:
- ./mocks/wiremock:/home/wiremock
networks:
- internal
ports:
- "8080:8080"
Create a mapping file at ./mocks/wiremock/mappings/payment-api.json:
{
"mappings": [
{
"request": {
"method": "POST",
"url": "/v1/payments",
"headers": {
"Content-Type": {"contains": "application/json"}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"id": "pay_mock_123",
"status": "succeeded",
"amount": 1000
},
"fixedDelayMilliseconds": 50
}
},
{
"request": {
"method": "POST",
"url": "/v1/payments/fail"
},
"response": {
"status": 402,
"jsonBody": {
"error": "card_declined",
"message": "Your card was declined."
}
}
}
]
}
In Go or PHP/Laravel services, use an environment variable to switch between the real API and the mock:
# .env.dev
PAYMENT_API_URL=http://wiremock:8080
PAYMENT_API_KEY=mock-key-dev
Mockoon as an Alternative
Mockoon is easier to set up and includes a GUI for creating mocks. In Docker, it runs via a CLI image:
services:
mockoon:
image: mockoon/cli:latest
command: --data /data/api-mock.json --port 3000
volumes:
- ./mocks/mockoon:/data
networks:
- internal
Reverse Proxy for Local Development: Traefik in Docker Compose
When you have 5–10 microservices, working with ports like localhost:3001, localhost:3002 gets unwieldy. Traefik lets you configure domain-based routing directly through Docker container labels.
# docker-compose.yml — adding Traefik
services:
traefik:
image: traefik:v3.2
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.localresolver.acme.tlschallenge=true"
ports:
- "80:80"
- "443:443"
- "8090:8080" # Traefik Dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./traefik/certs:/certs
networks:
- internal
user-service:
labels:
- "traefik.enable=true"
- "traefik.http.routers.user.rule=Host(`user.localhost`)"
- "traefik.http.routers.user.entrypoints=web"
- "traefik.http.services.user.loadbalancer.server.port=8000"
order-service:
labels:
- "traefik.enable=true"
- "traefik.http.routers.order.rule=Host(`order.localhost`) && PathPrefix(`/api`)"
- "traefik.http.routers.order.entrypoints=web"
- "traefik.http.services.order.loadbalancer.server.port=8001"
For local HTTPS, add *.localhost to /etc/hosts or use mkcert to generate self-signed certificates that browsers accept without warnings. Once configured, each service is accessible via a URL like http://user.localhost, which greatly improves the Developer Experience.
Hot Reload for Go Services in Docker: Air and Watchers
Manually rebuilding the Go binary on every file change is unacceptable. The Air tool solves this by watching for changes and automatically restarting the application inside the container.
Dockerfile for Go with Hot Reload
# Dockerfile.dev for a Go service
FROM golang:1.23-alpine AS dev
WORKDIR /app
# Install Air
RUN go install github.com/air-verse/air@latest
# Cache dependencies separately
COPY go.mod go.sum ./
RUN go mod download
# Source code is mounted via volume — not copied
EXPOSE 8000
CMD ["air", "-c", ".air.toml"]
Air Configuration (.air.toml)
[build]
cmd = "go build -o ./tmp/main ./cmd/server"
bin = "./tmp/main"
full_bin = "./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["tmp", "vendor", "testdata"]
delay = 1000 # milliseconds before restart
kill_delay = "0.5s"
rerun = false
[log]
time = true
[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
clean_on_exit = true
docker-compose.override.yml for Go with Hot Reload
services:
user-service:
build:
context: ./user-service
dockerfile: Dockerfile.dev
volumes:
- ./user-service:/app # mount source code
- go-mod-cache:/go/pkg/mod # cache modules between runs
environment:
- CGO_ENABLED=0
- GOPROXY=direct
volumes:
go-mod-cache:
An important note: use a named volume for the Go module cache (go-mod-cache) to avoid downloading dependencies on every docker compose up. This reduces cold start time from several minutes to just seconds.
Hot Reload for PHP/Laravel: Volume Mount, OPcache, and Octane
For PHP projects using Laravel, the hot reload approach differs from Go because PHP is an interpreted language — file changes are picked up without restarting the process.
Basic Laravel Configuration in Docker
# Dockerfile.dev for PHP/Laravel
FROM php:8.4-fpm-alpine AS dev
RUN apk add --no-cache \
git curl zip unzip \
libpng-dev libjpeg-dev \
&& docker-php-ext-install pdo_mysql pdo_pgsql opcache
# OPcache for dev: disable caching
COPY ./docker/php/opcache-dev.ini /usr/local/etc/php/conf.d/opcache.ini
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
CMD ["php-fpm"]
; docker/php/opcache-dev.ini — OPcache in development mode
opcache.enable=1
opcache.revalidate_freq=0 ; check for changes on every request
opcache.validate_timestamps=1 ; required for hot reload
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
# docker-compose.override.yml for Laravel
services:
laravel-app:
build:
context: ./laravel-app
dockerfile: Dockerfile.dev
volumes:
- ./laravel-app:/var/www/html # code mounted directly
- vendor-cache:/var/www/html/vendor # vendor as a separate volume
environment:
- APP_ENV=local
- APP_DEBUG=true
- DB_HOST=postgres
- CACHE_DRIVER=redis
- QUEUE_CONNECTION=redis
volumes:
vendor-cache:
Laravel Octane in Development Mode
Laravel Octane (Swoole or FrankenPHP) significantly speeds up PHP applications, but requires special configuration for hot reload. With Octane, use the --watch flag together with a file watcher utility:
services:
laravel-octane:
command: php artisan octane:start --server=frankenphp --host=0.0.0.0 --port=8000 --watch
volumes:
- ./laravel-app:/var/www/html
environment:
- OCTANE_HTTPS=false
The --watch flag in Octane monitors PHP file changes and reloads workers without a full server restart, providing nearly the same feedback speed as Go with Air.
Shared Networks and Container Communication: DNS and Service Discovery
Docker Compose automatically creates an internal DNS system for containers on the same network. The service name from docker-compose.yml becomes the DNS name. This allows a Go service to reach a Laravel service by name — http://laravel-app:8000 — rather than by IP address.
# Example network configuration for service isolation
networks:
frontend: # network for services accessible via Traefik
driver: bridge
backend: # internal network for inter-service communication
driver: bridge
data: # network for databases
driver: bridge
services:
traefik:
networks: [frontend, backend]
api-gateway:
networks: [frontend, backend]
user-service:
networks: [backend, data]
postgres:
networks: [data]
To debug inter-service communication, use a temporary container with network utilities:
docker compose run --rm --network internal alpine sh -c "nslookup user-service && wget -qO- http://user-service:8000/health"
Test Data Management: Seed Scripts, Fixtures, and DB Snapshots
Reproducible test data is the key to stable local development. Here are several approaches:
Seed Scripts on Container Start
services:
postgres:
image: postgres:16-alpine
volumes:
- ./docker/postgres/init:/docker-entrypoint-initdb.d # scripts run on first start
- postgres-data:/var/lib/postgresql/data
Place SQL files in ./docker/postgres/init/ in execution order: 01-schema.sql, 02-seed-users.sql, 03-seed-orders.sql.
DB Snapshots for Fast Recovery
# Create a snapshot
docker compose exec postgres pg_dump -U postgres users > ./snapshots/users_$(date +%Y%m%d).sql
# Restore from a snapshot
cat ./snapshots/users_20260115.sql | docker compose exec -T postgres psql -U postgres users
# Full data reset to initial state
docker compose down -v && docker compose up -d
Fixtures in Laravel
In Laravel projects, use Seeders via Artisan, run inside the container:
docker compose exec laravel-app php artisan migrate:fresh --seed
Faster Builds: Layer Caching, BuildKit, and Parallel Builds
Slow Docker image builds are one of the top developer complaints. BuildKit, enabled by default since Docker 23+, solves most of these issues.
# Enable BuildKit (if not already enabled by default)
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Build all services in parallel
docker compose build --parallel
# Build with detailed progress output
docker compose build --progress=plain user-service
Optimizing Dockerfile Layer Caching
# Correct layer order in a Go Dockerfile
FROM golang:1.23-alpine AS builder
WORKDIR /app
# Copy only go.mod and go.sum first
COPY go.mod go.sum ./
# This layer is cached as long as dependencies don't change
RUN go mod download
# Then copy the source code
COPY . .
RUN CGO_ENABLED=0 go build -o /app/server ./cmd/server
# Minimal final image
FROM scratch
COPY --from=builder /app/server /server
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["/server"]
BuildKit Cache Mounts for Go Modules and PHP Composer
# Using BuildKit cache mount
# syntax=docker/dockerfile:1
FROM golang:1.23-alpine
WORKDIR /app
COPY go.mod go.sum ./
# Module cache is preserved between builds on the host
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 go build -o ./tmp/main ./cmd/server
The same approach for PHP/Composer:
# syntax=docker/dockerfile:1
FROM php:8.4-fpm-alpine
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY composer.json composer.lock ./
RUN --mount=type=cache,target=/root/.composer/cache \
composer install --no-dev --no-scripts --optimize-autoloader
Moving from Docker Compose to Kubernetes: Minimizing the Gap Between Local and Prod
One of the biggest risks of using Docker Compose for local development is the large gap between the local environment and production Kubernetes. Here's how to minimize it.
Kompose for Conversion
The kompose utility converts docker-compose.yml into Kubernetes manifests. It's not a silver bullet, but it gives you a solid starting point:
# Install kompose
curl -L https://github.com/kubernetes/kompose/releases/download/v1.33.0/kompose-linux-amd64 -o kompose
chmod +x kompose && sudo mv kompose /usr/local/bin/
# Convert
kompose convert -f docker-compose.yml -o ./k8s/
Using Helm Values for Different Environments
Design Helm charts with values-dev.yaml and values-prod.yaml so that the same templates work in both environments. Environment variables you use in Docker Compose via .env should be represented in Kubernetes as ConfigMap and Secret.
Local Kubernetes with kind or minikube
When Docker Compose no longer reflects the reality of production, switch to local Kubernetes with kind (Kubernetes in Docker):
# Create a local cluster
kind create cluster --config ./kind-config.yaml
# Load a local image into kind
kind load docker-image myapp/user-service:dev
# Deploy via Helm
helm upgrade --install user-service ./charts/user-service \
--values ./charts/user-service/values-dev.yaml
The key principle: keep Docker Compose and Kubernetes manifests in sync through shared environment variables and unified images. The difference should only be in orchestration, not in application configuration.
Summary: The Complete Local Environment Picture
The environment we've built includes:
- Docker Compose with override files and profiles — a reproducible foundation for the entire team.
- WireMock — isolation from external REST APIs, simulation of edge cases.
- Traefik — domain-based routing instead of ports, bringing you closer to a production-like topology.
- Air — hot reload for Go services without rebuilding images.
- Laravel Octane with the
--watchflag — instant feedback for PHP developers. - BuildKit cache mounts — builds in seconds instead of minutes.
- Seed scripts and snapshots — reproducible test data.
This environment reduces the time from code change to visible result to 1–3 seconds for most operations and completely eliminates the "works on my machine" problem. Investment in a proper local environment pays off within the second week of active microservices development.
Technologies
Tags
Ruslan Ismailov
Senior Web / Backend Developer. Senior web/backend developer with 9 years of experience. Stack: PHP, Laravel, PostgreSQL, Redis, Docker, Kubernetes, REST, microservices, CI/CD. More about me →