docker-nodejs is a utility repository designed to streamline the deployment of Node.js applications using Docker Compose. It provides automated setup scripts, environment variable management, and configuration for both development and production environments.
Before using this tool, ensure you have the following installed:
- Docker
- Docker Compose
- Git
bash(standard on Linux/macOS)
-
Clone the repository:
git clone <repository_url> cd docker-nodejs
-
Run the setup script:
./setup.sh
This script will:
- Generate a
.envfile from.env.example. - Ask you to configure
APP_NAMEand other variables in.envif it's the first run. - Setup Docker Compose and Dockerfile templates based on your
DEPLOYMENT_MODE.
- Generate a
-
Configure environment: Open the generated
.envfile and set the required variables:APP_NAME=your_app_name DEPLOYMENT_MODE=development # or productionNote:
APP_NAMEshould match the directory name of your application inside theapp/folder. -
Re-run setup: After configuring
.env, run./setup.shagain to apply changes and generate the final configuration files. -
Run the application:
docker compose up --build -d
To run your application in development mode with features like hot-reloading:
-
Configure
.env: Ensure your.envfile lists development mode:DEPLOYMENT_MODE=development
-
Run Setup: Execute
./setup.sh. This will:- Configure
docker-compose.ymlto mount your localapp/<APP_NAME>directory into the container. - Set the container command to
npm run dev. - Use
dockerfile.dev.examplewhich installs all dependencies (including devDependencies). - (Optional) Update file ownership if
RUN_SUDOERS_SETUP=Yis set, ensuring you can edit files locally without permission issues.
- Configure
-
Start the container:
docker compose up --build
You can now edit files in
app/<APP_NAME>and see changes reflected immediately.
If you want to create a new Node.js/Next.js application from scratch but don't want to install Node.js locally, follow this guide.
-
Configure
.env: Open.envand set:APP_NAME=your_new_app_name DEPLOYMENT_MODE=initialization HOST_USER_ID=1000 # Your user ID (run `id -u`) HOST_GROUP_ID=1000 # Your group ID (run `id -g`)
-
Run Setup:
./setup.sh
This will create the directory
app/<APP_NAME>if it doesn't exist and generate a minimaldocker-compose.ymlthat mounts this directory. -
Start the Initial Container:
docker compose up -d --build
-
Run Bootstrapping Commands: Use the helper script
scripts/bootstraping/run.shto execute commands inside the running container.Example: Create a Next.js app
# Uses a helper script to bypass Docker volume permission issues ./scripts/bootstraping/init-next-app.sh "npx create-next-app@latest temp --typescript --tailwind --eslint"
Example: Initialize Shadcn UI
./scripts/bootstraping/run.sh "npx shadcn@latest init"Example: Clean up/Reset directory (Handling wildcards)
# Use sh -c to ensure wildcards (*) are expanded inside the container, not your local shell ./scripts/bootstraping/run.sh sh -c "rm -rf *"
-
Switch to Development Mode: Once your project is initialized:
- Stop the init container:
docker compose down - Edit
.env: setDEPLOYMENT_MODE=development - Run
./setup.shagain to generate the full development configuration. - Start dev server:
docker compose up -d --build
- Stop the init container:
The .env file is true source of truth for the deployment configuration.
APP_NAME: The name of the subdirectory inapp/where your Node.js application code resides.DEPLOYMENT_MODE:development: Sets up volumes for live reloading, sets ownership for dev users, and usesdockerfile.dev.example.production: Optimizes for production build usingdockerfile.exampleand standard deployment settings.
NEXT_PUBLIC_*: Any variable starting withNEXT_PUBLIC_in your.envwill be automatically extracted and passed as a build argument to Docker.
The main entry point for the repository.
- Usage:
./setup.sh - Function:
- Merges
.env.examplefrom the root andapp/<APP_NAME>/. - Validates the
.envfile. - Processes
docker-compose.ymlanddockerfiletemplates usingsedfor variable substitution. - Injects
NEXT_PUBLIC_variables into the build process. - (Development) Handles sudoers setup and file permissions if
RUN_SUDOERS_SETUP=Y.
- Merges
A helper script to manage environment file updates safely.
- Usage:
scripts/update_env_file.sh <source_example_file> - Function:
- Creates a timestamped backup of the existing
.env(e.g.,.env.backup_20231024...). - Updates
.envwith new keys from the example file while preserving existing values from the latest backup. - Rotates backups (keeping the last 3).
- Ensures correct file ownership.
- Creates a timestamped backup of the existing
app/: Place your Node.js application source code in a subdirectory here (e.g.,app/my-web-app/).scripts/: Contains helper scripts for environment updates and deployment tasks.setup.sh: Main configuration automation script.docker-compose.yml.example&dockerfile.*.example: Templates used bysetup.shto generate actual configuration files.