Dynamically get a running container id/name created by docker run command
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you create a container with docker run and then need its id or name for later commands, the best approach is to capture that information at creation time. Docker already returns the container id in detached mode, and it can also write the id to a file. Polling docker ps after the fact can work, but it is much less reliable in automation because multiple similar containers may be running.
Easiest Case: Detached Mode Returns the Container ID
When you run a container with -d, Docker prints the container id to standard output.
This is the cleanest pattern for scripts because the id comes directly from the creation command. No extra lookup is required.
Best Practice: Assign a Name Explicitly
If your workflow will refer to the container multiple times, naming it is often better than chasing the id.
A predictable name makes scripts easier to read and debug. It also avoids accidental matches when multiple similar containers exist.
Use --cidfile for Robust Automation
For shell automation, --cidfile is very useful. Docker writes the created container id to a file.
This is especially helpful when:
- you want the id even if command output is redirected,
- a script is split across steps,
- another tool needs to consume the id later.
It is a more deterministic interface than grepping docker ps.
Foreground Containers Need a Different Pattern
If you run without -d, Docker attaches to the container process instead of returning the id in a script-friendly way. In that case, naming the container or using --cidfile is the better solution.
Example:
Even if the process runs in the foreground, you still have a reliable id in /tmp/batch.cid.
Why docker ps Lookup Is a Fallback, Not a Primary Method
You can search for a running container after creation:
Or by name pattern:
This is fine for debugging, but it is weaker in automation because:
- multiple containers can match,
- timing can race with startup,
- ids are no longer tied to the exact
docker runinvocation.
Prefer creation-time capture whenever possible.
Container Name Versus Container ID
Choose the identifier based on the workflow:
- use container id when you need exact machine-level identity,
- use
--namewhen humans or scripts will interact with the container repeatedly, - use both in more structured automation.
Example:
Now you have:
- a stable human-readable name,
- the exact container id stored for machine use.
Integrating with Later Docker Commands
Once you have the id or name, every follow-up command becomes straightforward:
The important part is that the identifier comes from the same lifecycle event that created the container.
Common Pitfalls
- Parsing
docker psoutput after the fact when the id could have been captured directly fromdocker run -d. - Avoiding
--nameand then writing harder-to-maintain scripts that depend only on opaque ids. - Assuming an image-based
docker ps --filter ancestor=...query identifies one unique container. - Running a foreground container without
--cidfileand then struggling to recover the exact id reliably. - Hardcoding container ids that change every time the container is recreated.
Summary
- In detached mode,
docker run -dalready returns the container id. - Use
--namewhen you want a stable, readable handle for later commands. - Use
--cidfilewhen automation needs the exact id in a robust way. - Treat
docker pslookups as a fallback, not the primary integration method. - Capture the identifier at container creation time whenever possible.
Related reading
- dynamo db local shell doesn't list tables using docker image
- econnrefused 127.0.0.15672 Rabbit-mq with docker compose
- ECONNREFUSED for Postgres on nodeJS with dockers
- ECS/ECR is common practice to have one repository per image and associated versions?
- DynamoDB table created by Terraform in LocalStack not visible in NoSQL Workbench
- EC2 Instance - Sending STDOUT logs to Cloud Watch
- ElasticSearch in Windows docker image vm max map count
- Enable Ingress controller on Docker Desktop with WLS2

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.