Skip to content

How can I set Docker labels when creating ECS tasks via AWS CLI?

0

ECS CLI Docker Labels How can I set Docker labels when creating ECS tasks via AWS CLI? I want to use dockerLabels in my ECS task definition JSON passed via aws ecs register-task-definition. How do I structure the CLI command to include them?

1 Answer
1
Accepted Answer

You can set Docker labels in your ECS task definition when using the AWS CLI by including them in the container definitions section of your task definition JSON. Docker labels are specified using the dockerLabels parameter, which is a key-value map of labels to add to the container.

Here's how to structure your register-task-definition command with Docker labels:

aws ecs register-task-definition \
--family "your-task-family" \
--container-definitions "[{
\"name\": \"your-container-name\",
\"image\": \"your-image\",
\"cpu\": 10,
\"memory\": 10,
\"essential\": true,
\"dockerLabels\": {
\"label1\": \"value1\",
\"label2\": \"value2\"
}
}]"

For a more complex task definition, you might want to store your JSON in a file and reference it:

aws ecs register-task-definition --cli-input-json file://task-definition.json

Where your task-definition.json file would include the dockerLabels parameter in the container definitions:

{
"family": "your-task-family",
"containerDefinitions": [
{
"name": "your-container-name",
"image": "your-image",
"cpu": 10,
"memory": 10,
"essential": true,
"dockerLabels": {
"label1": "value1",
"label2": "value2"
}
}
]
}

Note that Docker labels are added at the container definition level, not at the task level, and they cannot be overridden or set dynamically when running a task with the run-task command. They must be defined in the task definition itself.
Sources
Amazon ECS examples using AWS CLI - AWS SDK Code Examples
Amazon ECS task definition parameters for the Fargate launch type - Amazon Elastic Container Service
Amazon ECS task definition parameters for the EC2 launch type - Amazon Elastic Container Service
how to override / set Docker Labels by using the AWS-cli ecs run-task | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.