Console.log not working in a dockerized Node.js / Express app

11,660

Remove the -d from the command you're using to run the container:

docker run -p 49160:8080

The -d option runs the container in the background, so you won't be able to see its output in your console.

If you want to keep the container running in the background and you want to access that container's shell, you can run the following command once your container is up and running:

docker exec -it <container-name> bash
Share:
11,660
human
Author by

human

Updated on June 14, 2022

Comments

  • human
    human almost 2 years

    I have built a Node.js app within a docker container with the following Dockerfile:

    FROM node:carbon
    
    WORKDIR /usr/src/app
    
    COPY package*.json ./
    
    RUN npm install
    
    COPY . .
    
    EXPOSE 8080
    CMD [ "npm", "start" ]
    

    I am trying to console.log within an express route, however after docker run -p 49160:8080 -d, the console is not interactive and logs are not being echoed at all.

    'use strict';
    
    // Requires
    const express = require('express');
    
    // Constants
    const PORT = 8080;
    const HOST = '0.0.0.0';
    
    // App
    const app = express();
    
    // Routes
    app.get('/', (req, res) => {
        // This isn't being printed anywhere
        console.log(req);
    });
    
    // Start
    app.listen(PORT, HOST);
    console.log(`Running on http://${HOST}:${PORT}`);
    

    What am I doing wrong?