Why is my docker node container exiting

I am trying to start a node container with docker-compose -

services:
  node:
    build:
      context: nodejs/
    ports:
      - "3000:3000"
    volumes: 
      - ../nodejs:/usr/src/app
    working_dir: '/usr/src/app'

      

My docker file

FROM node:6.10
EXPOSE 3000

      

The problem is that it exits immediately -

$ docker-compose up
Starting docker_node_1
Attaching to docker_node_1
docker_node_1 exited with code 0

      

And nothing in the logs - docker logs docker_node_1

returns nothing.

There package.json links to the main script -

{
  ...
  "main": "server.js",
  ...
}

      

And my main script is just a simple express server -

const express = require('express');
const app = express();
const port = 3000;

app.listen(port, (err) => {  
  if (err) {
    return console.log('something bad happened', err);
  }

  console.log(`server is listening on ${port}`);
});

      

I'm guessing I'm missing something obvious, but I can't see what it is.

+3


source to share


1 answer


You are not specifying your docker command

. This is a key concept representing container

: the kind of isolated process (process = command, your program)

You can do this in your Dockerfile:

CMD npm start

      



Or in your docker-compose.yml file:

services:
  node:
    command: npm start
    build:
      context: nodejs/
    ports:
      - "3000:3000"
    volumes: 
      - ../nodejs:/usr/src/app
    working_dir: '/usr/src/app'

      

Both approaches are equivalent. But edit it as your needs (npm run, npm run build, etc.)

+1


source







All Articles