2

Background

When you start a Docker container as in the following example:

$ docker run -ti ubuntu:latest

Docker docker automatically assigns the container an ID and a friendly name.

Docker friendly names are of the format adjective_famousPerson.

Problem

I would like to get the friendly name that Docker will generate for the next container, so that I can assign that name to the hostname parameter of my docker run command.

For example:

$ docker run -ti --hostname docker.next_friendly_name ubuntu:latest

Question

Is there a built in way to do this with docker?

I need a friendly name that is randomly generated, but I don't want to script it up myself if there is a way to do this with Docker or even Docker Swarm or Kubernetes.

Additionally, the name of the container and the hostname of the container need to match.

10
  • Could you explain why --name is not sufficient?
    – 030
    Nov 15, 2019 at 9:29
  • I need the name of the container, and the Linux hostname command from inside the container to match for analytics and logging purposes. So my question is referring to both inside and outside of the container. Nov 15, 2019 at 12:19
  • If you would use for example docker-compose then that will do the routing for you. Did you have a look whether that could solve the issue?
    – 030
    Nov 15, 2019 at 12:50
  • Are you sure Docker is the right tool for the job and you don’t actually need VMs instead? The way you want to have analytics inside the container itself suggests, i think, that you want to use Docker to run small “virtual machines” and not actually containers.
    – Birb
    Nov 15, 2019 at 22:09
  • @030 docker-compose probably actually won't work for me. I will give you the details as to what I am doing, but those details aren't relevant to the question. I am running load tests against an endpoint, and each one of my containers has code to hit that endpoint with, so that I can scale and simulate a few hundred so several thousand possible clients hitting our end point. There are other details I am leaving out, but I'm pulling telemetry data from each of the containers and a friendly name is easier to work with. These are not long term containers. They are random and are scaled. Nov 15, 2019 at 23:23

3 Answers 3

1

As the name of the container is randomly generated you cannot get it before it's generated (by definition). You can use this service to generate random names :

NAME=$(curl -s https://frightanic.com/goodies_content/docker-names.php); \
docker run -ti --name $NAME --hostname $NAME ubuntu:latest
2
0

Yes, there is a built in way. You can define your own "friendly name" by using the --name parameter when creating the container:

docker run -ti --hostname OTHER_CONTAINER_FRIENDLY_NAME --name MY_NAME ubuntu:latest

If you are trying to automate this process then your build tool can supply the name from a variable or via the run script. If you are using a docker-compose file you can specify the name directly in the yaml:

services:
  myservice:
    container_name: example_friendly_name

0

You should be using --name to create your own friendly names. The randomly generated ones you are thinking of are done at run time so you have no possible way of knowing what the next one will be till it's generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.