2

I am running the startup.sh script from Dockerfile and I get the error below when I run the docker container:

docker run -p 5308:5308 activity-logger
: not found line 2:
: not found line 5:
startup.sh: line 13: syntax error: unexpected end of file (expecting "then")

If I try to run sh startup.sh from my command line it seems to work well. Any ideas?

startup.sh

#!/bin/sh

export env_file=`echo microservice-configuration/$LS_ENVIRONMENT.environment.properties`
export startup_command="npm run start:dist"

if [ -f $env_file ]; then
  echo "Using environment specific configuration file $env_file"
  env $(cat $env_file | xargs) $startup_command
else
  echo "There is no environment specific configuration file $env_file"
  $startup_command
fi

Dockerfile

FROM node:6.9.4-alpine

# Create app directory
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY . /opt/app

RUN npm install -g yarn
RUN yarn
RUN yarn build
# This is not working very well - the dependencies cannot be installed afterwards
# RUN yarn --production

ENV NODE_ENV production

EXPOSE 5308

CMD ["/bin/sh", "startup.sh"]
  • Though this isn't entirely correct; you [cannot portably use the Bash `export var=value` syntax in a POSIX `sh` script](http://unix.stackexchange.com/questions/193095/where-is-export-var-value-not-available). Change the syntax to `var=value export var` or change the shebang to `#!/bin/bash` if you want to give up `sh` compatibility for this minor piece of syntactic sugar. – tripleee Feb 09 '17 at 05:32
  • Also the `echo` in backticks is [useless](http://www.iki.fi/era/unix/award.html#echo); you simply want `env_file=microservice-configuration/$LS_ENVIRONMENT.environment.properties` – tripleee Feb 09 '17 at 05:33
  • And the `cat | xargs` looks really fishy, too. I guess you mean something like `env $(cat "$env_file") $startup_command`? – tripleee Feb 09 '17 at 05:40
  • @MichaelFreidgeim That proposed duplicate seems unrelated; my money would still be on DOS line feeds or some related whitespace wonkiness. – tripleee Jan 25 '21 at 05:32

1 Answers1

5

: not found line 2:

That's pointing to something screwy with the white space in this file. The most likely cause is editing the script on windows and getting the wrong line feeds. Stop using whatever editor you were using and fix the script with a better editor or a utility like dos2unix.

BMitch
  • 231,797
  • 42
  • 475
  • 450