Docker. No such file or directory - python

I have some files which I want to move them to a docker container.
But at the end docker can't find a file..
The folder with the files on local machine are at /home/katalonne/flask4
File Structure if it matters:
The Dockerfile:
#
# First Flask App Dockerfile
#
#
# Pull base image.
FROM centos:7.0.1406
# Build commands
RUN yum install -y python-setuptools mysql-connector mysql-devel gcc python-devel
RUN easy_install pip
RUN mkdir /opt/flask4
WORKDIR /opt/flask4
ADD requirements.txt /opt/flask4
RUN pip install -r requirements.txt
ADD . /opt/flask4
# Define deafult command.
CMD ["python","hello.py"]
# Expose ports.
EXPOSE 5000
So I built the image with this command :
docker build -t flask4 .
I ran the container with volume by :
docker run -d -p 5000:5000 -v /home/Katalonne/flask4:/opt/flask4 --name web flask4
And when I want to run the file on the container :
docker logs -f web
I get this error that it can not find my hello.py file :
python: can't open file 'hello.py': [Errno 2] No such file or directory
What is my fault?
P.S. : I'm a Docker and Linux partially-noob.

The files and directories that are located in the same location as your Dockerfile are indeed available (temporarily) to your docker build. But, after the docker build, unless you have used ADD or COPY to move those files permanently to the docker container, they will not be available to your docker container after the build is done. This file context is for the build, but you want to move them to the container.
You can add the following command:
...
ADD . /opt/flask4
ADD . .
# Define deafult command.
CMD ["python","hello.py"]
The line ADD . . should copy over all the things in your temporary build context to the container. The location that these files will go to is where your WORKDIR is pointing to (/opt/flask4).
If you only wanted to add hello.py to your container, then use
ADD hello.py hello.py
So, when you run CMD ["python","hello.py"], the pwd that you will be in is /opt/flask4, and hello.py should be in there, and running the command python hello.py in that directory should work.
HTH.

Related

Docker mounting image error executable file not found in $PATH: unknown

I have a directory in which code files and subdirectories are, i want to mount these files to the docker image and run the index.py
My Dockerfile looks like this:
# Selected base python version
FROM python:3.9.6
COPY requirements.txt ./
# Install all packages - see readme to create the requirements.txt
RUN pip install -r requirements.txt
# Port the container listens
EXPOSE 5000
CMD ["python3", "index.py"]
My build process is like this:
docker build -t demo .
docker run -it -p 127.0.0.1:5000:5000 demo -v "$(pwd)":/.
However, the following errors occurs:
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-v": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
What is wrong?
I tried different paths, but they all lead to the same errors.
Google the error didn't lead to any solution.
The solution is to change the file:
# Selected base python version
FROM python:3.9.6
COPY requirements.txt ./
# Install all packages - see readme to create the requirements.txt
RUN pip install -r requirements.txt
# Port the container listens
EXPOSE 5000
CMD ["python3", "app/index.py"]
and run:
docker run -it -p 127.0.0.1:5000:5000 -v "$(pwd)"/:/app demo

Unable to mount host directory to docker container

