Which command allows passing the node hostname as an environment variable into each task in a service?

Prepare for the Docker Certified Associate test with our comprehensive quizzes that include key questions and detailed explanations. Ace your certification!

The correct choice for passing the node hostname as an environment variable into each task in a service is achieved through the command that uses the syntax --env NODE_HOSTNAME="{{.Node.Hostname}}". This approach directly assigns the node's hostname dynamically to an environment variable named NODE_HOSTNAME. In the context of Docker services, this is particularly useful when deploying tasks across a Swarm cluster, as it allows each container to access information about the node it is running on, providing flexibility and context awareness.

Using NODE_HOSTNAME as the environment variable name makes it clear what information is being represented, thereby improving code readability and maintainability for anyone who works with the service configuration in the future. It is a common practice in container orchestration to pass essential information about the environment into containers to facilitate various operational needs.

The other options do not align with the intended functionality. For instance, while the first option uses a similar syntax, the assigned variable does not have a standard naming convention that reflects the data being passed. The option using --hostname instead modifies the hostname of the container itself rather than assigning it as an environment variable, which does not meet the requirement of passing the hostname into tasks. The docker run command is meant for

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy