Aidan Ewen
Aidan Ewen

Reputation: 13308

Why is my docker node container exiting

I'm trying to run 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 it exits immediately -

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

And there's nothing in the logs - docker logs docker_node_1 returns nothing.

There's a package.json referencing 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 guess I'm missing something obvious but I can't see what it is...

Upvotes: 14

Views: 22253

Answers (1)

Robert
Robert

Reputation: 36763

It's missing specifying the docker command. That is the key concept that represents the container: sort of isolated process (process = the command, your program)

You can do it in Dockerfile:

CMD npm start

Or in docker-compose.yml:

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)

Upvotes: 20

Related Questions