I have a python app that runs every day to download images and saves them into specified folders with each day folder creation like this /home/ubuntu/images/yyyymmdd.
I have built a docker container of my python app on ubuntu 20. When I try to run the app by mounting the host directory then log message prints folder created /home/ubuntu/images/20220123 but I can not see any folder.
I have checked the docker folder /var/lib/docker and found that random hash is created inside folder containers and overlay2. So I have tried to mount with both directories as below but no luck.
sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/containers --network=host testapp/img-downloader:0.0.1
sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/overlay2 --network=host testapp/img-downloader:0.0.1
I can see the data folder created inside the images folder and image files got saved like this
/var/lib/docker/overlay2/d52bcf61cae2e563c3c8561bab53b4bb2dd2ea2d633a14d40c96d7992fffae28/diff/home/ubuntu/images/20220123
What I am missing here so that it's not saving images to host directory like /home/ubuntu/images/20220123 instead of the inside docker container.
My Dockerfile is as below -
FROM alpine:3.14
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3-dev mariadb-dev gcc musl-dev g++ && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
COPY ./requirements.txt /requirements.txt
WORKDIR /
RUN pip3 install -r requirements.txt
COPY ./ /
ENTRYPOINT [ "python3" ]
CMD [ "./main.py" ]
Please help here. thanks
...What i am missing here so that its not saving images to host directory like /home/ubuntu/images/20220123 instead of inside docker container.
Presumed you meant you want to save images to a directory on the host and not inside the container. There's no need to mount /var/lib/docker/.... You need to ensure your program saved files to a path that is bind mounted to the host. Examples:
mkdir images # Create a directory on the host to hold the images
docker run -it --rm -v ~/images:/images alpine ash -c "mkdir /images/yesterday; mkdir /images/today; echo 'hello' > /images/today/msg.txt; echo 'done.'"
After the container exited, issue ls -ld images/* on the host will show you the 2 directories created; cat images/today/msg.txt will print you the content you saved via the container (simulate if you download images via the container).

No such file or directory error when running Docker container

I have a REST Api for a Flask app with an Oracle database for which I use Oracle Instant Client.
I managed to run the app from my computer and it works fine and my task is to make a Docker file for this app. I don`t have much experience with Docker.
This is the Dockerfile that I have written
FROM python:3.7.5-slim-buster
# Installing Oracle instant client
WORKDIR /opt/oracle
RUN apt-get update && apt-get install -y libaio1 wget unzip \
&& wget
https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-
linuxx64.zip \
&& unzip instantclient-basiclite-linuxx64.zip \
&& rm -f instantclient-basiclite-linuxx64.zip \
&& cd /opt/oracle/instantclient* \
&& rm -f *jdbc* *occi* *mysql* *README *jar uidrvci genezi adrci \
&& echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-
instantclient.conf \
&& ldconfig
WORKDIR /app
COPY . .
EXPOSE 5000
CMD ["python", "/app/__init__.py"]
I use the following commands:
docker build - < Dockerfile
And the Docker image build with no errors
docker run -d -p 5000:5000 (docker image id)
docker start -ai (docker container id)
And I get this error: python: can't open file '/app/__init__.py': [Errno 2] No such file or directory
The folder structure of the app on my computer is the following:
C:\Proiecte_python\Flask_Docker_App-Start\app
and in app are the instant oracle client the python file and the Dockerfile.
Can please someone help me because I think it`s something wrong in the Dockerfile CMD path or something like that. I have tried many variants but none work
The last line of your Dockerfile
CMD ["python", "/app/__init__.py"]
is equivalent to executing
python /app/__init__.py
The error you are getting is that the file __init__.py does not exist.
The lines
WORKDIR /app
COPY . .
Are telling your container to CD into the /app directory then copy all files from your host machine (Eg your physical machine) into the /app directory of the container. (the COPY . . means to copy from the current directory of your host - eg the location you're running docker commands from - into the current directory of the container - /app).
It seems that as part of receiving the Dockerfile you should have also downloaded the __init.py__ file and then the Dockerfile would have copied that into your container for you.
Alternatively you may have missed steps in your instructions where you were meant to write your own __init.py__ file for testing.
Either way your solutions are to find the __init.py__ file and put it into your current working directory ( C:\Proiecte_python\Flask_Docker_App-Start\app ) and ensure that you run your docker build and docker run commands from that same directory eg -
cd C:\Proiecte_python\Flask_Docker_App-Start\app
docker build <....>
docker run <....>
docker start <....>
Or your other solution is to go back to the instructions and ensure that you have created the python file and put it in the correct place.
As a very basic Flask/Docker tutorial see the below link
https://runnable.com/docker/python/dockerize-your-flask-application

How to run a python program using Singularity from a docker container?

I have created a docker container for my pure python program and have set python main.py to be executed when the container is run. Running the container works as expected on my local machine. However, I want to run the container on my institution's high-performance cluster. The cluster machines use Singularity, which I am using to pull my docker image hosted on Dockerhub (the repo is darshank11/ga_paci_final). However, when I try to run the Singularity container, I get the following error: python3: can't open file 'main.py': [Errno 2] No such file or directory.
I've tried to change the base image in the Dockerfile, for example from FROM python:latest to FROM ubuntu:latest. I've made sure the docker container worked on my local machine, and then got one of my co-workers to pull the container from Dockerhub and run it too. Everything works fine until I get to Singularity.
Here is my docker file:
FROM ubuntu:16.04
RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev
RUN mkdir src
WORKDIR /src
COPY . /src
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
CMD ["python3", "-u", "main.py"]
You're getting that error because the execution context is not what you're expecting. The run path in singularity is the current directory on the host OS (e.g., ~/ga_paci_final), which has been mounted into the singularity image.
As mentioned in the comments, one solution is to give the full path to the python file in the docker CMD statement. Another option is to modify the %runscript block of singularity definition file to something like:
%runscript
cd /src
python3 -u main.py
That way you ensure the run environment is identical between Docker and Singularity.

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