3 Tips For Naming Docker Containers
When you create a docker container, it is assigned a universally exclusive symbol (UUID). These are necessary to elude naming conflicts and promote automation without humankind 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 humankind-readable long display or the more frequently shown
12 character short form, which might look something like
To support 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 ergodic names don't give any more perception into the container function than the UUID.
Here are three tips that can make it easier to keep your relatednesses as you learn to work with containers.
1 Name the container when you run it
--name=meaningful_name to the
docker run command, an
evil_ptolomy becomes more perceptible in interactive sessions as well as in the production of regulates like
docker ps. There are limitations, however. Since container names must be distinctive, you cannot use intended naming and scale a service beyond one container.
On the Command line or in a dockerfile:
docker run --name=meaningful_name
For instance, if we ran a container based on the
nginx base graphic and commenced
it like this:
- docker run --name nginx -d nginx
The name would appear in the database of running containers:
- docker ps
OutputCONTAINER 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 production of
docker ps and can be used to oversee the container, it will not appear in the command prompt of the container if you attach to it or in log records. In order to accomplish that, you'll also need to assign a hostname.
2 Assign a hostname to the container
The ideal given
--hostname command is set inside
/etc/hosts inside the container. Consequently, it appears in command prompt. It plays a role in configuring container DNS and can be useful while in the learning levels of a multi-container setup. It is not uncomplicated to accesses from outside the container, but it will appear in the containers log records, and when those records are written to a volume private of the host, it can make it easier to identify the container.
cli and Dockerfile:
docker run --hostname=ideal OR
docker run -h ideal
--hostname are both helpful for identification of containers, sometimes, it's not about naming the container at all. Rather, it's about having a container cleanable up after itself without having to remember to do it yourself.
3 Automatically delete containers when they exit
When debugging, its useful that a stopped container persists after it exits. You can retain data like log records and investigate the containers closing attribute. 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 erase it when it exits. This can make it easier to keep things cleanable.
Be cautious, though! If you're using Docker volumes,
--rm will remove any volumes NOT selected
cli and Dockerfile:
docker run --rm
This is very helpful when you're building an illustration 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.
These three flags for
--rm can each, in their own route, 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 govern.