Docker Python ENV返回sh:页面未找到

rhfm7lfc  于 2023-10-16  发布在  Docker
关注(0)|答案(1)|浏览(110)

我正在提交我的文件模式和文件。如果需要,请通过。
我的问题是,当我尝试这个-> [docker-compose run --rm app sh -c "flake8"]时,它返回“sh: flake8: not found

requirements.txt

Django>=4.2.5
djangorestframework>=3.14.0

requirements.dev.txt

flake8>=6.1.0

docker-compose.yml

version: "3.9"

services:
  app:
    build:
      context: .
      args:
        - DEV=true
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"

Dockerfile

FROM python:alpine3.18
LABEL maintainer="Gazi"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./app /app
WORKDIR /app
EXPOSE 8000

ARG DEV=false
RUN python -m venv /py && \
    /py/bin/pip install --upgrade pip && \
    /py/bin/pip install -r /tmp/requirements.txt && \
    if [$DEV = "true"]; \
    then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
    fi && \
    rm -rf /tmp && \
    adduser \
    --disabled-password \ 
    --no-create-home \
    django-user

ENV PATH="/py/bin:$PATH"

USER django-user

.flake8应用内位置

[flake8]
exclude =
    migrations,
    __pycache__,
    manage.py,
    settings.py

当我尝试这个-> [docker-compose run --rm app sh -c "flake8"]时,它返回“sh: flake8: not found

如何解决这个问题?

uujelgoq

uujelgoq1#

我想你的shell脚本中有一个语法错误。请考虑以下示例:

$ DEV=false
$ if [$DEV = true]; then
> echo hello world
> fi
bash: [false: command not found

[是一个命令的名称;与其他命令一样,命令名和参数之间需要空格,参数之间也需要空格:

if [ $DEV = true ]; then ...

如果你像这样重写你的Dockerfile.

RUN python -m venv /py && \
    /py/bin/pip install --upgrade pip && \
    /py/bin/pip install -r /tmp/requirements.txt && \
    if [ $DEV = "true" ]; \
    then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
    fi && \
    rm -rf /tmp && \
    adduser \
    --disabled-password \ 
    --no-create-home \
    django-user

它可能会像你期望的那样工作。

相关问题