Docker: unable to acces other files in directory - docker

I'm new to docker and I'm trying to create a docker image in which a python 2.7 script runs. This file imports functions and classes from other files in the same directory. Building the docker image already works, but running it gives an error that the imported files in the python 2.7 script don't exist.
This is the dockerfile I'm using
FROM python:2.7
WORKDIR C:\something\something\print
RUN apt-get update
RUN apt-get install python-cairo
RUN apt-get install python-gi
RUN apt-get install python-gi-cairo
RUN apt-get install -y gir1.2-gtk-3.0
RUN apt-get install -y mapnik-utils
RUN pip install bs4
RUN pip install image
RUN pip install mapnik
RUN pip install numpy
RUN pip install psycopg2
COPY test_server.py ./
CMD [ "python", "./test_server.py"]
And this is the error I receive after I do "docker run"
Traceback (most recent call last):
File "./test_server.py", line 13, in <module>
from utils import client, reprojector, formats
ImportError: No module named utils
The module named utils is a folder in the WORKDIR I specified in the dockerfile, but I don't understand why it can't be accessed. What am I doing wrong here?

Related

importerror: libgl.so.1: cannot open shared object file: no such file or directory (PyQt5)

I am trying to run a simple PyQt5 application on a docker container. But when I am running my docker compose file I am getting the following error:
from pyqt5.qtwidgets import * importerror: libgl.so.1: cannot open shared object file: no such file or directory.
Can someone help me fix this problem?
Dockerfile
FROM ubuntu:latest
# Preparing work environment
ADD server.py .
ADD hinto.py .
RUN apt-get update
RUN apt-get upgrade
RUN apt-get -y install python3
RUN apt-get -y install python3-pip
# Preparing work environment
RUN pip3 install PyQt5
as you can see I am installing the PyQt5 package so I do not understand where it got wrong.
pip isn't intended for installing native dependencies (like libgl, which is a C library, not a Python library). On Ubuntu, managing native dependencies is apt-get's job.
Don't use pip to install software your distro has its own packages for. In this case, that looks like:
apt-get -y install python3-pyqt5
As you can see at https://packages.ubuntu.com/jammy/python3-pyqt5, this lists libqt5gui5 as a dependency, which in turn depends on libgl1.

Is it possible create a base image from a file?

repo with a few services and in each service, I have the following base code:
FROM python:3.8.13-slim-bullseye
WORKDIR /usr/app
RUN apt-get update
RUN apt-get install default-libmysqlclient-dev build-essential -y
RUN python -m pip install --upgrade pip
RUN pip install pipenv setuptools
This is a little slow to rebuild each time, and sometimes I need to drop all images, so the idea is to know if is possible, to create Dockerfile as a base image and import this from another docker file in order to build these steps only one time locally.
Thanks

Having trouble with my Dockerfile and setting correct working directory

This is my first Dockerfile and this is what I have so far.
FROM python:3.6-stretch
# install build utilities
RUN apt-get update && \
apt-get install -y gcc make apt-transport-https ca-certificates build-essential
# check our python environment
RUN python3 --version
RUN pip3 --version
# set the working directory for containers
WORKDIR /usr/src/toxic-content-monitoring
# Installing python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy all the files from the project’s root to the working directory
COPY src/ /src/
RUN ls -la /src/*
# Running Python Application
CMD ["python3", "/src/main.py"]
But when I am trying to run the docker image the files inside the data folder cannot be found.
Here is a picture of the my project structure.
Changed it to what was suggested and still getting an error.
FROM python:3.6-stretch
# install build utilities
RUN apt-get update && \
apt-get install -y gcc make apt-transport-https ca-certificates build-essential
# check our python environment
RUN python3 --version
RUN pip3 --version
# set the working directory for containers
WORKDIR /usr/src/toxic-content-monitoring
# Installing python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy all the files from the project’s root to the working directory
COPY src/ /src/
COPY data /data/
RUN ls -la /src/*
# Running Python Application
CMD ["python3", "/src/main.py"]
This is the error message.
docker run toxic-content-monitoring:0.1
wiki.en.vec: 6.60GB [10:05, 10.9MB/s]
0%| | 0/2519371 [00:00<?, ?it/s]Skipping token 2519370 with 1-dimensional vector ['300']; likely a header
100%|██████████| 2519371/2519371 [08:36<00:00, 4878.67it/s]
Traceback (most recent call last):
File "/src/main.py", line 11, in <module>
from preprocessor import normalize_comment, clean_text
File "/src/preprocessor.py", line 42, in <module>
word_file = open(link, "r")
FileNotFoundError: [Errno 2] No such file or directory: 'data/identity_hateWordFile.txt'
I believe your Dockerfile is missing COPY data/ /data/ command in it. You are only doing a copy of src folder and not data. Please include COPY data /data/ right after COPY src/ /src/ line in your Dockerfile and give it a try.

Python custom module not found after running DockerFile

I am new to Docker and am currently having issues importing local python files into my Flask application using a Dockerfile
File hierarchy:
survey_tool
cli
survey_input.py
web
app.py
Current Dockerfile:
FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY cli /app
COPY web /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
In my application, I am trying to run a method from another python file (called gui_prompt).
When I try to build the app, the app immediately crashes and gives me this stack trace when running the docker logs file on the container id:
File "app.py", line 3, in <module>
from cli.survey_input import gui_prompt
ImportError: No module named cli.survey_input
Am I not importing the cli folder correctly?

flask not found in Docker

In the Docker file, I get this error
ImportError: No module named flask
Here is the Docker file
FROM continuumio/anaconda3
MAINTAINER kumar
COPY ./flask_master /usr/local/python
EXPOSE 5000
WORKDIR /usr/local/python
RUN apt-get update && apt-get install -y python3-pip
RUN pip3 install -r requirements.txt
CMD python flaskPredictAPI.py
Here is the requirements.txt
flask
flasgger
joblib
But when I run it, I get this
docker run -i -t -p 5000:5000 randomforestapi
Traceback (most recent call last):
File "flaskPredictAPI.py", line 1, in
from flask import Flask, request
ImportError: No module named flask
Are you able to build your docker image successfully? Did you checked if during build the requirements.txt gets executed without any error? I would suggest you generate your requirements.txt using below command in your project folder.
pip freeze > requirements.txt
It would look something like this
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

Resources