0

What command do I run to get the container ID of an image?

I would think docker ps -a something...

CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS                    PORTS                              NAMES
17ef697da46d   local_django         "/entrypoint /start"     56 minutes ago   Up 56 minutes             0.0.0.0:8000->8000/tcp             django-1

I only want the container ID returned from an image search

njouro
  • 11
  • 4
  • Try this docker ps -aqf "name=local_django" https://stackoverflow.com/questions/34496882/get-docker-container-id-from-container-name – Marco Massetti Dec 01 '22 at 10:19
  • It doesn't return? ➜ ~ docker ps -aqf "name=local_django" ➜ ~ – njouro Dec 01 '22 at 10:20
  • For me is working, you can also try the docker inspect as it's written in the answer – Marco Massetti Dec 01 '22 at 10:22
  • (You can launch multiple containers from an image, or none, so this may not have a single answer. If you know the container name then you can directly use that with CLI tools without looking up the hex container ID.) – David Maze Dec 01 '22 at 11:28

2 Answers2

1

You can add the -q option to only get the ID back. And the -f option to filter on 'ancestor' which means the image name or any image in the chain of images it's based on.

ps -aqf ancestor=local_django
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
1

Based on image

docker ps -aqf ancestor=<image_name>

You can get it based on name as well

docker ps -aqf name=<container_name>

Also to get all container ids

docker ps -q
Shubham Waje
  • 781
  • 7
  • 12