I have graphql node server. I am able to run it locally without docker. But after creating a docker container of the server and binding the container port with the host port this doesn't work.
Here's my Dockerfile code:
FROM node:boron-alpine
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
ENV SERVER_PORT 8080
EXPOSE 8080
CMD npm run build && npm start
My node server code is as such :
app.listen(process.env.SERVER_PORT,'0.0.0.0')
console.log(`listening at ${port}`)
I run docker with the following command:
docker run -it -p 8080:8080 nodeapi
This works perfectly nodejs can see the env SERVER_PORT cause it shows
"listening at 8080"
in the console.
But when i open localhost:8080 this doesn't work (The site cannot be reached).
I have also tried running docker command
docker run -it -p 127.0.0.1:8080:8080 nodeapi
This doesn't work
I am using docker toolbox on windows 10 latest build
Thank you
Docker toolbox doesn't map ports to localhost. It maps it to the Docker VM IP's
Run below command to get the IP
docker-machine ip
Then use the http://<IP>:8080
in your browser
If you avoid specifying the IP 0.0.0.0
app.listen(process.env.SERVER_PORT, function () {
console.log('Listening on port '+ process.env.SERVER_PORT);
});
and
docker run -it -p 8080:8080 nodeapi
it will let you load the site as
http://localhost:8080
when you start the Docker using docker quickstart terminal, you can see the IP Address. (you can get to know the ip-address using the command "docker-machine ip" as well)
URL to access will be http://ip-address:portbinded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With