Containerized Local Development Environment Setup Guide
This guide explains how to build a containerized local development environment, covering why containerization solves dependency conflicts, hardware and OS selection, step-by-step container runtime installation, multi-service orchestration with a declarative file, image size reduction and build cache usage, and common troubleshooting. It helps developers create reproducible, disposable, and easily rebuildable environments for running multiple runtime versions and services in parallel.
1. Why Containerize Your Local Development Environment
In the traditional development model, project dependencies are installed directly on the host operating system. As the number of projects grows, their requirements for runtime versions, extension libraries, and database engines often conflict with one another. One project needs an older runtime version while another requires a newer one; certain services listen on the same port by default; and incomplete uninstalls leave behind a large amount of residual configuration. Together, these issues are the root cause of “it works on my machine, but breaks on another.”
Container technology packages an application and its dependencies into lightweight, isolated units. Each unit has its own filesystem view, process space, and network stack. Compared with virtual machines, containers share the host kernel, so they have low startup overhead and high density. Compared with direct installation, containers are declarative, disposable, and rebuildable. The environment is no longer a “configured state” but a “described artifact”—which is exactly the core value of a reproducible development environment.
It is worth emphasizing that containers solve packaging and isolation problems; they do not automatically solve every engineering problem. Data volumes, port mapping, dependency startup order, and image size still need to be designed proactively by developers, as detailed below.
2. Resource and System Selection Recommendations
Containers themselves have low overhead, but a development machine usually needs to run multiple services at the same time, so resource planning should leave some headroom.
- CPU: 2 or more cores is a safer choice, providing smoother scheduling when multiple containers build concurrently or run tests.
- Memory: 2G is enough for basic use, but if you start multiple services such as a database, cache, and message queue at the same time, 4G or more is recommended.
- System disk: reserve at least 20G. Image layers, build cache, logs, and data volumes will keep consuming space, so long-term use requires regular cleanup.
- Operating system: prefer a server distribution with a long-term support release, as these distributions have the most complete documentation and compatibility in the container ecosystem.
If you choose to set up on a cloud host, most cloud providers offer images with the container runtime preinstalled, so you can skip the installation step. If you use bare metal or a local virtual machine, you need to install it manually. There is no essential difference between the two approaches in subsequent use.
3. Installing and Initializing the Container Runtime
Taking a common server distribution as an example, the installation process can be summarized in six steps. The core idea is: remove historical versions, prepare dependencies, add a trusted software source, install the runtime, verify the version, and configure auto-start.
Step one, remove old versions to avoid conflicts between binaries and configurations:
sudo apt remove docker docker-engine docker.io containerd runc
Step two, update the index and install basic dependencies for subsequent key verification and transfer:
sudo apt updatesudo apt install -y ca-certificates curl gnupg lsb-release
Step three, import the official signing public key to ensure the software source is trusted:
sudo mkdir -p /etc/apt/keyringscurl -fsSL <official key URL> | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Step four, write the software source configuration, with the architecture and distribution codename detected automatically:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] <official repository URL> $(lsb_release -cs) stable" \| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step five, install the engine, CLI components, build extension, and orchestration plugin:
sudo apt updatesudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step six, verify and set it to start on boot:
docker --versiondocker compose versionsudo systemctl enable --now docker
If you want to use it without sudo, you can add the current user to the container group, but this operation is equivalent to granting higher privileges. Evaluate the security impact and log in again for it to take effect.
4. Orchestrating Multiple Services: Describing the Environment with a Declarative File
A single container is suitable for packaging a single process, but a real development environment is often a combination of “runtime + database + cache.” An orchestration tool describes services, networks, and data volumes in one declarative file, and a single command can create, start, and destroy the entire environment.
An example configuration is as follows:
services:app:build: .ports:- "8080:8080"environment:DB_HOST: dbCACHE_HOST: cachedepends_on:- db- cachedb:image: <database image>volumes:- db_data:/var/lib/mysqlcache:image: <cache image>volumes:db_data:
Several key design points are worth noting. Services access each other by service name through the built-in network, without hardcoding addresses; data is persisted through named volumes, so it remains after the container is rebuilt; ports are mapped only for services that need to be exposed, while the rest remain internally reachable, reducing the attack surface; depends_on only controls startup order, so if the application needs to wait until dependencies are truly ready, retry or health check logic should be implemented on the application side.
5. Image Slimming and Build Cache
Image size directly affects pull speed, build time, and disk usage. Practical recommendations include: use official minimal base images; copy the dependency manifest first, then install dependencies, and copy the source code last to maximize build cache usage; combine install and cleanup commands in the same layer to avoid cache residue; use multi-stage builds to separate build artifacts from the runtime environment; and use an ignore file to exclude dependency directories, version control directories, and local configuration.
Cleanup strategy is equally important. Regularly removing dangling images, stopped containers, and unused networks can reclaim a lot of space; build cache can be reclaimed separately. Before cleanup, be sure to confirm that no volumes are in use to avoid accidentally deleting data.
6. Common Troubleshooting Approaches
- Port already in use: adjust the host mapping port, or stop the conflicting process.
- Container starts and exits immediately: check the container logs; it is usually missing configuration or dependencies not ready.
- Disk keeps growing: check image and build cache usage, and build a habit of regular cleanup.
- Inconsistent time zone and time: explicitly mount the time zone configuration or set environment variables.
- Abnormal file permissions: check the mapping between the user running inside the container and the owner of the host directory.
7. Summary
The essence of a containerized local development environment is to transform the environment from a “manually maintained state” into a “versionable description.” After mastering the five sets of capabilities—runtime installation, orchestration declaration, data persistence, image optimization, and troubleshooting—coexisting multiple runtime versions, running multiple services in parallel, and rebuilding the environment at any time all become routine operations. To further reduce maintenance costs, you can put the orchestration file under version control and use common capabilities such as cloud container platforms, image registries, and logging and monitoring alerts to smoothly extend the local environment to team sharing and production deployment stages.
