Search code examples
node.jstypescriptdockerexpresswinston

Winston logs does not work in the Docker Container


I am creating a nodejs/express app with typescript. I have installed the winston package. using npm install winston. I am following this article. i want to dockerize this.

What i have done so far:

  1. I have imported the winston package in the logger.ts.
  2. import the logger.js file to the server.ts file.

Winston logger works when I run npm run dev command. I see the winston logs in the console. When i implement the same in docker container.

I use docker build -t docker-image . command then docker-compose up.

My Dockerfile:

FROM node:10
WORKDIR /app
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . /app

EXPOSE 5000
CMD [ "node", "dist/server.js" ]

docker-compose.yml file:

version: '3'
services:
  app:
    container_name: hoptik-pims-node
    restart: always
    build: .
    ports:
      - '5000:5000'
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    ports:
      - '2717:27017'

I try to access the page http://localhost:5000/logger. I get Cannot GET /logger. enter image description here I do not understand what I am doing wrong. I have spent last couple of days trying fix this issue. Can someone help me fix this.

Thanks in advance.


Solution

  • It seems you are doing it wrong, in order to view logs you should not be going to localhost:5000/logger, instead you should be looking at docker logs using following command

    docker logs <your-container-id> -f
    

    Example looks like

    docker logs 2921e28c845f -f 
    

    OR

    docker logs 2921e28c845f5ba9c97c371b81e1d68db28650e576ea18b78592713362baa140 -f
    

    This should show you proper logs.