How to fix import error with modules on python docker - python

i'm creating an app and wanted to dockerize it. But after the build, when i do sudo docker-compose up i get the error module cogs not found at the line from cogs import FILES in my main file (josix.py).
I already checked several topics on StackOverflow about import errors with docker but none of them got me the right answer so i try with this one.
The code looks like this :
/app
josix.py
cogs/
init.py (containing FILES variable)
others py files
(init.py and other py files are inside cogs directory)
Dockerfile :
FROM python:3
WORKDIR /app
COPY Josix/* ./
RUN pip install --no-cache-dir -r requirements.txt
ENV PYTHONPATH "${PYTHONPATH}:/app/"
CMD [ "python3", "josix.py" ]
I tried to change the pythonpath, add several ones, change the import

Remove the * from your copy command to copy sub-directories as well as files (and you don't need to add to PYTHONPATH /app is already in it).
FROM python:3
WORKDIR /app
COPY Josix/ ./
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python3", "josix.py" ]

Related

how to correctly copy requirements.txt for docker file

This is how my current folder structure looks like:
I am present in the FinTechExplained_Python_Docker folder. My Dockerfile looks like this:
FROM python:3.8-slim-buster
WORKDIR /src
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "main.py"]
However, when I run this command
docker build --tag FinTechExplained_Python_Docker .
I get this error
ERROR [3/5] COPY requirements.txt requirements.txt 0.0s
------
> [3/5] COPY requirements.txt requirements.txt:
------
failed to compute cache key: "/requirements.txt" not found: not found
What am I doing wrong?
Edit:
I also tried changing it to:
COPY str/requirements.txt requirements.txt:
but then I would still get the error that:
failed to compute cache key: "/src/requirements.txt" not found: not found
maybe the second COPY statement is also to be changed but not sure how
You need to specify the source of your COPY statements relative to the build context, like this
FROM python:3.8-slim-buster
WORKDIR /src
COPY src/requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ .
CMD [ "python", "main.py"]
When building image from Dockerfile it searches file from directory where Dockerfile is located (FinTechExplained_Python_Docker in your case).
So basically requirements located at FinTechExplained_Python_Docker/src/requirements.txt, but docker searches them at FinTechExplained_Python_Docker/requirements.txt.
To fix this you have to change 5th line to:
COPY src/requirements.txt requirements.txt
Oh, I think I got why you are having failed to compute cache key....
When you are copying file in Dockerfile using COPY you have to pass Directory where file should be saved as second argument.
Like COPY LOCAL_PATH_TO_FILE SERVER_PATH_TO_DIRECTORY
In your case:
FROM python:3.8-slim-buster
WORKDIR /src
COPY src/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ .
CMD [ "python", "main.py"]

Docker CMD not executing, program not running

So I have a python project for which I have a Dockerfile, but the problem is for some magical reason when I build the docker image and then run it I don't see the result of the code I would see if I just ran it normally on pycharm or on the console, anyone knows why this happens? Its so odd.
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
COPY . /app
RUN pip3 install -r requirements.txt
CMD ["python3","main.py"]
The requirements.txt file is where I store all the packages that need to be installed in order to my program work.
You copy your application to wrong path.
Since you use WORKDIR you need to know (from the formal site):
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile
This means that your COPY . /app copy your application into /app/app, but when you write CMD ["python3","main.py"] this command will execute in /app. I believe the main.py file is not there, but in /app/app.
To fix it you need to change COPY . /app to COPY . .

"No such file or directory" when building Docker image

I have an existing and working Dockerfile, that I want to update to have a better structure, and to allow me to volume-mount part of the application directory to enable persistence.
Folder structure is:
.git/
.gitignore
Dockerfile
README.md
Sorter.py
WebService.py
requirements.txt
The current working Dockerfile is:
FROM python:3.8-alpine
ADD . /
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "python", "./WebService.py" ]
I now want to simply do this not in the root directory, so I do the following:
FROM python:3.8-alpine
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["WebService.py"]
I build the image "docker build -t , and run it using the same docker-compose file as for the first buildscript - and I get the error:
"python: can't open file '/app/WebService.py': [Errno 2] No such file or directory"
The problem is with this line WORKDIR /app, as you can see the Docker tries to run the file /app/WebService.py, but you do not have it. Your file is in . not in app folder. So either you should create the app folder and put your WebService.py in that folder, either just remove WORKDIR /app.
Here's the solution I found:
FROM python:3.8-alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "python", "./WebService.py" ]

How to install local packages using pip as part of a docker build?

I've got a package that I want to build into a docker image which depends on an adjacent package on my system.
My requirements.txt looks something like this:
-e ../other_module
numpy==1.0.0
flask==0.12.5
When I call pip install -r requirements.txt in a virtualenv this works fine. However, if I call this in a Dockerfile, e.g.:
ADD requirements.txt /app
RUN pip install -r requirements.txt
and run using docker build . I get an error saying the following:
../other_module should either be a path to a local project or a VCS url beginning with svn+, git+, hg+, or bzr+
What, if anything, am I doing wrong here?
First of all, you need to add other_module to your Docker image. Without that, the pip install command will not be able to find it. However you cant ADD a directory that is outside the directory of the Dockerfile according to the documentation:
The path must be inside the context of the build; you cannot ADD
../something /something, because the first step of a docker build is
to send the context directory (and subdirectories) to the docker
daemon.
So you have to move the other_module directory into the same directory as your Dockerfile, i.e. your structure should look something like
.
├── Dockerfile
├── requirements.txt
├── other_module
| ├── modue_file.xyz
| └── another_module_file.xyz
then add the following to the dockerfile:
ADD /other_module /other_module
ADD requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
The WORKDIR command moves you into /app so the next step, RUN pip install... will be executed inside the /app directory. And from the app-directory, you now have the directory../other_module avaliable

Can't build Dockerfile -: Not a directory Error using ADD command

I am trying to make a dockerfile for a python/flask webapp and keep running into issues inspite of multiple changes based off what I've read
The Dockerfile I have at present is as follows:
FROM ubuntu:latest
#Update OS
RUN sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y upgrade
# Install Python
RUN apt-get install -y python-dev python-pip
# Add requirements.txt
ADD requirements.txt /webapp
ADD requirements.txt .
# Install uwsgi Python web server
RUN pip install uwsgi
# Install app requirements
RUN pip install -r requirements.txt
# Create app directory
ADD . /webapp
# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp
# Expose port 8000 for uwsgi
EXPOSE 8000
ENTRYPOINT ["uwsgi", "--http", "127.0.0.1:8000", "--module", "app:app", "--processes", "1", "--threads", "8"]
#ENTRYPOINT ["python"]
CMD ["app.py"]
Attempting to run this Dockerfile with the command sudo docker build -t imgcomparer .
gives the error:
Step 10/15 : ADD . /webapp
Error processing tar file(exit status 1): Error setting up pivot dir: mkdir /var/lib/docker/aufs/mnt/53420471c832e61b7f75ac5fc5268d64b932a4d589a8464c63bf5868f127ff04/webapp/.pivot_root981494252: not a directory
After some research, I discovered that putting a trailing / at the end of the path would work (see this question and this one)
Upon doing that (and the same on the following lines) I have the following in my dockerfile:
# Create app directory
ADD . /webapp/
# Set the default directory for our environment
ENV HOME /webapp/
WORKDIR /webapp/
that gives this error:
Step 10/15 : ADD . /webapp/
stat /var/lib/docker/aufs/mnt/f37b19a8d72d39cbbdfb0bae6359aee499fab0515e2415e251a50d528708bdd3/webapp/: not a directory
Last, I tried removing the problematic line altogether. When I have
# Create app directory
# ADD . /webapp
# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp
The docker file successfully builds! But, unsurprisingly, trying to run it gives an error:
sudo docker run -t imgcomparer
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "chdir to cwd (\"/webapp\") set in config.json failed: not a directory"
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
Directory Structure is as follows
app.py
image_data.db
README.txt
requirements.txt
Dockerfile
templates
- index.html
static/
- image.js
- main.css
img/
- camera.png
images/
- empty
I believe you have to create the directory before referencing to it:
RUN mkdir /webapp
edit:
(before ADD requirements.txt /webapp)
With
ADD somefile.ext /folder
(without trailing slash to the folder) you reference a file, so you get a file named folder at the root directory, with the contents of somefile.ext in it.
Be careful when you need to reference a directory and when a file.
Thus you could also:
ADD requirements.txt /webapp/
Besides: why do you add requirements.txt twice? You should aim for a little steps as possible in a Dockerfile, so you could do:
[...]
RUN apt-get install -y python-dev python-pip && \
pip install uwsgi
ADD . /webapp/
RUN pip install -r /webapp/requirements.txt
[...]

Categories

Resources