简体   繁体   中英

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:

  1. docker build - < Dockerfile And the Docker image build with no errors
  2. docker run -d -p 5000:5000 (docker image id)
  3. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM