npxd v1.4.1
npxd
npxfor Docker
npxd <command> is like npx <command> but runs command inside Docker container defined in docker-compose.yml.
The main benefit: seamless command invocation from either host machine or inside container:
- if invoked from host machine - ensures container is running via
docker compose upand runs command viadocker compose exec - if invoked from inside container - just runs command ¯\_(ツ)_/¯
Usage
- Ensure you have
docker-compose.ymlfile in your project - Install npxd (details):
npm i -D npxd - Prepend
package.jsonscripts withnpxd:"scripts": { - "test": "mocha test/*.js", + "test": "npxd mocha test/*.js", }, - Run as usual from your host machine:
npm test Check results of command executed in container:
$ npm test > npxd mocha test/*.js Recreating example_app_1 ... done ✓ should add numbers 1 passing (36ms)
See full example code in example directory.
Installation
There are to installation options:
Install from NPM (recommended)
Installing from npm will add npxd executable to node_modules/.bin of your project.
You will be able to use npxd command in package.json scripts.
npm i -D npxdInstall from GitHub
Alternatively you can download script directly from GitHub and store it in your project.
It allows you to adopt script for your needs (as it's a few lines of code).
But in that case you should invoke it as ./npxd.sh not npxd in package.json scripts.
wget -O npxd.sh https://raw.githubusercontent.com/vitalets/npxd/main/npxd.sh
chmod +x npxd.shPassing env variables
This will NOT work:
"scripts": {
"lint": "FOO=42 npxd eslint ./src"
},But this will work:
"scripts": {
"lint": "npxd env FOO=42 eslint ./src"
},Configuring service name
- if there is single service in
docker-compose.yml, commands will automatically run inside it - if there are several services in
docker-compose.yml, you should create.npxdrcfile containing target service name
Example of .npxdrc that runs commands in my_app service:
my_appNote: when you install
npxddirectly from GitHub you may just edit./npxd.shto set particular service name.
Usage from cli
When invoked not from package.json scripts section you need to use npx in front of npxd
(as it is usual executable inside node_modules/.bin):
$ npx npxd eslint ./srcOr if you installed from GitHub, just run script:
$ ./npxd.sh eslint ./srcLicense
MIT @ Vitaliy Potapov