How to get a docker container id.

David Carr

2 min read - 9th Dec, 2024

Docker containers

List All Running Containers

To get the Docker container ID, you can use the docker ps command, which lists all running containers along with their container IDs. Below are different methods to retrieve the container ID:

docker ps

For example:

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                   NAMES
d9b100f2f636   nginx:latest  "/docker-entrypoint.…"   5 minutes ago   Up 5 minutes   0.0.0.0:80->80/tcp      nginx-container

Get the Container ID for a Specific Container

If you know the name of the container, you can filter the results.

By Name:

docker ps --filter "name=<container_name>"

get only the container ID add -q

docker ps --filter "name=<container_name>" -q

Get Only the Container ID

To get only the container ID without the additional table columns, use:

docker ps -q

This lists only the container IDs of all running containers.


Get the ID of a Stopped Container

If the container is not running, use docker ps -a to list all containers, including stopped ones:

docker ps -a

Or filter by name or image for stopped containers:

docker ps -a --filter "name=<container_name>" -q

Find the ID Using the Container Name

If you know the container’s name but not the ID, you can use:

docker inspect <container_name> -f '{{.Id}}'

For example:

docker inspect api-php-1 -f '{{.Id}}'

Output:

5f590b2ae931aecfd4a6704190c9be84be2db432a1d1455f33549415ca52655b

Summary of Commands

TaskCommand
List all running container IDsdocker ps -q
List all containers (including stopped)docker ps -a -q
Filter by container namedocker ps --filter "name=<name>" -q
Filter by image namedocker ps --filter "ancestor=<image>" -q
Get ID using container namedocker inspect <container_name> -f '{{.Id}}'

With these commands, you can easily retrieve the container ID you need for further Docker operations!

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.