Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerized React App failed to bind to $PORT on Heroku

I'm trying to deploy a Dockerized React App to Heroku, but keep getting the

"R10: Failed to bind to $PORT error on Heroku"

.

The dockerized app runs perfectly fine when i docker run it locally.

My docker file looks like the following:

FROM node:10.15.3
RUN mkdir -p /app
WORKDIR /app
COPY . .

ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN npm install --verbose
RUN npm install serve -g -silent

# start app
RUN npm run build
CMD ["serve", "-l", "tcp://0.0.0.0:${PORT}", "-s", "/app/build"]

I followed the online solution to change the "listening" port on serve to $PORT from Heroku. Now the application is served on Heroku's port according to logs, but still, get the

"Failed to bind to $PORT error"

.

Please help!

like image 342
preyansh98 Avatar asked Jan 21 '26 09:01

preyansh98


1 Answers

variable substitution does not happen in CMD that is why ${PORT} is consider as a text instead of consuming its value.

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

docker-cmd

Change CMD to

CMD ["sh", "-c", "serve -l tcp://0.0.0.0:${PORT} -s /app/build"]
like image 183
Adiii Avatar answered Jan 23 '26 01:01

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!