我正在将一个Flask应用程序Docker化。在不使用docker运行应用程序之前,我首先运行“export FLASK_APP=app”,然后运行“flask run”,因此我将其转移到Dockerfile中。
这是运行
这是运行
docker-compose -f docker-compose.yml up --build
后出现的错误消息:
通过键入无法启动服务app:b'OCI运行时创建失败: container_linux.go:348:容器进程启动导致"exec: \\\"flask\\\":在$PATH中找不到可执行文件":未知'
docker-compose -f docker-compose.yml logs -f
检查日志后,我看到此错误并且容器以代码127退出:
文件夹结构/bin/sh: 1: flask: 未找到
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)
python
作为入口点而不是flask。因此,这可能会解决问题。 - atakanyenel