Tutorial

3 Tips for Naming Docker Containers

Updated on January 5, 2018
3 Tips for Naming Docker Containers

Introduction

When you create a Docker container, it is assigned a universally unique identifier (UUID). These are essential to avoid naming conflicts and promote automation without human intervention. They effectively identify containers to the host and network. However, they take more effort for humans to differentiate between, whether in the 64 character human-readable long display or the more frequently displayed 12 character short form, which might look something like 285c9f0f9d3d.

To help the humans, Docker also supplies containers with a randomly-generated name from two words, joined by an underscore, e.g. evil_ptolemy. This can make it easier to tell one container from another, but the random names don’t give any more insight into the container function than the UUID.

Here are three tips that can make it easier to keep your bearings as you learn to work with containers.

1 — Name the container when you run it

By adding --name=meaningful_name to the docker run command, an evil_ptolomy becomes more recognizable in interactive sessions as well as in the output of commands like docker ps. There are limitations, however. Since container names must be unique, you cannot use deliberate naming and scale a service beyond one container.

On the Command Line or in a Dockerfile: docker run --name=meaningful_name

For example, if we ran a container based on the nginx base image and started it like this:

  1. docker run --name nginx -d nginx

The name would appear in the list of running containers:

  1. docker ps
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 08f333ef7216 nginx "nginx -g 'daemon off" 15 seconds ago Up 14 seconds 80/tcp, 443/tcp nginx

While the name appears in the output of docker ps and can be used to manage the container, it will not appear in the command prompt of the container if you attach to it or in log files. In order to accomplish that, you’ll also need to assign a hostname.

2 — Assign a hostname to the container

The value supplied to the --hostname command is set inside /etc/hostname and /etc/hosts inside the container. Consequently, it appears in command prompt. It plays a role in configuring container DNS and can be helpful while in the learning stages of a multi-container setup. It is not easy to access from outside the container, but it will appear in the container’s log files, and when those files are written to a volume independent of the host, it can make it easier to identify the container.

CLI and Dockerfile: docker run --hostname=value OR docker run -h value

While --name and --hostname are both useful for identification of containers, sometimes, it’s not about naming the container at all. Rather, it’s about having a container clean up after itself without having to remember to do it yourself.

3 — Automatically delete containers when they exit

When debugging, it’s helpful that a stopped container persists after it exits. You can retain data like log files and investigate the container’s final state. Sometimes, however, you know when you run the container that you won’t want it around when you’re done. In this case, you can use the --rm flag to automatically delete it when it exits. This can make it easier to keep things clean.

Be careful, though! If you’re using Docker volumes, --rm will remove any volumes NOT specified by name.

CLI and Dockerfile: docker run --rm

This is very useful when you’re building an image and need to attach to a running container. You want to look around, and you don’t want to fill up your disk with containers you don’t intend to use again.

Conclusion

These three flags for docker run, --name, --hostname, and --rm can each, in their own way, make it easier to know what’s what when learning Docker. You can learn more about containers and working with the docker run command in the Working with Docker Containers guide.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Informative, thanks.

Simple and Nice article. Please make more of these.

hello i love if some one can help me with that i cant find the wrong commend here :

docker stop blockchain-service docker rm blockchain-service docker run --name blockchain-service -d –restart always -v ~/ethereum/:/var/run/ethereum -e GETH_IPC_PATH=/var/run/ethereum/ethereum-data.ipc -e SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/407151891480/geth-mainnet-transactions -e MASTER_WALLET_ADDRESS=0xMASTER_KEY --log-opt max-size=100m --log-opt max-file=3 -e SQS_PAYMENT_REQUESTS_QUEUE_URL=geth-mainnet-payment-requests -e FAKE_USER_WALLET_PRIVATE_KEY=PRIVATE_KEY -e FAKE_USER_WALLET_PUBLIC_KEY=0xPUBLIC_KEY -e AWS_ACCESS_KEY_ID=ACCESS_KEY -e AWS_SECRET_ACCESS_KEY=ACCESS_KEY -e ENV=prod -e EXPECTED_TRANSACTION_AMOUNT=1000000000000000000000 -e DB_PORT=5432 -e DB_NAME=endordb -e DB_USER=endor_admin -e DB_HOST=MY_DB.us-east-1.rds.amazonaws.com -e DB_PASSWORD=NO_PASSWORD -e SLACK_TOKEN=xoxp-14092887333-345643415810-495051526626-e 2e61b404d4be9b0a7002ae01e25991a 407151891480.dkr.ecr.us-east-1.amazonaws.com/blockchain-service

Thank you.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.