aksakalli / gtop

System monitoring dashboard for terminal
MIT License
9.74k stars 326 forks source link

Running gtop in Docker #50

Closed pascalandy closed 5 years ago

pascalandy commented 7 years ago

Environment

Description

For the docker folks, I'd like to do a PR but I need your help first.

Dockerfile

FROM node:4

RUN apt-get update -y && apt-get upgrade -qy && \
    npm install gtop -g

ENTRYPOINT ["gtop"]

Try it

docker run --rm -it \
--name gtop \
-e LANG=en_US.utf8 -e TERM=xterm-256color \
devmtl/gtop:stable

Issue

We can see in the screenshot that the processes are those running in the container and not those from the host.

My question is: Which folder(s) should we mount in the container ?

screen shot 2017-09-26 at 2 07 29 pm

Many cheers!

aksakalli commented 7 years ago

hi @pascalandy ,

gtop relies on top ps command for getting process information. Maybe these resources can help you to figure it out:

pascalandy commented 7 years ago

Humm the only thing I could see is

docker run --rm -it \
--name gtop \
-v /proc/stat:/proc/stat \
-e LANG=en_US.utf8 -e TERM=xterm-256color \
devmtl/gtop:stable

But it does not work.

aksakalli commented 7 years ago

/proc is a virtual file system which contains process information. /proc/stat has cpu information and already mounted as it is from the host:

screenshot from 2017-09-26 23-58-14

Processes are actually listed under /proc as /proc/ID:

screenshot from 2017-09-26 23-58-14 another copy

So if I try to mount /proc as docker run --rm -it --name gtop -v /proc:/proc:ro gtop, I am getting following error:

docker: Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"rootfs_linux.go:57: mounting \\\"/proc\\\" to rootfs \\\"/var/lib/docker/aufs/mnt/0c0d46f3d42d69fb91e850f6a88d13c34436a5d0639022efffe9fd68ac9110a7\\\" at \\\"/proc\\\" caused \\\"\\\\\\\"/var/lib/docker/aufs/mnt/0c0d46f3d42d69fb91e850f6a88d13c34436a5d0639022efffe9fd68ac9110a7/proc\\\\\\\" cannot be mounted because it is located inside \\\\\\\"/proc\\\\\\\"\\\"\"".

To tackle this, other projects such as cAdvisor and prometheus mount the host /proc to a different folder like /proc:/host/proc (see also https://github.com/prometheus/node_exporter/issues/66).

gtop uses ps command for process table, if it is running inside a container, it should get process information from /host/proc instead of /proc.

aksakalli commented 7 years ago

also for Network History, you need to add --net="host" parameter to docker run

pascalandy commented 7 years ago

We are getting closer :) The network now appears but the processes are still those from the container.

docker run --rm -it \
--name gtop \
--net="host" \
-v /proc:/host/proc:ro \
-e LANG=en_US.utf8 -e TERM=xterm-256color \
devmtl/gtop:stable
screen shot 2017-09-26 at 7 26 39 pm
ianmiell commented 7 years ago

You probably want to run as privileged. It might want to be called out here that this is more a docker than a gtop question.

jcberthon commented 7 years ago

For the process list, you might want to use —pid=host.

Running in privileged mode is a bad idea. You don’t run top or htop with sudo. Docker provides capability management. If you’re denied permission to access a resource, check if you can add a corresponding capability.

I can help more and even test. But not today.

aksakalli commented 7 years ago

@jcberthon it worked!

here is Dockerfile:

FROM node:4

ENV LANG=en_US.utf8 \
    TERM=xterm-256color

RUN apt-get update -y && apt-get upgrade -qy && \
    npm install gtop -g

ENTRYPOINT ["gtop"]

Building and running the image:

docker build -t gtop .
docker run --rm -it --name gtop --net="host" --pid="host" gtop

More explanation about --pid: https://docs.docker.com/engine/reference/run/#pid-settings-pid

pascalandy commented 7 years ago

Thanks @aksakalli Just submit a PR !

I also used FROM node:4-slim

pascalandy commented 7 years ago

Now, I'm wondering how to package this via zeit-pkg as this image's size is 259MB

REPOSITORY                                       TAG                                             IMAGE ID            CREATED             SIZE
devmtl/gtop                                      stable                                          c221d2221580        14 minutes ago      259MB

I can't replicate the example from @marcosnils to this project which uses npm install.

See an example where zeit pkg downsize the image size from 300Mo to 100Mo - https://hackernoon.com/reducing-nodejs-docker-images-size-by-50-using-multi-sage-builds-and-zeit-pkg-360ab8b6c6d2

Cheers!

jcberthon commented 7 years ago

I have never use npm/node/pkg but from reading the manual from pkg I think you need to define so-called "assets" so that they are "bundled" in the resulting file. See https://www.npmjs.com/package/pkg#assets

In the example from Hackernoon, the "helloworld" is so simple that there is probably no needs for assets, hence it probably works for them.

In the assets, you should put your dependencies I guess. But as I said, I have no clue about npm/node/etc. I know well Docker.

jcberthon commented 7 years ago

A few more things. In the Dockerfile, you should create a user and define it as the image's user. So that the container is run as a standard user and not root. This is particularly important since you want to give access to all other processes via --pid host and to your host network devices.

You should also assess if you need an init system for clean shutdown and avoiding zombies. I guess you are not forking any processes and if you do not store any data, then this might not be relevant.

pascalandy commented 7 years ago

Same for me:

But as I said, I have no clue about npm/node/etc. I know well Docker.

I agree as well and it's a work in progress :)

This is particularly important since you want to give access to all other processes via --pid host and to your host network devices.

ianmiell commented 7 years ago

https://zwischenzugs.wordpress.com/2015/06/24/the-most-pointless-docker-command-ever/

cigzigwon commented 4 years ago

It will not work in a Docker Alpine box w/o procps installed first. Just fixed this today in my cloud.