Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 517
human Avatar asked Jul 27 '26 18:07

human


1 Answers

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
like image 76
Christian Santos Avatar answered Jul 29 '26 08:07

Christian Santos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!