How to put multiline commands in ECS Task definition

Viewed 6255

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

3 Answers

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\""
            ]

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;
}

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

Related