Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 851
Aidan Ewen Avatar asked May 31 '17 15:05

Aidan Ewen


People also ask

Why does my docker container keep exiting?

Why docker container is exited immediately? You're running a shell in a container, but you haven't assigned a terminal: If you're running a container with a shell (like bash ) as the default command, then the container will exit immediately if you haven't attached an interactive terminal.

How do you stop a container from exiting?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.

How do I stop an exited docker container?

Remove a container upon exiting If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run --rm to automatically delete it when it exits: Run and Remove: docker run --rm image_name.

How do you stop a docker from detaching?

To stop a container you use the docker stop command and pass the name of the container and the number of seconds before a container is killed. The default number of seconds the command will wait before the killing is 10 seconds.


1 Answers

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)

like image 59
Robert Avatar answered Oct 01 '22 10:10

Robert