如何通过Docker运行带环境与全局JSON的Newman?已遇命令报错
The error you're seeing happens because Docker containers can't directly access files on your host machine by default. When you used paths like ~/environments/staging.json in your original command, the Newman instance inside the container had no idea where that directory was—those paths only exist on your local system, not inside the Ubuntu 14.04 container.
Here's how to fix this properly using volume mounts (-v flag) to share your local files with the container, and then reference the container-side paths in your Newman command:
Step 1: Understand Volume Mounts
The -v flag lets you map a directory/file from your host machine to a path inside the container. Think of it as creating a shared folder between your local system and the container.
Step 2: Updated Docker Run Command
Let's assume your files are located at these local paths:
tests.jsonin your current working directorystaging.jsonin~/environmentsglobals.jsonin~/data
Use this command to mount each local directory to a container directory, then use the container paths in Newman:
sudo docker run -it --name newman \ -v $(pwd):/tests \ -v ~/environments:/envs \ -v ~/data:/globals \ postman/newman_ubuntu1404 \ newman run /tests/tests.json \ --environment /envs/staging.json \ -n 1 \ --globals /globals/globals.json
Breakdown of the command:
-v $(pwd):/tests: Maps your current local directory (wheretests.jsonlives) to/testsinside the container-v ~/environments:/envs: Maps your local~/environmentsdirectory to/envsinside the container-v ~/data:/globals: Maps your local~/datadirectory to/globalsinside the container- The Newman command now uses the container-side paths (
/tests/tests.json,/envs/staging.json, etc.) which Newman can access directly.
Step 3: Simplify with a Single Shared Directory
If you want to clean things up, move all your Newman-related files (tests, environments, globals) into one local directory (e.g., ~/postman-files). Then you only need one volume mount:
sudo docker run -it --name newman \ -v ~/postman-files:/postman \ postman/newman_ubuntu1404 \ newman run /postman/tests.json \ --environment /postman/staging.json \ -n 1 \ --globals /postman/globals.json
Bonus: Auto-Clean Up the Container
Add the --rm flag to automatically delete the container after it finishes running (avoids cluttering your system with unused containers):
sudo docker run -it --rm \ -v $(pwd):/tests \ -v ~/environments:/envs \ -v ~/data:/globals \ postman/newman_ubuntu1404 \ newman run /tests/tests.json \ --environment /envs/staging.json \ -n 1 \ --globals /globals/globals.json
This should resolve the "Invalid command or parameter" error, as Newman will now be able to find all the required JSON files inside the container.
内容的提问来源于stack exchange,提问作者QA Prof

