0

I've docker container running on a machine, in order to go inside the container I used command -

docker exec -it <container-id> bash

but when I run whoami here it returns root

But I want to perform actions with different user hence to login inside container with different user.

How can I login with different user inside container?

Alpha
  • 13,320
  • 27
  • 96
  • 163
  • 2
    Does this answer your question? [Connect to docker container as user other than root](https://stackoverflow.com/questions/35734474/connect-to-docker-container-as-user-other-than-root) – Dornaut Oct 21 '20 at 10:25
  • Usually a container runs only a single process, so there's only a single user ID that matters, and an interactive shell is just for debugging. Can you provide a [mcve] of application code that needs this functionality? – David Maze Oct 21 '20 at 12:56

2 Answers2

1
docker exec -it --user <user> <container-id> /bin/sh

or

docker exec -it --user <user> <container-id> /bin/bash

but this also depends on the docker you are running, for some dockers you will also have to add your user in the passwd file or you will get this error

docker: Error response from daemon: unable to find user <user>: no matching entries in passwd file.
Andrei Stoicescu
  • 659
  • 1
  • 6
  • 11
1

After logging in as root, To login as a different user:

su <username>

You can create a new user by:

useradd <username>

To create a user with a home directory in /home:

useradd -m <username>

And create a password by using:

passwd <username>
Austin Jerry
  • 111
  • 4