What is the command to get the Docker container id from the container name?
-
You should read [ask] and provide a [mcve]. How does `docker --list` or similar look like? What is the command to create a new container? etc – fedorqui Dec 28 '15 at 16:27
-
1There is no such thing as a "container" that I know of in bash. Perhaps you mean *docker* containers? If so, people watching the "bash" tag might get confused. however you get the container id from the name, I'm sure it is an actual command and has nothing to do with the bash programming language. – djhaskin987 Dec 28 '15 at 17:41
13 Answers
In Linux:
sudo docker ps -aqf "name=containername"
Or in OS X, Windows:
docker ps -aqf "name=containername"
where containername
is your container name.
To avoid getting false positives, as @llia Sidorenko notes, you can use regex anchors like so:
docker ps -aqf "name=^containername$"
explanation:
-q
for quiet. output only the ID-a
for all. works even if your container is not running-f
for filter.^
container name must start with this string$
container name must end with this string
-
11a note to anyone who stumbles upon this: sudo is no longer required on linux if you [add yourself to the docker group](https://docs.docker.com/engine/installation/linux/ubuntulinux/#/create-a-docker-group) (highly recommended) – code_monk Nov 23 '16 at 04:16
-
13Please be careful with this answer, as name=containername is actually a wildcard and it will match anything with name (.*)containername(.*) – Ilia Sidorenko Dec 23 '16 at 01:07
-
that seems to generate a short id. if I look in /var/lib/docker/containers I see long ids. how to get the long id? – ekkis Apr 20 '17 at 00:57
-
6@ekkis use the `--no-trunc` flag. so `docker ps --no-trunc -aqf "name=containername"` – code_monk Apr 21 '17 at 22:15
-
6As of 2019-01-07, this did not work for me. I'm using docker v17.06.0. The command ```docker inspect --format="{{.Id}}" imageName``` worked for me. – PatS Jan 07 '19 at 23:30
-
-
1@code_monk, Thanks for the confirmation. I've added a comment to Rosberg Linhares answer which is where I got the idea from, and suggested his answer be changed to the correct answer. – PatS Jan 15 '19 at 00:52
-
The regex anchors version doesn't work for me (no match). I resorted to using awk: ` docker ps | awk '$2 ~ /^your-image-name$/ { print $1 }'` – Apteryx Oct 28 '19 at 19:36
-
1This helped me tremendously thank you. I want to add that if you want to find the running IMAGE name instead, try this `docker ps -qf "ancestor=containername"` where containername is the full name like `searx\searx` – iamdoubz Oct 16 '20 at 16:01
You can try this:
docker inspect --format="{{.Id}}" container_name
This approach is OS independent.

- 3,537
- 1
- 32
- 35
-
6
-
3@vijay, for me this gives the container ID: [https://i.ibb.co/BPwyxDj/Docker-Container-Id.png](https://i.ibb.co/BPwyxDj/Docker-Container-Id.png) – Rosberg Linhares Feb 09 '19 at 15:41
-
This also gives me the container ID. The full container ID, not just the truncated first 12 characters. – cowlinator May 09 '19 at 00:48
-
1
-
6@vijay @RosbergLinhares if you provide the name of the container (given in the `NAMES` column with `docker ps`), it gives the container ID. If the image name (given in the `IMAGE` column with `docker ps`) is provided instead, it outputs the image ID instead. – Wolfson Apr 14 '20 at 14:46
-
If you want to avoid the ambiguity between image and container names, you can use `docker container inspect --format="{{.Id}}" container_name` instead. This will only return container IDs and say `Error: No such container: name` otherwise. – Dario Seidl Aug 08 '22 at 15:46
You could use the following command to print the container id:
docker container ls | grep 'container-name' | awk '{print $1}'
As a bonus point, if you want to login to the container with a container name:
docker exec -it $(docker container ls | grep 'container-name' | awk '{print $1}') /bin/bash

- 873
- 9
- 16
Get container Ids of running containers ::
$docker ps -qf "name=IMAGE_NAME" -f: Filter output based on conditions provided -q: Only display numeric container IDs
Get container Ids of all containers ::
$docker ps -aqf "name=IMAGE_NAME" -a: all containers

- 43,623
- 55
- 191
- 321

- 7,515
- 3
- 24
- 21
-
2`docker ps -fq ` did not work however `docker ps -qf` did (order of flags) – thom_nic Feb 01 '17 at 15:14
The following command:
docker ps --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | Image: {{.Image}} | Ports: {{.Ports}}'
Gives this output:
CONTAINER ID : d8453812a556 | Name: peer0.ORG2.ac.ae | Image: hyperledger/fabric-peer:1.4 | Ports: 0.0.0.0:27051->7051/tcp, 0.0.0.0:27053->7053/tcp
CONTAINER ID : d11bdaf8e7a0 | Name: peer0.ORG1.ac.ae | Image: hyperledger/fabric-peer:1.4 | Ports: 0.0.0.0:17051->7051/tcp, 0.0.0.0:17053->7053/tcp
CONTAINER ID : b521f48a3cf4 | Name: couchdb1 | Image: hyperledger/fabric-couchdb:0.4.15 | Ports: 4369/tcp, 9100/tcp, 0.0.0.0:5985->5984/tcp
CONTAINER ID : 14436927aff7 | Name: ca.ORG1.ac.ae | Image: hyperledger/fabric-ca:1.4 | Ports: 0.0.0.0:7054->7054/tcp
CONTAINER ID : 9958e9f860cb | Name: couchdb | Image: hyperledger/fabric-couchdb:0.4.15 | Ports: 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp
CONTAINER ID : 107466b8b1cd | Name: ca.ORG2.ac.ae | Image: hyperledger/fabric-ca:1.4 | Ports: 0.0.0.0:7055->7054/tcp
CONTAINER ID : 882aa0101af2 | Name: orderer1.o1.ac.ae | Image: hyperledger/fabric-orderer:1.4 | Ports: 0.0.0.0:7050->7050/tcp

- 9,564
- 146
- 81
- 122

- 599
- 7
- 11
If you want to get complete ContainerId based on Container name then use following command
docker ps --no-trunc -aqf name=containername

- 5,030
- 7
- 49
- 67
In my case I was running Tensorflow Docker container in Ubuntu 20.04 :Run your docker container in One terminal , I ran it with
docker run -it od
And then started another terminal and ran below docker ps
with sudo:
sudo docker ps
I successfully got container id:
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
e4ca1ad20b84 od "/bin/bash" 18 minutes ago
Up 18 minutes unruffled_stonebraker

- 466
- 2
- 9
- 20
Thanks for the answer of https://stackoverflow.com/a/65513726/889126, it gave me an idea to make a complete bash script as it is
export api_image_id=$(docker inspect --format="{{.Id}}" <image-name> | sed '/^[[:space:]]*$/d')
sudo docker exec -i -t ${api_image_id} /bin/bash
I need a specific container and make a script to extract some info from it in a quick sight.
Hope this would help others.

- 2,253
- 25
- 42
To have container id and image Id -
$ docker container ls -a | awk 'NR>1 {print $1, $2}'

- 3,872
- 6
- 22
- 37

- 21
- 2
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 17:32
-
This just gives ids and names list, doesn't answer the question. imagine you have 100 containers running and this will not be helpful – cerofrais Oct 31 '21 at 18:02
I tried sudo docker container stats
, and it will give out Container ID along with details of memory usage and Name, etc. If you want to stop viewing the process, do Ctrl+C
. I hope you find it useful.

- 75
- 1
- 2
- 10
I also need the container name or Id which a script requires to attach to the container. took some tweaking but this works perfectly well for me...
export svr=$(docker ps --format "table {{.ID}}"| sed 's/CONTAINER ID//g' | sed '/^[[:space:]]*$/d')
docker exec -it $svr bash
The sed command is needed to get rid of the fact that the words CONTAINER ID gets printed too ... but I just need the actual id stored in a var.

- 30,962
- 25
- 85
- 135

- 64
- 5
Docker image inspect ImageName\ImageId --format={{'.ConatinerConfig.Hostname'}}
-
1Please add an explanation to your answer. Answers that are code only (or in this case command only) are not helpful to new users who might not understand what the command or code actually does. Look up cargo cult programming for why this is bad. – Max Vollmer Nov 21 '19 at 11:39
The simplest way I can think of is to parse the output of docker ps
Let's run the latest ubuntu image interactively and connect to it
docker run -it ubuntu /bin/bash
If you run docker ps
in another terminal you can see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8fddbcbb101c ubuntu:latest "/bin/bash" 10 minutes ago Up 10 minutes gloomy_pasteur
Unfortunately, parsing this format isn't easy since they uses spaces to manually align stuff
$ sudo docker ps | sed -e 's/ /@/g'
CONTAINER@ID@@@@@@@@IMAGE@@@@@@@@@@@@@@@COMMAND@@@@@@@@@@@@@CREATED@@@@@@@@@@@@@STATUS@@@@@@@@@@@@@@PORTS@@@@@@@@@@@@@@@NAMES
8fddbcbb101c@@@@@@@@ubuntu:latest@@@@@@@"/bin/bash"@@@@@@@@@13@minutes@ago@@@@@@Up@13@minutes@@@@@@@@@@@@@@@@@@@@@@@@@@@gloomy_pasteur@@@@@@
Here is a script that converts the output to JSON.
https://gist.github.com/mminer/a08566f13ef687c17b39
Actually, the output is a bit more convenient to work with than that. Every field is 20 characters wide.
[['CONTAINER ID',0],['IMAGE',20],['COMMAND',40],['CREATED',60],['STATUS',80],['PORTS',100],['NAMES',120]]

- 6,710
- 3
- 25
- 65