I named my containers in Docker, but now I forgot the names...
How can I list all of the used names?
docker -ps just gives me the running containers and docker images gives me all the images but no names.
I just want a list where I can see how I named the different containers when I created them.
As mentioned by #nwinkler, you use docker ps -a to list all of your containers even stopped ones.
Now, you can also use Format in combination to docker ps -a as a convenient way to print only part of the information that is relevant to you.
For example you can list your container IDs with their associated names with:
$ docker ps -a --format "{{.ID}}: {{.Name}}"
caee09882462: peaceful_saha
You can also use the regular table format with the column titles:
$ docker ps -a --format "table {{.ID}}\t{{.Names}}"
CONTAINER ID NAMES
caee09882462 peaceful_saha
If you only want a list of all the used names:
$ docker ps -a --format "{{.Names}}"
peaceful_saha
You can run docker ps -a to show all running and stopped containers.
The container ID will be in the first column of the output, and the name will be in the last column.
Example:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b74154d7133 wnameless/oracle-xe-11g "/bin/sh -c '/usr/sbi" 9 months ago Exited (0) 13 days ago 8080/tcp, 0.0.0.0:49160->22/tcp, 0.0.0.0:49161->1521/tcp oracle_xe
Related
I am running a docker image using the following command:
sudo docker run -it -v "${pwd}:/qc/output" qc
It works perfectly then I used the following command to get the list of existing docker containers
sudo docker ps --filter ancestor=ubuntu
but it returns only headers which is the following line:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
do you know why it is empty and how I can get the information for the existing docker containers?
you will get all running containers details by command
docker ps
use docker ps -a to see all active and non-active container details
I use docker-compose to create a bunch of containers and link them together. For some of the container definitions, I might have restart: always as the restart policy.
Now I have a postgres container that respawns back to life if stopped.
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8bb2b781630 postgres:latest "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp dcat_postgres.1.z3pyl24kiq2n4clt0ua77nfx5
docker stop a8bb2b781630
a8bb2b781630
$ docker rm -f a8bb2b781630
a8bb2b781630
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93fa7b72c2ca postgres:latest "docker-entrypoint.s…" 12 seconds ago Up 4 seconds 5432/tcp dcat_postgres.1.oucuo5zg3y9ws3p7jvlfztflb
Using docker-compose down in the dir that started the service doesn't work either.
$ docker-compose down
Removing dcat_postgres_1 ... done
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ee7fb0e98cd postgres:latest "docker-entrypoint.s…" 13 seconds ago Up 5 seconds 5432/tcp dcat_postgres.1.jhv1q6vns1avakqjxdusmbb78
How can I kill a container and keep it from coming back to life?
EDIT: The container respawns even after restarting the Docker service.
Docker - 18.06.1-ce-mac73 (26764)
macOS High-Sierra, (10.13.6)
I figured it out. Turns out it was related to docker swarm. I had experimented with it at some point without fully understanding what it is and what it does and apparently it just stayed there.
All I had to do was:
docker swarm leave --force
and it worked like a head-shot to an actual zombie.
Can you try an option like moby/moby issue 10032:
docker stop $(docker ps -a -q) &
docker update --restart=no $(docker ps -a -q) &
systemctl restart docker
(this assume here you have only one running container: the one you cannot prevent to start)
A docker rm -f should be enough though, unless you are using docker with a provision tool like Puppet.
As it turned out, another process (other than docker itself) was responsible for the container to restart (here docker swarm)
Update 2020/2021: For multiple containers, possibly without having to restart the docker daemon
docker ps -a --format="{{.ID}}" | \
xargs docker update --restart=no | \
xargs docker stop
Check if you need, as in the issue, remove the images as well ( | xargs docker rmi $(docker images -qa) --force)
This is a bit strange.
I loaded two docker images in my system, say image1 and image2.
I then created the following containers:
containerA with image1
containerB with image2
When I run the command docker ps -a --filter "name=containerA" --format "{{.Status}}", I get two values (Example: Up 11 minutes and Up 16 minutes)
If I run the same command but for containerB, I get only one value. So:
docker ps -a --filter "name=containerB" --format "{{.Status}}" gives me Up 16 minutes
So, I'm getting the status of both containers even tough the containerA has just one image.
Is this a bug? Am I filtering wrong?
Thank you
If one container name is a substring of another, you'll see this behavior.
From the documentation:
The name filter matches on all or part of a container’s name.
So if you do a:
docker ps -a --filter "name=container5" --format "{{.Status}}"
docker ps -a --filter "name=container51" --format "{{.Status}}"
the first command would include container5 and container51.
Try this (it seems filter allows regular expressions):
docker ps -a --filter "name=^container5$" --format "{{.Status}}"
That will give only a result, not two even if container51 does exist.
The docker command has a ps sub-command that emits very long lines:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e8ec8a16da4 waisbrot/wait:latest "/wait" 4 minutes ago Exited (0) 4 minutes ago wait-for-janus-test
9dbf0739561f whoop/downsampler:master "./run.bash" 4 minutes ago Up 4 minutes 0.0.0.0:32855->4369/tcp, 0.0.0.0:32854->9100/tcp, 0.0.0.0:32853->9101/tcp, 0.0.0.0:32852->9102/tcp, 0.0.0.0:32851->9103/tcp, 0.0.0.0:32850->9104/tcp, 0.0.0.0:32849->9105/tcp, 0.0.0.0:32848->9106/tcp, 0.0.0.0:32847->9107/tcp, 0.0.0.0:32846->9108/tcp, 0.0.0.0:32845->9109/tcp, 0.0.0.0:32844->9110/tcp metrics-downsampler-test
6cf56623bb48 whoop/janus:master "./start.bash" 4 minutes ago Up 4 minutes 0.0.0.0:32843->80/tcp janus-test
882b50303d54 whoop/recalculator:master "./run.bash" 4 minutes ago Exited (1) 4 minutes ago internum-test
It can be instructed to output only specific columns:
docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}"
I'd like to be able to say docker ps and get the --format "table..." argument added on for me. Is there a nice way to do this?
I know I could say
alias dp='docker ps --format ...'
but I'd prefer to keep the sub-command.
I'm using zsh as my shell.
You can wrap docker in a function that checks for the specific subcommand and passes everything else through. (The below will actually work with not just zsh, but any POSIX-compliant shell -- a category to which zsh doesn't quite belong).
docker() {
case $1 in
ps)
shift
command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$#"
;;
*)
command docker "$#";;
esac
}
If you wanted a more generic wrapper function (that doesn't need to know about your specific desired ps logic), that could be done as follows (note that this version is not compatible with baseline POSIX sh due to its use of local; however, this is an extension implemented even by ash and its derivatives):
docker() {
local cmd=$1; shift
if command -v "docker_$cmd" >/dev/null 2>/dev/null; then
"docker_$cmd" "$#"
else
command docker "$cmd" "$#"
fi
}
...after which any subcommand can have its own functions defined, without the wrapper needing to be modified to know about them (you could also create a script in the PATH named docker_ps, or provide the command in any other manner you choose):
docker_ps() {
command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$#"
}
Using Docker Config
Since this is fundamentally a docker questions, not a bash question, you don't even need an alias. Docker CLI allows you to customize these commands in your own config file! From this great tip from Container 42:
Create or find your docker config file (if you've ever used docker login it should already be created.
~/.docker/config.json
Then add the default formatting for docker to use every time it runs the ps command as a top level property in the config:
{
"psFormat": "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}",
}
Then just run docker ps like normal:
PS Format
Docker uses go templates and has a list of the valid placeholders:
Command
Description
.ID
Container ID
.Image
Image ID
.Command
Quoted command
.CreatedAt
Time when the container was created.
.RunningFor
Elapsed time since the container was started.
.Ports
Exposed ports.
.Status
Container status.
.Size
Container disk size.
.Names
Container names.
.Labels
All labels assigned to the container.
.Label
Value of a specific label for this container.
.Mounts
Names of the volumes mounted in this container.
.Networks
Names of the networks attached to this container.
Alternative Solutions / Threads
Github Issues
Default "docker ps" output is too wide
docker ps output is so long it's unreadable
Third Party Commands
ctop - Top-like interface for container metrics
docker-pretty-ps - beautiful, colored, long output log
dockerps - A better docker ps
You can alias subcommands. With aliasing, you still get the nice zsh completions as if you were typing the full command. That's why I prefer them over functions.
The equivalent of your alias is:
alias dp='docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}'\t{{.Status}}"
But the full commands seem to now be recommended, and ls has replaced ps, which makes your alias now:
alias dp='docker container ls --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}'\t{{.Status}}"
It's nice to have docker aliases for everything. For this, I've been working on a set of comprehensive aliases, which would have your alias as something like:
alias ddcls='docker container ls --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}"
In my case, I needed to disable only docker login command (some folks used that command on our CI-runner breaking a generic config file for docker).
So, I added to my .bashrc:
_docker() {
if [ "$1" = "login" ]; then
echo "login is disabled, to login please update a config file manually!"
return
fi
/usr/bin/docker "$#"
}
alias docker="_docker"
I have an app in a docker setup. I would like to run a script on the host that would run some commands in an existing (running container).
If I know the container id, say ... it's 50250e572090 ... then I can run the script like this
For example ...
#!/usr/bin/env bash
docker exec 50250e572090 example_command_1_here
docker exec 50250e572090 example_command_2_here
docker exec 50250e572090 example_command_3_here
docker exec 50250e572090 example_command_4_here
It's working great! ... but the thing here is that I only know the image name ... not the container id. To find the container id ... I use docker ps ... where I get something like this ...
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50250e572090 aws_beanstalk/staging-app:latest "/sbin/my_init" 29 hours ago Up 29 hours 80/tcp, 443/tcp drunk_bardeen
It's output isn't something that I can use (pipe through). Which command can I run to get the container id as the output which can then be piped into the script? Or now that it's clear what I'm trying to achieve ... is there a better way?
Ps: My context is that I'm on elastic beanstalk ... but I don't see how this changes anything. Might as well be on the local host ... the problem is the same.
I was able to achieve this using the -q flag. Like so ...
#!/usr/bin/env bash
docker exec `docker ps -q` example_command_1_here
docker exec `docker ps -q` example_command_2_here
docker exec `docker ps -q` example_command_3_here
docker exec `docker ps -q` example_command_4_here
What you're requesting is not that easy. Multiple containers can use the same image.
You can use docker ps with a filter to only see containers derived from a specific image:
$ docker ps -q --filter "ancestor=aws_beanstalk/staging-app:latest"
Please note that this will return all running containers using the aws_beanstalk/staging-app:latest image which might be more than one.
You can run docker inspect command and get the Id of the container;
viswesn#viswesn-PC1:~$ docker inspect My_First_Docker | grep Id | awk '{print $2}'
"e3824f0121f24dded9792f133344a2d68b46ea13065481c30caf35d0ac6be40e",
I know this question is old, but I wanted a better answer than was given here, and I figured it out:
docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=image/repo/and:tag"
You can leave off :tag if you want, or you can filter on something else entirely. The output will be the full, un-truncated ID of each matching container. No column headers or anything else extraneous.
If you only need the short version (first twelve hex digits) of the ID, leave off --no-trunc.