When I run docker stop $(docker ps -a -q) to stop all Docker containers. It works fine. It stops all running containers.
But if I add to an NPM script, like:
package.json
"scripts": {
"docker:stop-all": "docker:stop-all": "docker stop $(docker ps -a -q)"
}
And I run: npm run docker:stop-all
I'm getting this error:
> docker stop $(docker ps -a -q)
unknown shorthand flag: 'a' in -a
See 'docker stop --help'.
How can I execute that stop-all command by using an NPM script?
Just found out what was the problem.
When you call npm run, npm itself will run a shell to run those instructions.
And that shell should be able to understand the instructions.
If npm calls PowerShell (on Windows), for example, it will not understand $(docker ps -a -q) since it's a Bash specific instruction.
So you need to config npm so it will execute bash or git-bash instead of Windows cmd or PowerShell.
I found out how to set that config on this question.
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
You can list your npm configs to check that it worked:
npm config ls
Related
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.
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.
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
The docker command "docker container rm $(docker ps -aq) -f" works fine from the command line. However, when I try to run it from a makefile using the following target ("remove_all_containers")...
remove_all_containers:
docker container rm $(docker ps -aq) -f
I get the error message:
host_name$ make remove_all_containers
docker container rm -f
"docker container rm" requires at least 1 argument.
See 'docker container rm --help'.
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
make: *** [remove_all_containers] Error 1
Clearly, when executed from within the makefile, the "docker ps" command is not being properly being properly executed in a way where its results can be collected and passed into the "container rm" command.
My Question: How do I get the "docker ps" command to run correctly from within the makefile and pass its results correctly into the "docker rm" command, also within the makefile?
Thanks, in advance, for any assistance you can offer.
You need a second $ in your recipe:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
# ^
The single $ is expanded as a makefile variable when the makefile is parsed. It expands to blank. Make therefore passes docker container rm -f to your shell. The second $ sign causes make to expand $$ to $, and it will pass docker container rm $(docker ps -aq) -f to bash, which I'm guessing is what you want.
Notice, if you put the shell in there as #EricMd proposed, it will run a shell command, but that command will be run at Makefile read time, as opposed to the time that the recipe is executed. If the docker ps -aq command is dependent on any other artifacts of your build it would not work.
Sounds like you don't have any containers in docker to remove. I sometimes use a different syntax for this scenario:
remove_all_containers:
docker container ls -aq | xargs --no-run-if-empty docker container rm -f
The xargs syntax will not run docker container rm if there are no containers to delete.
According to the documentation, docker ps -a should list all containers.
You obtained this message "docker container rm" requires at least 1 argument certainly because you forgot to prepend the command at stake with Make's shell builtin:
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
Note also that the docker ps admits a filtering feature: the online doc describes the various flavors of the corresponding -f flag.
For example, below are three Bash alias examples that can be useful to (i) stop all containers, (ii) remove all stopped containers; and (iii) remove dangling images−that would be tagged as <none> when doing docker images ls:
alias docker-stop='docker stop $(docker ps -a -q)'
alias docker-clean='docker rm $(docker ps -a -q -f status=exited)'
alias docker-purge='docker rmi $(docker images -q -f dangling=true)'
I tested for 2 way follow bellow answer:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
I am biginner in block chain development. I tried to hyperledger composer in multiple host. I successfully running ./startfabric.sh.
Then I tried to run ./createpeerAdmincard.sh
But I got an error message that no valid response from any peer. I changed many time composer vertion 0.16 to 0.19.x. And I tried to delete .composer file in home directory. But my problem is not solved. Please give a solution as possible.
Os is : Ubuntu 16.04
Hyperledger composer is:1.0.4
Thanks
Before anything, I would run these commands in the directory which has ./startFabric.sh to uninstall previous versions of your CLI / remove all cards or credentials you may have generated up to this point.
$ npm uninstall -g composer-rest-server
$ rm -rf ~/.composer
$ rm *.card
$ rm -rf credentials/
(optional - kill docker containers)
$ docker kill $(docker ps -q)
$ docker rm $(docker ps -aq)
$ docker rmi $(docker images -q)
now run
./downloadFabric.sh
and then after
./startFabric.sh
Now, install composer and composer-rest-server (version 19.5) ** this is very important.
npm install -g composer-cli#0.19.5
and then
npm install -g composer-rest-server#0.19.5
and then, after all that, try and use ./createPeerAdminCard.sh
I find this tutorial pretty useful to get up and running with Composer -> https://github.com/IBM/BlockchainNetwork-CompositeJourney#1-starting-hyperledger-fabric
I'm reading a book on docker. It is a couple of years old.
I'll cite:
If you want to get rid of all your stopped containers, you can use
the output of docker ps -aq -f status=exited , which gets the
IDs of all stopped containers. For example:
$ docker rm -v $(docker ps -aq -f status=exited)
When I run this, I get:
michael#michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Could you help me understand what should I do to gain what is intended.
In order to remove all our stopped containers, you can first run
$ docker ps -a
This gives you the list of running and stopped containers, from which you can select what are the containers that you wanted to get rid. But if you want to get rid of all stopped containers, then you need to use
$ docker container prune
This removes all stopped containers by giving you the following messages.
Warning! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
your container id list will be printed.
It could simply means that you have no container with a status 'exited'.
The commands becomes then:
sudo docker rm -v
The lack of any parameter would trigger the error message you see.
But today, this would be done with docker container prune anyway.
$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get
http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D:
dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.
The permission denied message comes from the embedded docker ps command. It is run by the shell outside of your parent sudo command, and the output is passed to sudo to run the docker rm as root. There are several fixes.
The easy option, run the docker ps with sudo:
$ sudo docker rm -v $(sudo docker ps -aq -f status=exited)
Option two is to run an entire shell as root:
$ sudo -s
# docker rm -v $(docker ps -aq -f status=exited)
# exit
Or you can give your user access to the docker socket so sudo is no longer needed:
$ sudo usermod -aG docker $USER
$ newgrp docker
The above is a one time change, and gives that user root access implicitly with docker. Then you can run:
$ docker rm -v $(docker ps -aq -f status=exited)
What seems to be happening is docker was started with different user. Hence, docker ps -aq -f status=exited could not be run due permission issue and as a result got blank result.
Running docker rm complains of missing argument due to blank result.
michael#michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
Actually its an rights issue.... the error message:
Got permission denied while trying to connect to the Docker daemon socket at unix://
Tells you that you cant connect to you docker daemon which is running under root. You should decide if you want to use docker with # sudo or as root user.
Manage Docker as a non-root user
Further as said the docker rm complains about no images found for deletion therefore it wouldn't possible to delete images.
The command docker rm $(docker ps -aq -f status=exited) is just fine with newest docker version 18.09.0 but you could use docker container prune as well that is the more interactive way.
I am trying to execute the next Makefile, but when I run the docker kill it fails because don't detect the "$(sudo docker ps -q)" or it does not execute this part.
I have the next Makefile:
.PHONY: kill all services farr-api farr-ingest farr-from-on-premise farr-real-time-processing
farr-api:
cd apis/1api && sudo docker-compose up -d
farr-ingest:
cd apis/2ingest && sudo docker-compose up -d
farr-from-on-premise:
cd apis/3onpremise && sudo docker-compose up -d
farr-real-time-processing:
cd apis/4realtimeprocessing && sudo docker-compose up -d
services:
cd services && sudo docker-compose up -d
all: services farr-from-on-premise farr-real-time-processing farr-ingest farr-api
kill:
sudo docker kill $(sudo docker ps -q)
When I run make kill it throws the next error:
sudo docker kill
"docker kill" requires at least 1 argument(s).
See 'docker kill --help'.
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
make: *** [kill] Error 1
It looks like "$" does not detect by Makefile.
But if I run manually sudo docker kill $(sudo docker ps -q) it works fine.
The target should look like this:
kill:
for c in $$(sudo docker ps -q); do sudo docker kill $$c; done
UPDATE:
turns out that docker kill works with multiple containers as arguments, so just escaping dollar is enough to kill all containers
kill:
sudo docker kill $$(sudo docker ps -q)