Issue running docker with command parameter using NPM scripts - docker

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

Related

Docker stop all containers command in an NPM script?

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

Removing docker container list throws error in commandpromt

I am using Docker on windows and trying to remove all containers with names starting with 'test' using below command
docker rm -f $(docker ps -a -q -f name=test)
It throws exception
unknown shorthand flag: 'a' in -a
See 'docker rm --help'.
I also tried the post on stack overflow.
docker ps -a -q -f name=test | xargs docker rm
Here I am getting an exception
'xargs' is not recognized as an internal or external command,
operable program or batch file.
To remove docker image first you need to stop the container that is attached to that image. After doing that you can simply run
To Stop all container
docker container stop $(docker container ls -aq)
docker rmi $(docker images -a -q)
This will remove all docker images from your system. If you are not in root you need to use
sudo docker rmi $(docker images -a -q)

Jenkins pipeline script to remove all docker containers - Windows :(

below is the line of code I am using in my jenkins pipeline script to remove all containers.....so I can then replace container with new version
bat 'docker rm $(docker ps -a -q) -f'
But I am getting the error
unknown shorthand flag: 'a' in -a
The command docker rm $(docker ps -a -q) -f works OK in Powershell......but when called from Jenkins using bat it fails
Running with 'bat' means it is a Windows command, so Windows don't know what is:
$(docker ps -a -q)
at all, since this is a linux (bash/sh) syntax!
You should replace it with something like this:
bat '''
FOR /F "tokens=* USEBACKQ" %%F IN (`docker ps -a -q`) DO (
SET var=%%F
)
docker rm -f %var%
'''
Or any Windows trick that will put the output of the first docker command into a variable and use it for deleting the container.
Please try docker rm -f $(docker ps -aq)
or
sh """
docker rm -f $(docker ps -aq)
"""
Update :
stage('Remove Containers') {
sh 'docker rm -f $(docker ps -aq)'
}

How to correct docker in makefile which requires at least 1 argument for remove all containers command

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

How do I delete all running Docker containers?

I remember using
docker rm -f `docker ps -aq`
to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:
unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.
What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.
Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),
Delete all Exited Containers
docker rm $(docker ps -q -f status=exited)
Delete all Stopped Containers
docker rm $(docker ps -a -q)
Delete All Running and Stopped Containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove all containers, without any criteria
docker container rm $(docker container ps -aq)
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,
docker system prune
All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,
docker container prune
docker image prune
docker network prune
docker volume prune
I had this issue when running in cmd. Switched to PowerShell and it worked!
Use:
docker rm -f $(docker ps -aq)
If anybody needs the Windows Shell Command (stop and remove container), here it is:
for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)
If you put it in a batch file, just add % to %c:
for /F %%c in ('docker ps -a -q') do (docker stop %%c)
I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.
But using Cygwin or Git Bash solved the problem for me.
Try using this command.
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
$ docker rm $(docker ps --filter status=created -q)
Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.
Run docker commands in Windows PowerShell will execute and run most of the commands
Hope you also remember to stop running containers first before running the delete command
docker stop $(docker ps -aq)
Use this:
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
If you want to include previously stopped containers:
docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
we can delete all running containers in docker ENV by the following the command -
docker container rm -f $(docker container ls -aq)
It should to the magic
if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down
should work
Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.
docker stop $(docker ps -a -q) && docker container prune -a
If the container is running, you cannot delete the image. First stop all the containers using the following command.
docker stop $(docker ps -aq)
you are saying running stop against the output of docker ps -aq.
'a' - get me all the containers
'q' - return only the container id.
Then run the following command to remove all the containers.
docker rm $(docker ps -aq)

Resources