sh: grep: command not found - docker

I'm executing a command docker ps -a | grep <imagename> from another container.
It shows
sh: grep: command not found
sh: docker: command not found
Can anyone support me on it?

When you create image1, depending on its base image:
it might not have docker installed in it (see "How to install docker in docker container?")
it might not have grep installed (depending on your base image, or your $PATH defined in that image )
So start by checking those two factors.

If you are searching for an image name, you can try:
docker ps --filter ancestor=image_name
If you are searching for a container name, you can try:
docker ps --filter "name=container_name"
But the fellas are right, if you don't have docker installed on that container, you won't be able to execute the docker ps command.

Related

What is difference between 'docker ps -all' and 'docker ps -a'?

The command 'docker ps -all' gives me all the created containers but 'docker ps -a' also supposed to give me same results but somehow it returns me only 1 or sometimes 2 latest entries of containers.
Can someone please explain me? I am new to docker. Any help is appreciated.
There is a difference between docker ps -a (which is identical to docker ps --all) and docker ps -all.
We can see the difference by looking at the documentation of docker ps:
Options
Name, shorthand Default Description
--all , -a Show all containers (default shows just running)
--filter , -f Filter output based on conditions provided
--format Pretty-print containers using a Go template
--last , -n -1 Show n last created containers (includes all states)
--latest , -l Show the latest created container (includes all states)
--no-trunc Don't truncate output
--quiet , -q Only display container IDs
--size , -s Display total file sizes
As we can see: docker ps -a and docker ps --all are identical.
Meanwhile, docker ps -all uses the shorthand form and is identical to docker ps -al. This command will only show the most recently created container.
As you can see in the documentation -a is just short for --all, they serve the same purpose.
docker ps -a and docker ps --all are same commands but if you use docker ps -all command it will return just last created container. docker ps -all is same as docker ps -al.

'Docker cp' with parameter expansion (PowerShell)

I want to copy a directory into a docker container, which is only defined by a certain query. I tried this:
docker cp ../from-here/. $(docker ps -aqf "name=my-ending$"):/to-here
Unforunately this throws this error:
"docker cp" requires exactly 2 arguments.
Running the same command with pasting the real Container ID works.
It seems, that PowerShell in combination with Docker doesn't allow parameter expansion.
Is there an easy work around for this problem in a single line?
in power shell you need to use | (pipe) to continue commands
docker cp ../from-here/. | % {docker ps -aqf "name=my-ending$"}:/to-here
This works:
docker #("cp", "../from-here/.", "$(docker ps -aqf "name=my-ending$"):/to-here")

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

What is the difference between docker ps -all and docker ps --all

The docker commands reference for ps mentions that
docker ps -a
docker ps --all
commands show the list of all containers.
While working on dockers, I mistakenly was using the command
docker ps -all
and it gave me the list of last container I ran, even though its status was Exited only.
Wasted much time due to this confusion. Have been looking for its official reference and couldn't find any. I was wondering if its a bug or am I missing something.
By convention on linux, -xyz is shorthand for -x -y -z, so it looks like docker's taking -all to be -a -l -l, or just -a -l.
From the reference quoted below, you can see that -a is shorthand for -all, and -l is shorthand for --latest, so you get shown the latest container run.
--all , -a Show all containers (default shows just running)
...
--latest , -l Show the latest created container (includes all states)
From the descriptions in the reference, it looks like -l effectively overrides -a, so your output for docker ps -all should be the same as for docker ps -l.

How do I find a conflicting container that docker reports is not running?

When I start a docker image with a specified name docker reports the name has already been taken by an already running docker container. I cannot find that container by looking at docker ps -a, nor can I remove it referencing it by the id or name. How can I find the conflicting running container? How do I debug this situation futher?
myuser#myhostname$ docker --version
Docker version 1.11.2, build b9f10c9
myuser#myhostname$ docker run --name=myimage-build ubuntu
docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
myuser#myhostname$ docker ps -a | grep 9467
myuser#myhostname$
myuser#myhostname$ docker rm -f 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e
Error response from daemon: No such container: 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e
myuser#myhostname$ docker rm -f myimage-build
Error response from daemon: No such container: myimage-build
myuser#myhostname$
myuser#myhostname$ docker run --name=myimage-build ubuntu
docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
Rao
I work in the same team as Wojtek.
I run the docker inspect you suggested:
docker inspect --format='{{.Name}}' $(docker ps -aq --no-trunc)
/an-unrelated-app
Doesn't seem to be there.
I use several docker-compose yamls with the same name of certains services, and deal with this problem every time.
for me, the fast way to avoid this problem is clean all containers.
docker rm $(docker container ls --all -q)

Resources