1

I've got a repo with some NPM convenience scripts to run some basic docker commands:

  "scripts": {
    "build": "docker build -t myreadyapi --build-arg LICENSE_SERVER=1.1.1.1 .",
    "prestart": "npm run build",
    "start": "docker run -p 8089:8088 myreadyapi",
    "debug": "docker exec -it $(docker ps -a -q --filter ancestor=myreadyapi) /bin/bash",
    "stop": "docker rm $(docker stop $(docker ps -a -q --filter ancestor=myreadyapi))"
  }

npm run build and npm run start work, but npm run debug and npm run stop cause an error:

Error: No such container: $(docker

Note: running this from Windows 10 PowerShell console.

The error happens for any docker script that has a command parameter (i.e. docker ... $(docker ...)).

Has anyone encountered this before and knows how to fix this?

Cheers.

Ryan.Bartsch
  • 3,698
  • 1
  • 26
  • 52

2 Answers2

0

It may happen that you have some stopped containers that match $(docker ps -a -q --filter ancestor=myreadyapi).
Or no container is found with matching filter.
One solution could be crate random container name and use that name in further commands or set ancestor system generated value.

Akash Sharma
  • 721
  • 3
  • 6
  • If I run the command directly in the console it works... it's just when I run it via NPM – Ryan.Bartsch Mar 28 '19 at 08:08
  • So, how does start work. After running the docker container, when it moves to `debug` does `start` exits. If that is the case try to change `start` as `docker run -d -p 8089:8088 myreadyapi` to run container as daemon. – Akash Sharma Mar 28 '19 at 10:50
  • I don't think it has anything to do with the docker commands - they all work perfectly fine when run directly from the command line. The issue is when trying to run docker commands (specifically ones that have command parameters) via an NPM script e.g. 'npm run debug'. – Ryan.Bartsch Mar 28 '19 at 21:02
  • They all work via the command line, but only build and start work via NPM. What's common in the stop and debug scripts that's causing them not to work? Command parameters is one thing... this is also highlighted in the error message - "Error: No such container: $(docker" – Ryan.Bartsch Mar 28 '19 at 21:04
0

I was able to get this working by adding "@powershell" in front of the command. That assumes Powershell is in your path

EG

"docker:stop":"@powershell docker rm $(docker stop $(docker ps -a -q --filter ancestor=myreadyapi))"

Referencing this answer

Jeff
  • 674
  • 1
  • 5
  • 17