Tips n Tricks: 04-I Hate, I HATE Random container name

Tips n Tricks: 04-I Hate, I HATE Random container name

Prologue

For the unininitiated in this gurukul, I had been approached by few intelligent and erterprising Gen-Z developers (thats what I prefer calling those buddies) to teach and initiate them to become experts in Docker also, BUT... NOT at all in a Fast Track mode but Slow and Steady mode. AND hence dear friends, I would be sticking to One Tip in a Day in this Tips and Tricks series for Docker.

So here I am, in this series, with the next tip for today

What have we covered so far

We have learnt to:

The Problem statement of today

While creating containers using

docker run nginx        

the system creates random names, which though looks interesting, often becomes a bother if I want to use previous commands from my command history

The Solution

Use the flag "--name"

docker run --name my-nginx nginx        

In the above command, docker assigns the name my-nginx to the container being create

But a word of caution

If there is another container, either in running or stopped or exited state, with the same name, docker would give an error

Obvisouly, you cannot have two containers having the same name

So how do I check the list of stopped container?
docker ps (for running container)
docker ps -a (for listing exited containers also)        
But how to delete the stopped container?
docker rm my-nginx        

Where my-nginx is the name of the container in stopped/exited state.

You can also replace the container name with container ID

Okay. Can I automatically have the container deleted when to stop it, instead of giving another command to delete it?

YES, Yes, yes.

docker run --rm --name my-nginx nginx        

the --rm flag would delete the container when it exits

Multiple commands today, but they all are closely related.

Enjoy and have a great Day ??

#docker #devops

Varad Waghmare

RHCSA | Kubernetes | Openshift | Podman | Cloud Support Engineer | Containers | Orchestration | Linux System Admin | AWS | PaaS | Devops enthusiast | Docker | Ansible

5 个月

Hello Uttam sir, Can you also please let us know the major differences between podman and docker? That will be helpful as well

回复
Amit Bansal

Technical Architect- Software Development

5 个月

Great advice

要查看或添加评论,请登录

Uttam Jaiswal的更多文章

社区洞察

其他会员也浏览了