Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-21T21:48:17.680Z
Answers: Your answer has no proof that it's not pregenerated.
Answers: Nakilon
Answers: CC BY-SA 4.0
Answers: 2019-11-22T08:45:28.403Z
Answers: You can see this : https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go#L832.
Answers: Hedi Nasr
Answers: CC BY-SA 4.0
Answers: 2019-11-14T19:33:36.570Z
Answers: 2019-11-14T19:33:36.570Z
Answers: Hedi Nasr
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-16T00:32:36.930Z
Answers: 2019-11-16T00:32:36.930Z
Answers: joshk132
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-15T17:49:22.167Z
Answers: 2019-11-15T17:49:22.167Z
Answers: Wesley Rolnick
Body: <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>
  
Comments: CC BY-SA 4.0
Comments: 2019-11-15T09:29:08.640Z
Comments: Could you explain why `--name` is not sufficient?
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-11-15T12:19:11.570Z
Comments: 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.
Comments: Dodzi Dzakuma
Comments: CC BY-SA 4.0
Comments: 2019-11-15T12:50:03.167Z
Comments: 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?
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-11-15T22:09:58.080Z
Comments: 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.
Comments: Birb
Comments: CC BY-SA 4.0
Comments: 2019-11-15T23:23:11.573Z
Comments: @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.
Comments: Dodzi Dzakuma
Comments: CC BY-SA 4.0
Comments: 2019-11-15T23:27:14.157Z
Comments: @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.
Comments: Dodzi Dzakuma
Comments: CC BY-SA 4.0
Comments: 2019-11-15T23:29:27.030Z
Comments: @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.
Comments: Dodzi Dzakuma
Comments: CC BY-SA 4.0
Comments: 2019-11-16T00:14:22.923Z
Comments: Sounds like Kubernetes might be worth looking into @DodziDzakuma, even though you say that Docker is the right tool for the job.
Comments: Birb
Comments: CC BY-SA 4.0
Comments: 2019-11-16T00:22:03.293Z
Comments: 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.
Comments: Dodzi Dzakuma
Comments: CC BY-SA 4.0
Comments: 2019-11-16T11:36:47.777Z
Comments: I would always prefer k8s over swarm as it has far more features and is more popular.
Comments: 030
Tags: <docker><kubernetes><containers><docker-swarm>
Title: How to get the next friendly name that Docker will assign to the container it will make?
UserDisplayName: Dodzi Dzakuma
ThreadText: # 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 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. username_3 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 @username_0, 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](https://frightanic.com/goodies_content/docker-names.php) 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
  ```
  
  
  ---
  
  