简体   繁体   中英

Dockerfile Run Python Pipenv Build Fails

Learning how to dockerize applications and I ran into a snag where my Dockerfile build fails outputting:

app_1  | Traceback (most recent call last):
app_1  |   File "app.py", line 4, in <module>
app_1  |     from flask import Flask, render_template, request, json
app_1  | ModuleNotFoundError: No module named 'flask'
bucketlist-flask-mysql_app_1 exited with code 1

The directory structure in the container is:

/app

/app -> app.py Pipfile.lock

The directory in the repository is

Dockerfile docker-compose.yml /src

/src -> Pipfile Pipfile.lock sql_scripts/ FlaskApp/app.py

The Dockerfile is:

# alpine with py3.8 - reqd version of python for pipenv
from python:3.8-alpine

#EXPOSE port, default is 5000, app uses 5000
EXPOSE 5000

# create a directory to run the app in
WORKDIR /app

# install pip system-wide
RUN pip install pipenv

#move the files into /app
COPY src/Pipfile.lock /app

# add the application files
COPY src/FlaskApp /app

# run the application at launch
RUN pipenv install --ignore-pipfile
CMD ["pipenv", "run", "python3", "app.py"]

and the docker-compose is:

version: "3"
services:
    app:
        build: .
        links:
            - db
        ports:
            - "5000:5000"
    db:
        image: mariadb
        restart: always
        ports:
            - "32000:3306"
        environment:
            MYSQL_ROOT_PASSWORD: root
        volumes:
            - ./src/sql_scripts:/docker-entry-point-initdb.d/:ro

I've done quite a bit of iteration and troubleshooting. If I run start a python:3.8-alpine container and manually copy over Pipfile.lock and app.py I can run in sh:

pip install pipenv
pipenv install --ignore-pipfile
pipenv run python3 app.py

When ran from sh manually the application builds and runs perfectly, my best conclusion currently is that the processes may be running concurrently and not allowing the pipenv install to finish executing?

For reference, I believe the python is just fine but the first 4 lines of app.py are:

#!/bin/bash
"exec" "pipenv" "run" "python3" "$(pwd | sed 's?src.*?src/FlaskApp/app.py?g')"
#START: app.py code \/
from flask import Flask, render_template, request, jsom

Solution, After a lot of troubleshooting I solved the problem by purging my docker containers, the real solution is after a change in your docker files, you need to run docker-compose down before running docker-compose up . I assumed when shutting down containers this process was involved and didnt know the docker-compose down command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM