Why is container exiting immediately after executing docker run command?

Viewed 2128

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}/`);
    });
1 Answers
Related