Docker container build failed: /bin/sh: 1: flask: not found

Viewed 12990

I'm dockerizing a Flask app. To run the app without docker, i first run "export FLASK_APP=app", then "flask run", so I transferred that over to the Dockerfile.

This is the error message after running 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'

Upon checking the logs by typing docker-compose -f docker-compose.yml logs -f, I see this error and the container is exited with code 127:

/bin/sh: 1: flask: not found

Folder structure

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)
1 Answers

This line here:

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

Will only activate your virtualenv for that specific command

Since virtualenv activation really only boils down to a PATH manipulation, you can do that via ENV

This should work:

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