gpt4 book ai didi

python - Docker 容器构建失败 :/bin/sh: 1: flask: not found

转载 作者:行者123 更新时间:2023-11-28 17:04:23 30 4
gpt4 key购买 nike

我正在对一个 Flask 应用进行 docker 化。要在没有 docker 的情况下运行应用程序,我首先运行“export FLASK_APP=app”,然后运行“flask run”,因此我将其转移到 Dockerfile。

这是运行 docker-compose -f docker-compose.yml up --build 后的错误信息

Cannot start service app: b'OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"flask\": executable file not found in $PATH": unknown'

通过键入 docker-compose -f docker-compose.yml logs -f 检查日志后,我看到了这个错误,并且容器以代码 127 退出:

/bin/sh: 1: flask: not found

文件夹结构

tree
- app
- __init__.py
- Dockerfile
- docker-compose.yml

docker-compose.yml

version: '2' 

services:
app:
restart: always
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
volumes:
- .:/app
depends_on:
- redis

redis:
image: redis
command: redis-server
ports:
- '6379:6379'

Dockerfile

# Pull base image.
FROM ubuntu

# Install Supervisor.
RUN \
mkdir /var/log/celery && \
mkdir /home/ubuntu && \
apt-get update && \
apt-get install -y supervisor python-pip wget vim git && \
rm -rf /var/lib/apt/lists/* && \
sed -i 's/^\(\[supervisord\]\)$/\1\nnodaemon=true/' /etc/supervisor/supervisord.conf

# needs to be set else Celery gives an error (because docker runs commands inside container as root)
ENV C_FORCE_ROOT=1

# expose port 80 of the container (HTTP port, change to 443 for HTTPS)
EXPOSE 80

# Create virtualenv.
RUN \
pip2 install --upgrade pip && \
pip install --upgrade virtualenv && \
virtualenv -p /usr/bin/python2.7 /home/ubuntu/.virtualenvs

WORKDIR /home/ubuntu/celery-scheduler

ADD requirements.txt /home/ubuntu/tree/

# Install app requirements
RUN \
. /home/ubuntu/.virtualenvs/bin/activate && \
pip install -r requirements.txt

COPY . /home/ubuntu/tree

# Copy supervisor configs
RUN \
cp configs/supervisord.conf /etc/supervisor/supervisord.conf && \
cp configs/conf.d/*.conf /etc/supervisor/conf.d/

ENV FLASK_APP /home/ubuntu/tree/app/
ENV FLASK_DEBUG 1

CMD ["flask", "run", "--host", "0.0.0.0", "--port", "5000"]

app/__init__.py

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import os

db = SQLAlchemy()

def create_app():
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)

with app.app_context():
db.create_all()
return app

from app import models
app = create_app()
migrate = Migrate(app, db)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)

最佳答案

这里是这一行:

RUN \
. /home/ubuntu/.virtualenvs/bin/activate && \
pip install -r requirements.txt

只会为该特定命令激活您的 virtualenv

由于 virtualenv 激活实际上只归结为 PATH 操作,您可以通过 ENV

这应该有效:

ENV PATH=/home/ubuntu/.virtualenvs/bin:$PATH
RUN pip install -r requirements.txt

关于python - Docker 容器构建失败 :/bin/sh: 1: flask: not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026933/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com