0

I developed apps and run them on the docker.

I'd like to see logs during the development.

I can see them by docker logs api-server.

But now I must send this command every time for debut.

So I decided to enter container by following command.

docker exec -it api-server sh

And then I want to watch logs real timely.

Are there any method to watch realtime logs on server(for example access log) ?

Dockerfile of api-server is following

FROM node:alpine

WORKDIR /src
COPY package.json .

RUN rm -rf /src/node_modules
RUN rm -rf /src/package-lock.json
RUN apk --no-cache add curl



RUN yarn install

CMD yarn start:debug

package.json is following

"scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
Heisenberg
  • 4,787
  • 9
  • 47
  • 76

1 Answers1

0

Since you're doing docker exec, I'm assuming you're either running api-server in detached mode or you're accessing this container from another place (other window or pc) to do docker exec.

If you want an "access.log" type of behavior, you can certainly write your docker logs api-server to a file.

Look at this answer https://stackoverflow.com/a/48345336 The -f flag will continue writing to this file everytime api-server spits out a new stdout/stderr, so you don't need to keep doing docker logs api-server.

On a side note, for complicated logging and workflows for containers, (especially container resource statistics), I use the Docker SDK, where you can use container.logs(stdout=True) and write this to a file, so that everything will be captured without you doing anything.

Alternatively, you can create a script that writes the stdout/stderr to a docker volume shared between some network drive or host machine, and in your Dockerfile, just copy this script over to your $HOME/.profile (or .bashrc depending on your shell).

Dharman
  • 30,962
  • 25
  • 85
  • 135
AGIII
  • 48
  • 1
  • 5
  • Thanks As I am begineer, I'd like to know where must I write `docker logs api-server` ? I tried`docker-compose up` and `docker logs -f api-server` but they didn't omit access log.As I developed `API` it was big issue.. Thanks – Heisenberg Oct 16 '20 at 06:24