'docker run program arguments in aws ecs

I have a working container in Amazon's ECS that runs a program as a task. I would like to pass some program arguments, as I would do when running locally with docker run. I have managed to do passing a new entrypoint in the container configuration in ECS, as if I were passing it in the docker run command line.

Unfortunately, when doing so, I am overriding the internal entrypoint that was already defined in the image. I would like to use the internal entrypoint, just adding some more command line arguments, like --debug options. Is there any way to do that?

Thanks in advance.



Solution 1:[1]

1. If you normally pass in command line arguments to your script like

python myscript.py --debug --name "joe schmoe" --quality best --dimensions 1920 1080

2. And you have a docker image with an entrypoint to run that script like

FROM python:3.7-alpine

# Add the application folder to the container
COPY . /myscript
WORKDIR /myscript

# Install required packages
RUN pip install -r requirements.txt --upgrade

# Run the script when the container is invoked
ENTRYPOINT ["python", "myscript.py"]

3. Then when editing a task/container definition via the aws ecs user interface, you must place the arguments into the "Command" field under the "Environment" section of the container settings and replace all spaces between arguments and values with commas, like so:

--debug,--name,joe schmoe,--quality,best,--dimensions,1920,1080

Items with spaces like joe schmoe are quoted as "joe schmoe", which is why something like --quality best wouldn't work, and instead needs to be comma delimited as --quality,best

enter image description here

4. After creating the task, if you take a look at the container details in the task definition, the command is displayed as:

["--debug","--name","joe schmoe","--quality","best","--dimensions","1920","1080"]

which is the same syntax accepted by the CMD instruction in a Dockerfile.

enter image description here

Solution 2:[2]

When you run a task in ECS you can specify container overrides.

In the AWS console this can be found at the bottom in the Advanced Options section.

enter image description here

On the CLI you can pass in a JSON object with the overrides like so:

aws ecs run-task ... --overrides '{"containerOverrides": [{"name": "whatever", "command": ["foo", "bar"}]}'

The command is the CMD that gets executed inside the container.

In the same way environment variables can be passed to a container. Here's the list of possible options as described in the aws-cli docs:

{
  "containerOverrides": [
    {
      "name": "string",
      "command": ["string", ...],
      "environment": [
        {
          "name": "string",
          "value": "string"
        }
        ...
      ],
      "cpu": integer,
      "memory": integer,
      "memoryReservation": integer
    }
    ...
  ],
  "taskRoleArn": "string",
  "executionRoleArn": "string"
}

For some reason the name always has to be set in the overrides. ¯\_(?)_/¯

Solution 3:[3]

You can add command line arguments for the container entry point when creating new ECS task revision in AWS console. Open your container settings, and under the ENVIRONMENT label put the comma-separated list of command line arguments into the "Command" field.

Example:

--debug,--packages,org.apache.hadoop:hadoop-aws:2.7.3

will add 2 command line arguments to your container entry point.

Solution 4:[4]

It was easy: the command line arguments can be passed as Command in ECS configuration, instead of entrypoint.

Solution 5:[5]

Use environment section in ecs task definition to inject your configs.

"environment" : [
    { "name" : "string", "value" : "string" },
    { "name" : "string", "value" : "string" }
]

Please refer to the following aws documentation http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_environment

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 udondan
Solution 3 xanadont
Solution 4 cserpell
Solution 5 Mogsdad