Search code examples
bashdockerdocker-composeamazon-ecs

aws fargate ECS not able to use the wait command in a docker CMD


Using Docker and docker-compose locally, the following works:

  1. docker-compose.yml contains image and build definitions, as well as args, for each individual container (of which there are eight), e.g:
        App1:
                image: app1
                build:
                        context: ./APP1
                        args:
                                SERVICE_NAME: APP1
                        dockerfile: Dockerfile
                ports:
                        - "8001:8001" # java debug ports
                restart: unless-stopped

etc.

  1. Dockerfile is the same across all eight, as follows:
FROM eclipse-temurin:21.0.1_12-jdk
ARG SERVICE_NAME
ARG APP_DIR=/opt/app
RUN mkdir -p ${APP_DIR}
COPY ${SERVICE_NAME}.sh ${APP_DIR}
COPY dependencies ${APP_DIR}/dependencies
COPY ${SERVICE_NAME}.jar ${APP_DIR}
COPY ${SERVICE_NAME}.local.properties ${APP_DIR}

USER root

RUN chmod -R 777 ${APP_DIR}

# When the container starts, run the startup script
ENV SERVICESCRIPT=${APP_DIR}/${SERVICE_NAME}.sh
CMD exec $SERVICESCRIPT
  1. Each application has its own .sh script which controls startup, in a way similar to the following and using an approach suggested here:
#!/bin/bash

nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8001 $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=docker -XX:+ShowCodeDetailsInExceptionMessages -jar /opt/app/APP1.jar /opt/app/APP1.local.properties > /opt/app/APP1.log &
wait -n
exit $?

This works fine, when I run using docker-compose up locally.

Now, I want to port this to Fargate. I have done this using the following changes:

  1. Update the docker-compose to use the image name containing the ECR repo.
  2. Change local.properties to awstest.properties (so that I am using the correct external endpoints etc)
  3. use docker-compose push to push the images to the ECR repo.

I then configured a fargate task definition and a service to run the application on Fargate, but the containers won't start up. The exception I see in Cloudwatch is as follows:

2024-02-22T12:06:07.141+00:00   /opt/app/APP1.sh: 1: : not found

2024-02-22T12:06:07.141+00:00   /opt/app/APP1.sh: 2: wait: Illegal option -n

Why would this fail on AWS when it works locally?


Solution

  • The issue turned out to be new lines in the file were in Windows format rather than linux. Changing the line endings resolved the issue