EN VI

Python - Bash not working properly when running Docker as non-root?

2024-03-12 03:00:06
Python - Bash not working properly when running Docker as non-root

I want to run a docker with a bash, when i was running with basic configuration it ran as root and it showed the terminal properly. But when i moved to non-root user now the terminal just shows $ and tab is not working, also it does not matter if i move in different folders it just shows the $

I am using a docker-compose.yml file for automatization of the process

here are the two files

Dockerfile

FROM python:3.10

# Create non-root user
RUN groupadd -r dsteam && useradd -r -g dsteam -m dsteam

# Set the working directory
WORKDIR /home/dsteam/app/

# Copy and install requirements
COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Change directory ownership
RUN chown -R dsteam:dsteam /home/dsteam/app


# Expose the port
EXPOSE 5001

# Switch to non-root user
USER dsteam

# Copy the rest of the application code
COPY . .

# Run your command
# CMD [ "uvicorn", "src.server.app:app", "--host", "0.0.0.0", "--port", "80", "--reload" ]

docker-compose.yml

version: '3'

services:
  aiproject:
    build: .
    image: aiproject-image
    container_name: aiproject-container
    ports:
      - "80:80"
    volumes:
      - .:/app
    stdin_open: true
    tty: true
    # Override the default command with bash
    command: ["bash"]

Solution:

But when i moved to non-root user now the terminal just shows $ and tab is not working, also it does not matter if i move in different folders it just shows the $

That sounds like you're using the /bin/sh shell. Given that this issue only happens when running as the new user, it's probably getting run because that's the new user's default shell.

When I run your example, and inspect /etc/passwd, it looks like the user you've created has /bin/sh as their default shell.

dsteam:x:999:999::/home/dsteam:/bin/sh

I'd suggest passing the -s option to useradd to set Bash as your default shell.

Example:

RUN groupadd -r dsteam && useradd -r -g dsteam -m -s /bin/bash dsteam
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login