My Dockerfile:
FROM python:3.9
WORKDIR /usr/src/app
RUN pip install python-dotenv
RUN pip install other_libraries...
During building container, message is displayed:
...
Step 3/4 : RUN pip install python-dotenv
---> Running in 5fffd3fe4042
Collecting python-dotenv
Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container 5fffd3fe4042
---> 2cd0942f520c
...
But when I run docker-compose exec container_name pip list there is no on list python-dotenv library.
I tried on python:3.9, python:3.8 with python-dotenv in 0.14 or 0.15 version.
Of course, when I run docker-compose exec container_name pip install python-dotenv everything is okey.
Why RUN command in Dockerfile not installed correctly?
Step by step.
Build image that including python libraries (using your Dockerfile)
docker build -t dotenv_image:1.0 .
List images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dotenv_image 1.0 507f2d0505a0 4 minutes ago 891MB
↑Okay it is here
Run container with sh shell to be able run commands and check what inside container
docker run --rm -it --entrypoint sh dotenv_image:1.0
Check libraries
pip freeze
python-dotenv==0.15.0
Library is here
Try to use library
python -c 'from dotenv import load_dotenv; print("ALL OK" if load_dotenv() else "CAN NOT LOAD")';
ALL OK
↑Positive output
I don't use docker-compose, but since your title says that your Dockerfile isn't working right, I can tell you that that's apparently not the problem. Your Dockerfile seems to work just fine:
>>> cat Dockerfile
FROM python:3.9
WORKDIR /usr/src/app
RUN pip install python-dotenv
>>> docker build -t so2 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM python:3.9
3.9: Pulling from library/python
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
0a5e725150a2: Pull complete
397dba5694db: Pull complete
b1d09d0eabcb: Pull complete
475299e7c7f3: Pull complete
d2fe14d8e6bc: Pull complete
Digest: sha256:429b2fd1f6657e4176d81815dc9e66477d74f8cbf986883c024c9b97f7d4d5a6
Status: Downloaded newer image for python:3.9
---> 5336a27a9b1f
Step 2/3 : WORKDIR /usr/src/app
---> Running in 37b03142a9b6
Removing intermediate container 37b03142a9b6
---> 4677ab34ce84
Step 3/3 : RUN pip install python-dotenv
---> Running in e89d17be1a32
Collecting python-dotenv
Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container e89d17be1a32
---> 55d00eeae4b4
Successfully built 55d00eeae4b4
Successfully tagged so2:latest
>>> docker run -it so2 bash
root#d211989c4bd7:/usr/src/app# pip list
Package Version
------------- -------
pip 20.2.4
python-dotenv 0.15.0
setuptools 50.3.2
wheel 0.35.1
root#d211989c4bd7:/usr/src/app# exit
>>> docker run -it so2
Python 3.9.0 (default, Oct 13 2020, 20:14:06)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dotenv import load_dotenv; load_dotenv()
True
>>>
>>> represent my prompt in my MacBook pro Terminal window.
Related
I am baffled by a strange issue that I am facing with docker-compose. Pip install is failing for certain packages within requirements.txt file.
docker version
Client:
Version: 18.09.9
API version: 1.39
Go version: go1.13.4
Git commit: 1752eb3
Built: Sat Nov 16 01:05:26 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.09.9
API version: 1.39 (minimum version 1.12)
Go version: go1.13.4
Git commit: 9552f2b
Built: Sat Nov 16 01:07:48 2019
OS/Arch: linux/amd64
Experimental: false
My docker-compose.yml file is:
version: "3.7"
services:
flask:
build: ./flask
container_name: flask
restart: always
environment:
- APP_NAME=MyFlaskApp
expose:
- 8080
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
The contents of Dockerfile inside ./flask directory is:
# Use the Python3.7.5 image
FROM python:3.7.5
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install the dependencies
RUN pip3 install -r requirements.txt
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
My requirements.txt file is (first few lines):
appdirs==1.4.3
apturl==0.5.2
asn1crypto==0.24.0
bcrypt==3.1.6
blinker==1.4
However, when I run docker-compose up command, it fails to install the second package in requirements.txt file.
Building flask
Step 1/6 : FROM python:3.7.5
---> fbf9f709ca9f
Step 2/6 : WORKDIR /app
---> Using cache
---> 39ab3ee34991
Step 3/6 : ADD . /app
---> Using cache
---> 8968809ff844
Step 4/6 : RUN python3 -m pip install --upgrade pip
---> Using cache
---> 15f717de5181
Step 5/6 : RUN pip3 install -r requirements.txt
---> Running in 7068f09498dc
Collecting appdirs==1.4.3
Downloading appdirs-1.4.3-py2.py3-none-any.whl (12 kB)
ERROR: Could not find a version that satisfies the requirement apturl==0.5.2 (from -r requirements.txt (line 2)) (from versions: none)
ERROR: No matching distribution found for apturl==0.5.2 (from -r requirements.txt (line 2))
ERROR: Service 'flask' failed to build: The command '/bin/sh -c pip3 install -r requirements.txt' returned a non-zero code: 1
I have tried a lot of possible options, but of no use. Some examples being:
Running pip upgrade inside the container.
Updating the DNS in docker as per the thread here.
as #WilliamD.Irons already pointed here apturl is a package, you can use :
RUN apt install apturl
RUN pip3 install -r requirements.txt
and remove apturl from your requirements.txt
apturl seems to be a client side Ubuntu program for adding support for links like click within a webpage:
The apturl is a graphical mini-program for installing packages from the repository that a user has. It is pre-installed on Ubuntu since version 7.10, and the Firefox and Pidgin programs come with support for it.
Additionally, there are other suggestions that support for this doesn't exisit outside Ubuntu. As the official python:3.7.5 image is based on Debian GNU/Linux this wouldn't be available anyway.
I'd question why this needs to be in the requirements for your Flask app, as you should just be able to write your own python function to generate compatible links within the app. Anyone hitting these links from a supported client (Any Ubuntu box with Firefox according to the above) should be able to successfully process those links.
If using a base container isn't an issue, try the Ubuntu environment and install python.
apturl is provided by ubuntu, specifically as it uses apt
# Use the ubuntu
FROM ubuntu:lts
# Set the working directory to /app
WORKDIR /app
# install python and pip, and delete cache
RUN apt update && apt install -y python3 python3-pip && rm -rf /var/lib/apt/lists/*
# Copy the current directory contents into the container at /app
ADD . /app
# Install the dependencies
RUN pip3 install -r requirements.txt
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
I am trying to host a simple Dash app on a Raspberry Pi in a Docker environment. The app runs as expected locally, but when building for ARMv6 and pulling the image, I get the following error (shortened for brevity):
ImportError: Unable to import required dependencies:
numpy:
[...]
Original error was: No module named 'numpy.core._multiarray_umath'
I build the app for ARMv6 and push it to my DockerHub repository using the following command:
docker buildx build --platform linux/arm/v6 -t <username>/<repo-name>:<tag> --push .
The image is then downloaded using docker-compose pull into the Docker environment on the Raspbery Pi and started using docker-compose up
My Dockerfile contains the following:
FROM python:3.9
WORKDIR /app
COPY requirements.txt requirements.txt
# Problems with brotli from piwheels means it needs to be downloaded from another repo.
RUN pip3 install --extra-index-url https://www.piwheels.org/simple --no-cache-dir -r requirements.txt \
&& pip3 uninstall -y brotli \
&& pip3 install --no-cache-dir brotli
COPY . .
CMD [ "python", "app.py" ]
Requirements.txt:
numpy==1.21.4
pandas==1.3.5
dash==2.0.0
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-bootstrap-components==1.0.2
dash-table==5.0.0
plotly==5.3.1
mysql-connector-python==8.0.27
dash-bootstrap-templates==1.0.4
I am trying to dockerize my python application. Errors are showing inside building Dockerfile and installing dependencies of scikit-learn ie. numpy.
Dockerfile
FROM python:alpine3.8
RUN apk update
RUN apk --no-cache add linux-headers gcc g++
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5001
ENTRYPOINT [ "python" ]
CMD [ "main.py" ]
requirements.txt
scikit-learn==0.23.2
pandas==1.1.3
Flask==1.1.2
ERROR: Could not find a version that satisfies the requirement setuptools (from versions: none)
ERROR: No matching distribution found for setuptools
Full Error
Agree with #senderle comment, Alpine is not the best choice here especially if you plan to use scientific Python packages that relies on numpy. If you absolutely need to use Alpine, you should have a look to other questions like Installing numpy on Docker Alpine.
Here is a suggestion, I've also replaced the ENTRYPOINT by CMD in order to be able to overwrite to ease debugging (for example to run a shell). If the ENTRYPOINT is python it will be not be possible to overwrite it and you will not be able to run anything other than python commands.
FROM python:3.8-slim
COPY . /app
WORKDIR /app
RUN pip install --quiet --no-cache-dir -r requirements.txt
EXPOSE 5001
CMD ["python", "main.py"]
Build, run, debug.
# build
$ docker build --rm -t my-app .
# run
docker run -it --rm my-app
# This is a test
# debug
$ docker run -it --rm my-app pip list
# Package Version
# --------------- -------
# click 7.1.2
# Flask 1.1.2
# itsdangerous 1.1.0
# Jinja2 2.11.2
# joblib 0.17.0
# MarkupSafe 1.1.1
# numpy 1.19.2
# pandas 1.1.3
# ...
This question already has answers here:
Activate python virtualenv in Dockerfile
(6 answers)
Closed 2 years ago.
I am trying to setup a python virtual environment on a docker image running a docker build
The terminal output is ok when I run docker build .. but when I login into my container, no packages are installer in my virtual environment.
#Dockerfile
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3.8 get-pip.py
RUN pip install virtualenv
RUN virtualenv venv
RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt
...
# Docker build command
docker build --no-cache -t auto-server:1.0 .
# Terminal output
Step 27/27 : RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt
---> Running in d27dbb9a4c97
Collecting asgiref==3.2.10
Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting beautifulsoup4==4.9.1
Downloading beautifulsoup4-4.9.1-py3-none-any.whl (115 kB)
Collecting Django==3.1.1
Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)
...
Successfully installed Django-3.1.1 asgiref-3.2.10 beautifulsoup4-4.9.1 fake-useragent-0.1.11 joblib-0.16.0 numpy-1.19.2 pandas-1.1.2 python-dateutil-2.8.1 pytz-2020.1 scikit-learn-0.23.2 scipy-1.5.2 six-1.15.0 sklearn-0.0 soupsieve-2.0.1 sqlparse-0.3.1 threadpoolctl-2.1.0
Here is what I get when I list pakages in my virtual environment:
$ docker exec -ti auto-server bash
root#9c1f914d1b7b:/home/ubuntu# source venv/bin/activate
(venv) root#9c1f914d1b7b:/home/ubuntu# pip list
Package Version
---------- -------
pip 20.2.2
setuptools 49.6.0
wheel 0.35.1
WARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.
You should consider upgrading via the '/home/ubuntu/venv/bin/python -m pip install --upgrade pip' command.
Is it the right way to do it? How to make sure packages will be installed?
Finally having a virtual environment is not mandatory in a container development environment, I just setup image python environment as desired.
# Dockerfile
...
RUN python3.8 get-pip.py
RUN pip install -r auto/requirements.txt
This is not exactly what I was looking for but it does the job.
I use simple Dockerfile for dev mode
# pull official base image
FROM python:3.6-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
Im trying to use pyzbar 0.1.4 on a Flask Server in Docker
The image was created by us, based in python 2.7 taken from alpine.
Install ZBar by
apk update
apk add zbar
Im getting the following error when running dockerfile
File "/usr/lib/python2.7/site-packages/pyzbar/pyzbar.py", line 8, in <module>
from .wrapper import (
File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 166, in <module>
c_uint_p, # minor
File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 159, in zbar_function
return prototype((fname, load_libzbar()))
File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 135, in load_libzbar
raise ImportError('Unable to find zbar shared library')
ImportError: Unable to find zbar shared library
Im trying to decode a QR image using that library
Dockerfile
FROM buffetcontainerimages.azurecr.io/base/buffetcloud-python:0.1
RUN pip install --upgrade pip setuptools wheel
COPY wheeldir /opt/app/wheeldir
COPY *requirements.txt /opt/app/src/
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/requirements.txt
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/test-requirements.txt
COPY . /opt/app/src/
WORKDIR /opt/app/src
RUN python setup.py install
EXPOSE 5000
CMD dronedemo
And requirements.txt
requests>=2.18.4
flask>=0.12.2
mechanize>=0.3.6
regex>=2.4.136
PyPDF2>=1.26.0
bs4>=4.5.3
pyzbar>=0.1.4
openpyxl>=2.5.0
selenium>=3.9.0
matplotlib>=2.1.2
When pip install zbar
pip install zbar
Collecting zbar
Downloading zbar-0.10.tar.bz2
...
zbarmodule.h:26:18: fatal error: zbar.h: No such file or directory
#include <zbar.h>
compilation terminated.
error: command 'gcc' failed with exit status 1
In Ubuntu install zbar-tools
sudo apt-get install zbar-tools
A simple test, looks good.
FROM python:2.7
RUN apt-get update && \
apt-get install -y build-essential libzbar-dev && \
pip install zbar
i tried alpine.. but the zbar lib is only available in the edge branch -- trying to get it to work was more trouble than it was worth.
PS. beware of images that are not in the docker repo. -- didnt know it was your image
Working example:
$ docker build -t yourimagenamehere .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM python:2.7
---> 9e92c8430ba0
... trunc...
Successfully built d951cd32ea74
Successfully tagged yourimagenamehere:latest
$ docker run -it --rm yourimagenamehere
Python 2.7.14 (default, Dec 12 2017, 16:55:09)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zbar
>>>
In Ubuntu terminal simply run this command and this will install zbar in your global package
sudo apt-get install zbar-tools
I encountered the same issue (happy to have found this thread). Not sure if this has already been solved but this might help you or future devs.
As usual, it worked on my machine locally but couldn't get it to work in a container
What I tried initially:
Building an image based on a Python3 image
What solved the issue:
Build with FROM ubuntu:18.04
Within Ubuntu I was able to install the zbar shared library. According to https://pypi.org/project/pyzbar/ we need sudo apt-get install libzbar0
Set LC_ALL & LANG ENV variables (not sure why, it was provided in an additional error)
Within requirements.txt downgrade Pillow==8.4.0 to Pillow==6.2.2
My Dockerfile:
FROM ubuntu:18.04
RUN apt-get update -y
# Get's shared library for zbar
RUN apt-get install -y libzbar0
# Installs Python
RUN apt-get install -y python3-pip python3-dev build-essential
COPY . /app
WORKDIR /app
COPY requirements.txt .
RUN pip3 install -r requirements.txt
# Initially encountered an issue that indicated I had to set these ENVs
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
And requirements.txt
fastapi==0.67.0
Pillow==6.2.2
pyzbar==0.1.8
urllib3==1.26.7
uvicorn==0.12.2
I grea with the second commend on your post, but you might want to try to install the pyzbar dependency through pip.
FROM buffetcontainerimages.azurecr.io/base/buffetcloud-python:0.1
RUN pip install --upgrade pip setuptools wheel pyzbar
COPY wheeldir /opt/app/wheeldir
COPY *requirements.txt /opt/app/src/
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/requirements.txt
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/test-requirements.txt
RUN pip install -y pyzbar
COPY . /opt/app/src/
WORKDIR /opt/app/src
RUN python setup.py install
EXPOSE 5000
CMD dronedemo