Today's Fast and Simple Tip is about docker.
To quickly test a Node.js application in development, without a build or Dockerfile, I use this command:
docker run -d --rm --entrypoint /app/run.sh -v $(pwd):/app -p 8080:8080 node:20-alpine
Here, the run.sh
script in the current directory (/app
) can be something like this:
#!/bin/sh
cd /app
[ -f package.json ] && npm install || echo "No package.json found, skipping install"
npm run serve -- --host 0.0.0.0
How does it work and why use it?
-
-d
: Runs in the background, freeing up the terminal. -
--rm
: Removes the container when stopped, perfect for disposable tests. -
-v $(pwd):/app
: Syncs your local code with the container in real time. -
--entrypoint /app/run.sh
: Runs your custom script, ignoring the default entrypoint of the image. -
-p 8080:8080
: Exposes the port for browser access (e.g., http://localhost:8080).
The run.sh
script checks if a package.json
exists before installing dependencies, preventing errors, and starts the server (ideal for Vue.js, React with Vite, etc.). This eliminates the need to build a Docker image during development, speeding up testing.
Tip:
If you run multiple containers, avoid using a fixed --name
to prevent conflicts. Docker automatically generates random names.