Fabian Bosler
Fabian Bosler

Reputation: 2510

Docker Container exits upon running with "sh -c"

I am trying to run a webserver (right now still locally) out of a docker container. I am currently going step by step to understand the different parts.


Dockerfile:

FROM node:12.2.0-alpine as build
ENV environment development

WORKDIR /app
COPY . /app
RUN cd /app/client && yarn && yarn build
RUN cd /app/server && yarn 

EXPOSE 5000
CMD ["sh", "-c","NODE_ENV=${environment}", "node", "server/server.js"]

Explanation: I have the "sh", "-c" part in the CMD command due to the fact that without it I was getting this error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"NODE_ENV=${environment}\": executable file not found in $PATH": unknown.


Building the container:

Building the container works just fine with:

docker build -t auth_example .

It takes a little while since the build context is (even after excluding all the node_modules) roughly 37MB, but that's okay.


Running the container:

Running the container and the app inside works like a charm if I do:

MyZSH: docker run -it -p 5000:5000 auth_example /bin/sh
/app # NODE_ENV=development node server/server.js

However, when running the container via the CMD command like this:

MyZSH: docker run -p 5000:5000 auth_example

Nothing happens, no errors, no nothing. The logs are empty and a docker ps -a reveals that the container was exited right upon start. I did some googling and tried different combinations of -t -i -d but that didn't solve it either.

Can anybody shed some light on this or point me into the right direction?

Upvotes: 0

Views: 346

Answers (1)

AKX
AKX

Reputation: 168913

The problem is you're passing three arguments to sh -c whereas you'd usually pass one (sh -c "... ... ...").

It's likely you don't need the sh -c invocation at all; use /usr/bin/env to alias that environment variable instead (or just directly pass in NODE_ENV instead of environment):

FROM node:12.2.0-alpine as build
ENV environment development

WORKDIR /app
COPY . /app
RUN cd /app/client && yarn && yarn build
RUN cd /app/server && yarn 

EXPOSE 5000
CMD /usr/bin/env NODE_ENV=${environment} node server/server.js

Upvotes: 1

Related Questions