简体   繁体   中英

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?

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

Adding this comment as I was searching for the same thing and this was first result on Google.

docker logs <container-name>

https://docs.docker.com/engine/reference/commandline/logs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM