EJS: Universal Espress.js Docker Container
1.2K
A universal Docker container for Node.js (Express.js) applications based on Alpine Linux. Supports both development and production, pre-installed with SQLite, curl, jq, nano and more. Includes healthcheck support, dynamic dependency installation, and hot-reload (Nodemon) in development mode.
node:lts-alpine3.20.env variables automaticallystart.sh/healthDockerfile
start.sh
/app/
āā package.json
āā index.js / app.js / main.js / server.js / src/index.js / dist/index.js
āā node_modules/
The following variables are required for secure startup:
JWT_SECRET: Your app's JWT secret key.JWT_EXPIRES_IN: JWT token expiration time.Optional variables:
NODE_ENV: Runtime environment (production by default, use development for hot-reload).PORT: Port exposed by your app (default is 3000).You can define them in your shell, .env file, or with -e in Docker.
docker build -t humbertovarona/ejs .
docker run -d -p 3000:3000 \
--name test_ejs \
-v $(pwd)/app:/usr/src/app \
-e JWT_SECRET=your_jwt_secret \
-e JWT_EXPIRES_IN=1d \
humbertovarona/ejs
docker run -d -p 3000:3000 \
--name test_ejs \
-e NODE_ENV=development \
-v $(pwd)/app:/usr/src/app \
-e JWT_SECRET=your_jwt_secret \
-e JWT_EXPIRES_IN=1d \
humbertovarona/ejs
Note:
nodemonmust be installed as a dependency (indevDependencies).
npm install --omit=devnode (searches for standard entrypoints)npm install)nodemon is found, runs your app with hot-reloadYou can use Docker Compose to manage multi-profile environments (production, development, test, etc.).
docker-compose.yml:
version: "3"
services:
app-prod:
image: humbertovarona/ejs:latest:1.0
container_name: app-express-prod
restart: unless-stopped
environment:
NODE_ENV: production
JWT_SECRET: "YOUR_SECRET_KEY"
JWT_EXPIRES_IN: "2h"
PORT: 3000
ports:
- "3000:3000"
volumes:
- ./app:/usr/src/app
app-dev:
image: humbertovarona/ejs:latest:1.0
container_name: app-express-dev
restart: unless-stopped
environment:
NODE_ENV: development
JWT_SECRET: "YOUR_SECRET_KEY"
JWT_EXPIRES_IN: "2h"
PORT: 3001
ports:
- "3001:3000"
volumes:
- ./app:/usr/src/app
- ./db/database_dev.sqlite:/usr/src/app/database.sqlite
openssl rand -base64 64
docker-compose up -d --profile app-prod
This will launch only the production service (using the image, ports, and variables from that profile).
Your app will be located at:
docker-compose up -d --profile app-dev
This will launch only the development service, with hot reload (if you use nodemon). Your app will be located at:
docker-compose up -d
This will start both services (app-prod and app-dev).
Tip: You can extend with multiple services (DB, cache, etc.) in the same file.
For example, stop development only:
docker-compose stop app-dev
Stop production only:
docker-compose stop app-prod
docker-compose logs -f app-prod
or
docker-compose logs -f app-dev
The container exposes /health for healthchecks (useful for Docker Swarm, Kubernetes, etc). Directly check health endpoint:
curl -k http://localhost:3000/health | jq
or
curl -k http://localhost:3001/health | jq
View real-time logs from the container:
docker logs -f test_ejs
The script /usr/local/bin/start.sh:
Validates required environment variables.
Installs dependencies according to the environment.
Searches for the first available entrypoint in this order:
index.js, app.js, main.js, server.js, src/index.js, dist/index.js, index.tsRuns the application with node (or with nodemon in development).
JWT_SECRET or JWT_EXPIRES_IN are missing, the container will abort with an error./app have proper permissions./health does not respond, verify your application implements the /health route.start.sh..env file:JWT_SECRET=SuperSecretKey
JWT_EXPIRES_IN=1d
PORT=3000
NODE_ENV=production
š¦ Software Repository and Citation Notice
This software is published under the Creative Commons Attribution 4.0 International License (CC0 1.0 Universal). You are free to use, modify, and distribute this code for any purpose, provided that proper credit is given to the original author.
Please cite this software using the following reference:
Author: H. L. Varona
Title: EJS: Universal Espress.js Docker Container
Github repo: https://github.com/humbertolvarona/ejsā
HL Varona
š§ [email protected]ā
š§ Project: VaronaTech
Content type
Image
Digest
sha256:7812f1703ā¦
Size
58 MB
Last updated
over 1 year ago
docker pull humbertovarona/ejs