0

Contents of Dockerfile:

FROM scardon/ruby-node-alpine:latest

WORKDIR /opt
COPY ./docker/docker-entrypoint.sh /usr/bin/

RUN apk update \
  && apk add --no-cache --virtual .build_deps \
  && apk add --no-cache --virtual .run_deps python make \
  && gem install slim \
  && npm i -g gulp-cli gulp \
  && apk del .build_deps \
  && chmod +x /usr/bin/docker-entrypoint.sh

EXPOSE 3000
VOLUME [ "/opt" ]

CMD ["docker-entrypoint.sh"]

Contents of docker-entrypoint.sh:

#!/usr/bin/env sh

exec npm i
exec npm run start

When i try to run the container with foreground mode:

docker run --rm -p 3000:3000 -v ~/Sites/app:/opt imagename

It fires npm i command and exit. How to keep npm run start in a background, so it will be running all the time.

UPDATED for Patok:

exec node /var/www/project/backend/server.js > /var/log/node-server.log &
exec /usr/sbin/nginx -g "daemon off;"
exec tail -f /var/log/nginx/project.error.log & tail -f /var/log/node-server.log

1 Answer 1

1

using multiple exec statements in script isn't possible, because it replaces current process by command running after exec. You could possibly use this:

#!/usr/bin/env sh

npm i
exec npm run start

or run npm i outside of this shell script (ie. directly in your Dockerfile's RUN directive).

8
  • i have a project, where's triple exec in a shell script (cmd). How do they work? Also i cant use npm i in a dockerfile's RUN, because i mount volume on a docker run command. Jan 3, 2019 at 17:04
  • If you have multiple exec statements in your file, only first one command after exec is started, another ones are ignored. you can of course have multiple exec statements when you use proper branching with if / elif / else statements, but only one at a time is started depending on arguments or variables passed or any other external data being processed. because exec statement replaces script process itself, the script is not being processed anymore after the first command. try testing it in shell when you modify your script - instead of npm commands use exec test1 and exec test2
    – patok
    Jan 4, 2019 at 12:18
  • You mean in my example, only first 2 exec's are fired? Jan 4, 2019 at 15:13
  • not only 2... only one is fired - the first one. Again - when you have script with multiple subsequent exec commands, then only FIRST ONE is run and replaces shell (sh in your script)... at that point no one of following exec will be started even the first exec command already ends its processing.
    – patok
    Jan 4, 2019 at 19:14
  • But the first one is going background, and second one is fired and the third in my case, because i can see the updated logs. Everything is fired in my example, but why? If you say only the first one would be fired? Jan 4, 2019 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .