6

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

Malik Bagwala
  • 2,695
  • 7
  • 23
  • 38

5 Answers5

4

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\""
            ]
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • This unfortunately did not solve the issue, My containers still stop – Malik Bagwala May 12 '20 at 11:25
  • @malikbagwala in the screenshot I see there are `/n` characters. The example from aws and what I use do not use any new line characters. Maybe this is issue then? – Marcin May 12 '20 at 11:28
4

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 &&.

SomeGuyOnAComputer
  • 5,414
  • 6
  • 40
  • 72
  • 1
    Minor typo. Index 0 of the command array should be "/bin/sh" instead of "bin/sh". Else you will encounter "exec bin/sh failed: No such file or directory". Other than that, this works. – ONMNZ Feb 20 '23 at 18:25
2

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
Matheus Santz
  • 538
  • 6
  • 7
1

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
ONMNZ
  • 322
  • 3
  • 8
0

The issue was with one of the services rk-nginx

This was the previous nginx.conf

upstream gunicorn {
    # docker will automatically resolve this to the correct address
    # because we use the same name as the service: "django"
    server django:8000;
}

However the django keyword gave me issues ( I think it was part of how ecs handles networks), I simply changed it to this

upstream gunicorn {
    server localhost:8000;
}
Malik Bagwala
  • 2,695
  • 7
  • 23
  • 38