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 stringYou can try this:
docker inspect --format="{{.Id}}" container_name
This approach is OS independent.
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
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
If you want to get complete ContainerId based on Container name then use following command
docker ps --no-trunc -aqf name=containername
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With