Malik Bagwala
Malik Bagwala

Reputation: 3029

How to put multiline commands in ECS Task definition

I am trying to run a django application inside of a docker container (ECS - Fargate)

However, I am having trouble figuring out how to run multiple commands in the Command section of a task definition, currently it is configured like this

enter image description here

Howevery my containers keep on STOPPING and I cant even see the logs in CloudWatch

enter image description here

How do I get it to execute properly?, any help is highly appreciated

Upvotes: 10

Views: 17133

Answers (4)

SomeGuyOnAComputer
SomeGuyOnAComputer

Reputation: 6248

This worked for me using a aws ecs run-task command.

{
  "command": [
    "/bin/sh", "-c",
    "echo 'Hello' && echo ' alien ' && echo 'World'"
  ]
}

The command only has to be split for bin/sh and -c and the rest of the commands can be joined together with &&.

Upvotes: 10

ONMNZ
ONMNZ

Reputation: 328

I have the following yaml in my AWS::ECS::TaskDefinition that works

          Command:
              - /bin/sh
              - -c
              - >-
                echo 'before: showmigrations --list'
                && python manage.py showmigrations --list
                && echo 'before: showmigrations --plan'
                && python manage.py showmigrations --plan
                && echo 'migrate'
                && python manage.py migrate
                && echo 'after: showmigrations --list'
                && python manage.py showmigrations --list
                && echo 'after: showmigrations --plan'
                && python manage.py showmigrations --plan

Upvotes: 1

Matheus Sant Ana
Matheus Sant Ana

Reputation: 690

Made it with && replacing spaces by commas, just like that: enter image description here

touch,/usr/app/log/test1.txt,&&,touch,/usr/app/log/test2.txt

Upvotes: 2

Marcin
Marcin

Reputation: 238747

In your case I would do this by using /bin/sh -c despite the entry point:

/bin/sh -c "python manage.py ... <and the rest>"

This is also how it is done in the offical AWS ECS tutorial:

            "entryPoint": [
                "sh",
                "-c"
            ],
            "command": [
                "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' >  /usr/local/apache2/htdocs/index.html && httpd-foreground\""
            ]

Upvotes: 5

Related Questions