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" ]
Related
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" ]
I am trying to copy a folder with contents. But when trying to access the file from docker it shows an error
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements/development.txt'
My Dockerfile contains:
FROM python
ENV PYTHONUNBUFFERED=1
RUN apt-get update \
&& pip install pip install gunicorn
WORKDIR /usr/src/app
COPY requirements/* /usr/src/app/
RUN pip install -r requirements/development.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "cryb.wsgi:application"]
COPY requirements/* /usr/src/app/
You're copying stuff from requirements to /usr/src/app. This does NOT preserve directory structure. Make that
RUN mkdir /usr/src/app/requirements
COPY requirements/* /usr/src/app/requirements/
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"]
I have a project with Flask as backend and VueJS for frontend. Want to put all in the container, which will run on production server.
I need to install all dependencies (npm install) and build my static files from vue (npm run build) to get dist folder (with HTML file and assets), then build Flask project: install python, dependencies and run the server on gunicorn. After that copy my dist folder to Flask directory.
I'm read about multi-stage and try to combine it, here is my Dockerfile code:
FROM python:3.7-alpine as backend-builder
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
FROM node:lts-alpine as build-stage
RUN cd ..
RUN ls
RUN mkdir /frontend
WORKDIR /frontend
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM busybox
COPY --from=build-stage /frontend/dist /app/dist
RUN ls
It's going but don't copy dist to Flask directory
My project structure is:
project_folder
- app (python + flask files)
-- app.py
-- wsgi.py
-- requirements.txt
-- etc
- frontend (vuejs files, packages)
-- public
-- src
-- package.json
-- etc
- Dockerfile
What i'm doing wrong? How to write Dockerfile, to solve my problem?
The FROM busybox line creates a new image, it doesn't reference the python image. Would just build the js before you build the python so that you can copy the files in when the python build is complete.
FROM node:lts-alpine as build-stage
RUN cd ..
RUN ls
RUN mkdir /frontend
WORKDIR /frontend
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM python:3.7-alpine as backend-builder
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
COPY --from=build-stage /frontend/dist /app/dist
My flask app, the main file that starts the flask server on port 5000 is inside:
Server/main/core/setup.py
The Docker gives me this error and I know because it cannot find the setup.py:
ERROR: for flask_api Cannot start service flask_api: oci runtime error:
container_linux.go:265: starting container process caused "exec:
\"/main/api/core\": stat /main/api/core: no such file or directory"
My error is inside the Dockerfile:
I change my default path of the setup.py from the Server/setup.py to Server/app/main/core/setup.py
Question how to start the server on this path
Dockerfile:
This is before I move the setup.py file that starts the server on port 5000
# FROM python:3.6-alpine
# ADD ./Server /app
# WORKDIR /app
# RUN pip install -r requirements.txt
# EXPOSE 5000
# CMD ["python", "api.py"]
This is after I move the file deeper
FROM python:3.6-alpine
ADD ./Server /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["/main/core"]
CMD ["python", "setup.py"]
Any help how to hit the setup.py ??
There is a little mess with entrypoint (https://docs.docker.com/engine/reference/builder/#entrypoint) and workdir (https://docs.docker.com/engine/reference/builder/#workdir)
Please remember that you can use WORKDIR multiple times.
Your setup.py should be in path:
Server/main/core/setup.py
I suppose that your Dockerfile should be:
FROM python:3.6-alpine
ADD ./Server /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
WORKDIR /app/main/core
CMD ["python", "setup.py"]