I'm getting the following error when executing
docker-compose up --build
web_1 | Traceback (most recent call last):
web_1 | File "glm-plotter.py", line 4, in <module>
web_1 | from flask import Flask, render_template, request, session
web_1 | ModuleNotFoundError: No module named 'flask'
glm-plotter_web_1 exited with code 1
I tried changing "Flask" to "flask" in the requirements.txt
Dockerfile
FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential
COPY requirements.txt /
RUN pip3 install --trusted-host pypi.python.org -r /requirements.txt
ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD ["python3", "glm-plotter.py"]
docker-compose.yml
version: "3"
services:
web:
volumes:
- ~/.aws:/root/.aws
build: .
ports:
- "5000:5000"
requirements.txt
click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
numpy==1.11.1
pandas==0.18.1
python-dateutil==2.5.3
pytz==2016.4
six==1.10.0
Werkzeug==0.11.10
glm-plotter.py
from flask import Flask, render_template, request, session
import os, json
import GLMparser
...
I created a Docker and it compiles fine. You might want to adapt it to your personal needs and add the last few lines from your Dockerfile above:
FROM library/python:3.6-stretch
COPY requirements.txt /
RUN pip install -r /requirements.txt
Please note that in the library/python images no explicit version number for python or pip is required since there is only one installed.
If you use miniconda image you have to create a new environment and activate it prior to installing the packages and run the program in your docker file. Something like:
FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential
COPY requirements.txt /
RUN ["conda", "create", "-n", "myenv", "python=3.4"]
RUN /bin/bash -c "source activate myenv && pip install --trusted-host pypi.python.org -r /requirements.txt"
ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD /bin/bash -c "source activate myenv && python glm-plotter.py"
Related
unfortunately I'm a bit desperate.
I have created a dockerimage and am running everything with docker-compose.
If i run docker-compose up i get this error:
| django.core.exceptions.ImproperlyConfigured: 'mssql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
web_1 | 'mysql', 'oracle', 'postgresql', 'sqlite3'
if i see in the pip list, i see to less packages.
:(
docker-compose run web pip list
Package Version
---------- -------
asgiref 3.5.2
Django 4.0.4
pip 22.0.4
psycopg2 2.9.3
setuptools 58.1.0
sqlparse 0.4.2
wheel 0.37.1
´´´
Dockerfile
FROM ubuntu:20.04
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN apt update -y && apt upgrade -< && apt-get update
RUN apt-get install -y pip curl git python3-pip openjdk-8-jdk unixodbc-dev
#RUN pip install --upgrade pip
RUN pip install -r requirements.txt
#ADD SQL SERVER ODBC Driver 17 for Ubuntu 20.04
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated msodbcsql17
RUN ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated mssql-tools
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
COPY . /code/
´´´
requirements.txt
Django>=4.0
psycopg2>=2.8
django-mssql==1.8
djangorestframework==3.13.1
pymssql==2.2.3
pyodbc==4.0.32
pyparsing==3.0.4
setuptools==61.2.0
sqlparse==0.4.1
´´´
docker-compose.yml
version: "3"
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
docker-compose up build -d
This builds the compose new. Then i get easy errors to fix. Just change some lines in Dockerfile.
Also change in requirements django-mssql into mssql-django
I have two similar dockerfiles. They differ only in the entrypoint
This is Dockerfile-cron
FROM python:3.8
WORKDIR /src
COPY requirements.txt ./
COPY requirements-dev.txt ./
COPY . ./
RUN apt-get update -y
RUN apt-get install libgl1-mesa-glx -y
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements-dev.txt
RUN chmod +x ./cron.sh
CMD ./cron.sh
This is cron.sh
#!/usr/bin/env bash
* * * * * python manage.py check_subscriptions > /proc/1/fd/1 2>/proc/1/fd/2
I have cron service in docker-compose
cron:
build:
dockerfile: deploy/Dockerfile-cron
context: .
networks:
- sortif-network
When I up docker-compose I get error
cron_1 | ./cron.sh: line 2: Invoice # 1 (7).pdf: command not found
I am trying to install rabbitmq (pika) driver in my python container, but in local deployment, there is no problem.
FROM ubuntu:20.04
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN apt-get update && apt-get -y install gcc python3.7 python3-pip
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python","index.py"]
this is my requerments.txt file :
requests
telethon
Flask
flask-mongoengine
Flask_JWT_Extended
Flask_Bcrypt
flask-restful
flask-cors
jsonschema
werkzeug
pandas
xlrd
Kanpai
pika
Flask-APScheduler
docker build steps complete with no error and install all the dependencies with no error but when I try to run my container it crashes with this error :
no module named 'pika'
installing python3.7 will not work here, you are still using python3.8 by using pip3 command and your CMD will also start python3.8, I suggest you to use python:3.7 base image
so try this:
FROM python:3.7
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN apt-get update && apt-get -y install gcc
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
CMD ["python","index.py"]
How can I install Numpy and tensorflow inside a Docker image?
I'm trying to create a image of this simple Flask app:
import numpy as np
import tensorflow as tf
from flask import Flask, jsonify
print(tf.__version__)
with open('../assets/model/v1/model_architecture_V1.json', 'r') as f:
model_json = f.read()
model = tf.keras.models.model_from_json(model_json)
model.load_weights("../assets/model/v1/model_weight_V1.h5")
app = Flask(__name__)
#app.route("/api/v1", methods=["GET"])
def getPrediction():
prediction = model.predict()
return jsonify({"Fe": 3123 })
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000, debug=True)
This is my DockerFile
FROM alpine:3.10
RUN apk add --no-cache python3-dev \
&& pip3 install --upgrade pip
WORKDIR /app
COPY . /app
RUN pip3 --no-cache-dir install -r requirements.txt
CMD ["python3","src/app.py"]
And this is my requirements.txt:
Flask==1.1.2
numpy==1.18.1
tensorflow==2.0.0
When I build the image, Docker throws an error that says tensorflow and numpy cant be found.
Error:
The issue here seems to be missing libraries to build the packages from .whl files.
When creating Docker images for python which includes heavy libraries like tensorflow, I would suggest you to use the official Debian images.
Please see below Dockerfile using Debian-Buster:
FROM python:3.7.5-buster
RUN echo \
&& apt-get update \
&& apt-get --yes install apt-file \
&& apt-file update
RUN echo \
&& apt-get --yes install build-essential
ARG USER=nobody
RUN usermod -aG sudo $USER
RUN pip3 install --upgrade pip
COPY . /app
WORKDIR /app
RUN pip3 --no-cache-dir install -r requirements.txt
USER $USER
# Using 4000 here as you used 4000 in the code. Flask has a default of 5000
EXPOSE 4000
ENTRYPOINT ["python"]
CMD ["app/app.py"]
I used below commands to build and run the docker image, and got the result at http://0.0.0.0:4000/api/v1
docker build -t tfdocker:v1 .
docker run -p 4000:4000 -t tfdocker:v1
For reference:
This was my directory structure:
├── Dockerfile
├── app
│ └── app.py
└── requirements.txt
1 directory, 3 files
Content of requirements.txt were:
Flask==1.1.2
numpy==1.18.4
tensorflow==2.2.0
Hope this helps!
When I run
docker-compose up --build
I get the following error:
web_1 | /opt/conda/bin/python3: can't find '__main__' module in 'glm-plotter'
glm-plotter/glm-plotter.py:
...
if __name__ == "__main__":
app.secret_key = 'B0er23j/4yX R~XHH!jmN]LWX/,?Rh'
app.run()
Dockerfile
FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y \
libpq-dev \
build-essential
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
ADD . /code
WORKDIR /code
RUN pip3 install -r requirements.txt
RUN cd glm-plotter
RUN ls glm-plotter
CMD ["python3", "glm-plotter"]
If glm-plotter refers to this repository, then according to its documentation you should run
python glm-plotter.py.
Accordingly, you should change your Dockerfile to:
CMD ["python3", "glm-plotter.py"]