Reputation: 1423
I am running docker for the first time and my container is exiting immediately after docker run command. The docker ps
is showing me an empty list. When I run docker ps -a
I am getting the results of all the containers I pushed in Exited state.
I have tried using -ti
command but the container is going in exit state.
I am using following commands to run the file: $ sudo docker build -t test_api3 .
and $ sudo docker run -p 8080:3000 -ti test_api3
or ($ sudo docker run -p 8080:3000 -d test_api3
)
Dockerfile
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm","start" ]
package.json
{
"name": "firstapp",
"version": "1.0.0",
"description": "first demo app",
"main": "http-server.js",
"scripts": {
"start": "node http-server.js"
},
"keywords": [
"S"
],
"author": "test",
"license": "ISC"
}
http-server.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Upvotes: 4
Views: 2894
Reputation: 484
I am running ubuntu os and it works (Let me know your setup if it still cannot work). The only problem with the code is that you cannot use the address 127.0.0.1 and must use 0.0.0.0 in your http-server.js. For more explanation refer to the link below
https://forums.docker.com/t/network-param-for-connecting-to-127-0-0-1-in-container/2333
Upvotes: 1