brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set a hostname on a Docker container, or it changes every time you recreate it

Docker
~/tips/tip-docker-run-hostname

docker run --rm alpine hostname

Give a container a fixed hostname so apps do not see a new machine after updates.

Without a hostname setting, a container is named after its own short ID, something like faef50dfeef1. Recreate the container for an update and the hostname is different.

docker run --rm alpine hostname
docker run --rm --hostname media alpine hostname

The first prints a random looking ID, the second prints media. In Compose the same setting is hostname: media on the service.

Most apps never notice, but some do. Media servers and sync tools often show the hostname as the server name in their clients, so after every update a new device appears in the list. Some apps tie a license, a cluster membership or a backup set to the hostname, and then a recreated container looks like a different machine.

I’d set it for anything that has a name visible to other devices. It costs one line and removes a source of odd duplicates.

The hostname is only what the container calls itself. Other containers do not find it by that name; on a user defined network they use the container name or the Compose service name, or a --network-alias. So setting hostname: db does not make a database reachable as db unless the service is called that too. And it does not change what the container is called in docker ps; that is --name.

Further reading: docker container run, Compose file reference: hostname

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link