<p>As the name of the container is randomly generated you cannot get it before it's generated (by definition). You can use <a href="https://frightanic.com/goodies_content/docker-names.php" rel="nofollow noreferrer">this</a> service to generate random names :</p>

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

CC BY-SA 4.0
2019-11-21T21:48:17.680Z
Your answer has no proof that it's not pregenerated.
Nakilon
CC BY-SA 4.0
2019-11-22T08:45:28.403Z
You can see this : https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go#L832.
Hedi Nasr
CC BY-SA 4.0
2019-11-14T19:33:36.570Z
2019-11-14T19:33:36.570Z
Hedi Nasr
<p>You should be using <code>--name</code> 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. </p>

CC BY-SA 4.0
2019-11-16T00:32:36.930Z
2019-11-16T00:32:36.930Z
joshk132
<p>Yes, there is a built in way.  You can define your own "friendly name" by using the --name parameter when creating the container:<pre><code>docker run -ti --hostname OTHER_CONTAINER_FRIENDLY_NAME --name MY_NAME ubuntu:latest</code></pre></p>

<p>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:<pre><code>services:
  myservice:
    container_name: example_friendly_name</code></pre></p>

CC BY-SA 4.0
2019-11-15T17:49:22.167Z
2019-11-15T17:49:22.167Z
Wesley Rolnick
<p><strong>Background</strong></p>

<p>When you start a Docker container as in the following example:</p>

<pre><code>$ docker run -ti ubuntu:latest
</code></pre>

<p>Docker docker automatically assigns the container an ID and a friendly name.</p>

<p>Docker friendly names are of the format <code>adjective_famousPerson</code>.</p>

<p><strong>Problem</strong></p>

<p>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 <code>docker run</code> command.</p>

<p>For example:</p>

<pre><code>$ docker run -ti --hostname docker.next_friendly_name ubuntu:latest
</code></pre>

<p><strong>Question</strong></p>

<p>Is there a built in way to do this with docker?</p>

<p>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 <strong>Docker</strong> or even <strong>Docker Swarm</strong> or <strong>Kubernetes</strong>.</p>

<p>Additionally, the <code>name</code> of the container and the <code>hostname</code> of the container need to match.</p>

CC BY-SA 4.0
2019-11-15T09:29:08.640Z
Could you explain why `--name` is not sufficient?
030
CC BY-SA 4.0
2019-11-15T12:19:11.570Z
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.
Dodzi Dzakuma
CC BY-SA 4.0
2019-11-15T12:50:03.167Z
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
CC BY-SA 4.0
2019-11-15T22:09:58.080Z
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
CC BY-SA 4.0
2019-11-15T23:23:11.573Z
@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.
Dodzi Dzakuma
CC BY-SA 4.0
2019-11-15T23:27:14.157Z
@Izmaki If you refer to my above comment to 030, you get some of the extra detail, but containers are 100% the correct solution to my problem. However, the problem that I am using containers for, and this question are separate things. If I jumped into topics such as log analytics, simulating random users for testing and validation, this question would be off topic. However, containers have allowed my team to do what it needs testing wise against our application. Without containers, what we have done wouldn't have been possible. Hedi Nasr's answer is probably the right solution.
Dodzi Dzakuma
CC BY-SA 4.0
2019-11-15T23:29:27.030Z
@Izmaki I was just hoping that there was a way to do this built into Docker. Seeing how it already does this. Like a simple flag saying, the hostname for the container and the container name should match, or a flag to say, override the id for the host name of the container with the friendly name assigned to that container.
Dodzi Dzakuma
CC BY-SA 4.0
2019-11-16T00:14:22.923Z
Sounds like Kubernetes might be worth looking into @DodziDzakuma, even though you say that Docker is the right tool for the job.
Birb
CC BY-SA 4.0
2019-11-16T00:22:03.293Z
Kubernetes probably has some features that that would help a lot! That's for sure. I did have Docker Swarm in my quesiton, but I'll Kubernetes as well.
Dodzi Dzakuma
CC BY-SA 4.0
2019-11-16T11:36:47.777Z
I would always prefer k8s over swarm as it has far more features and is more popular.
030
<docker><kubernetes><containers><docker-swarm>
How to get the next friendly name that Docker will assign to the container it will make?
Dodzi Dzakuma
# How to get the next friendly name that Docker will assign to the container it will make?

(Asked by: username_0 on 2019-11-14)

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.

**Tags:** docker, kubernetes, containers, docker-swarm

> **Comments:**
> **username_1**: Could you explain why `--name` is not sufficient?  
> **username_0**: 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.  
> **username_1**: 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?  
> **username_2**: 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.  
> **username_0**: @username_1 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.  
> **username_0**: @Izmaki If you refer to my above comment to username_1, you get some of the extra detail, but containers are 100% the correct solution to my problem. However, the problem that I am using containers for, and this question are separate things. If I jumped into topics such as log analytics, simulating random users for testing and validation, this question would be off topic. However, containers have allowed my team to do what it needs testing wise against our application. Without containers, what we have done wouldn't have been possible. username_3's answer is probably the right solution.  
> **username_0**: @Izmaki I was just hoping that there was a way to do this built into Docker. Seeing how it already does this. Like a simple flag saying, the hostname for the container and the container name should match, or a flag to say, override the id for the host name of the container with the friendly name assigned to that container.  
> **username_2**: Sounds like Kubernetes might be worth looking into @DodziDzakuma, even though you say that Docker is the right tool for the job.  
> **username_0**: Kubernetes probably has some features that that would help a lot! That's for sure. I did have Docker Swarm in my quesiton, but I'll Kubernetes as well.  
> **username_1**: I would always prefer k8s over swarm as it has far more features and is more popular.  

---

## 3 answers

### Answer by username_3 on 2019-11-14. Score: 1 | Accepted answer

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

> **Comments:**
> **username_4**: Your answer has no proof that it's not pregenerated.  
> **username_3**: You can see this : https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go#L832.  

---

### Answer by username_5 on 2019-11-16. Score: 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.


---

### Answer by username_6 on 2019-11-15. Score: 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


---

