以非root身份运行Docker时Bash无法正常工作
我想用 bash 运行一个 docker,当我用基本配置运行时,它是以 root 用户身份运行的,终端显示正常。但是当我切换到非 root 用户后,终端只显示 $,而且 Tab 键也不工作。不管我切换到哪个文件夹,终端都只显示 $。
我正在使用一个 docker-compose.yml 文件来自动化这个过程。
以下是这两个文件:
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"]
1 个回答
1
但是当我切换到非根用户时,终端只显示 $,而且 Tab 键也不工作,不管我换到哪个文件夹,它都只显示 $。
听起来你正在使用 /bin/sh
这个命令行工具。因为这个问题只在你作为新用户时出现,所以很可能是因为这是新用户的默认命令行工具。
当我运行你的例子并查看 /etc/passwd
文件时,发现你创建的用户的默认命令行工具是 /bin/sh
。
dsteam:x:999:999::/home/dsteam:/bin/sh
我建议你在添加用户时使用 -s
选项,这样可以把 Bash 设置为你的默认命令行工具。
示例:
RUN groupadd -r dsteam && useradd -r -g dsteam -m -s /bin/bash dsteam