Philip Kirkbride
Philip Kirkbride

Reputation: 22879

Generate a random number in docker-compose file?

I have the following docker-compose file:

version: '2'

services:
  puppetagent:
    build: .
    extra_hosts:
      puppet: "${MASTER_IP}"
    image: 2klic/device:v1
    container_name: test_agent

I'd like to modify container_name: test_agent in a way that appends a random 4 digit number to the name test_agent.

E.g. test_agent1243, test_agent4352, ect.

Is there a convenient way to do something like this?

Upvotes: 2

Views: 6107

Answers (2)

Soumen Mukherjee
Soumen Mukherjee

Reputation: 3262

You can substitute the random number by an environment variable , some thing like this https://docs.docker.com/compose/environment-variables/#the-env-file , so before every scale up you can substitute a random value to the env variable from a script.

Upvotes: 1

Rawkode
Rawkode

Reputation: 22592

Why not just omit container_name and let compose allocate names to it?

Upvotes: 0

Related Questions