Docker: unix “who” command doesn't work inside

2019-05-30 22:49发布

问题:

I have a Docker image that has one non-root user created named builder.

The application that supposed to run inside the container uses unix who command.

For some reason it returns empty string inside the container:

builder@2dc3831c558b:~$ who
builder@2dc3831c558b:~$

I cannot use whoami because of implementation details.

(I'm using Docker 1.6.2 on Debian Jessie)

EDIT (additional details regarding why I use "who"):

I use the command who with the parameters am i, that is who am i. This suppose to return the user who first made the login. So, for example, sudo who am i returns builder, while sudo whoami returns root.

回答1:

The command who includes options like -b: time of last system boot.

Since all commands from a container translates into system calls to the kernel, that would not return anything container related, but docker-host related (ie the underlying host).

See also "Difference between who and whoami commands": whoami prints effective username of being ran whoami, which is not the same as who (printing information about users who are currently logged in).

The current workarounds listed in issue 18547 are:

The registry configuration is stored in the client, so something as simple as cat ~/.docker/config.json will give you the answer you're looking for.

docker info | grep Username should give you this information.

But that is not the same as running the command from within a container session. id -u might be closer.

By default, there is no direct loggin when a container is started by the docker daemon.
As Auzias commented, only a direct ssh connection (initiating a login session) would allow who to return anything. But with docker, this is generally not needed since docker exec (for debug purposes) exists (and spare the image maintainer to include ssh unless it is really needed).



标签: unix docker