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/
Related
My app directory structure look like this:
Root/
app.py
requirements.txt
imageHelpers/
helpers.py
config.yml
model.pth
Instead of copying my model.pth to the server, I want to download it from the web while building the container. I want my model.pth to have the exact path as imageHelpers/model.pth. What should be the path for wget -O PATH_TO_MODEL URL?
Below is my Dockerfile
FROM python:3.9
WORKDIR /fast_api_test
COPY ./requirements.txt /fast_api_test/requirements.txt
RUN pip3 install -r /fast_api_test/requirements.txt
COPY . .
RUN python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' \
&& wget -q -O /imageHelpers/model.pth https://MY_URL/model.pth
EXPOSE 5000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "5000"]
Using AWS + Ubuntu if that's required.
when I run sudo docker build -t fast_api . , it gives me error:
/fast_api_test/imageHelpers/model.pth: No such file or directory
The command '/bin/sh -c pip3 install --upgrade -r /fast_api_test/requirements.txt && python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' && wget -q -O /fast_api_test/imageHelpers/model.pth https://MY_URL/model.pth' returned a non-zero code: 1
COPY (like ADD) tries to copy from OUTSIDE the image INTO the image.
as the file is downloaded INSIDE the image already by wget, you need to use the commandline cp command
try this:
RUN cp <downloaded_file> /fast_api_test
COPY . /fast_api_test
I'm using docker for the first time. I have a web application in angular and a backend application in python/flask. After some struggle, I finally managed to get everything to work, but docker can't find my API program in my backend:
My server file is at /my_backend/src/server.py
My docker file is at /my_backend/Dockerfile.dockerfile
Content of Dockerfile.dockerfile:
FROM python:3.7-slim
COPY /src .
WORKDIR /src/app
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]
The error message in the command prompt is
Attaching to backend, frontend
backend | python: can't open file 'server.py': [Errno 2] No such file or directory
backend exited with code 2
Feel free to ask for more information.
I used this tutorial: https://medium.com/analytics-vidhya/dockerizing-angular-application-and-python-backend-bottle-flask-framework-21dcbf8715e5
If your script is in /src directory, don't use WORKDIR /src/app but WORKDIR /src or CMD ["python","../server.py"]
Solution 1:
FROM python:3.7-slim
COPY /src .
WORKDIR /src # <- HERE
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]
Solution 2:
FROM python:3.7-slim
COPY /src .
WORKDIR /src/app
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","../server.py"] # <- HERE, or "/src/server.py"
Suggested by #TheFool:
FROM python:3.7-slim
WORKDIR /src/app # Swap WORKDIR and COPY
COPY /src .
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]
turn your workdir and copy instructions around to make it work.
FROM python:3.7-slim
WORKDIR /src/app # workdir first
COPY /src .
ENV PYTHONUNBUFFERED 1
COPY requirements.txt /tmp/requirements.txt
RUN python3 -m pip install -r /tmp/requirements.txt
CMD ["python","server.py"]
My Dockerfile
FROM python:3.7 AS builder
RUN python3 -m venv /venv
COPY requirements.txt .
RUN /venv/bin/pip3 install -r requirements.txt
FROM python:3.7
WORKDIR /home/sokov_admin/www/bot-telegram
COPY . .
CMD ["/venv/bin/python", "./bot.py"]
When I run the docker image I have this error:
docker: Error response from daemon: OCI runtime create failed:
container_linux.go:380: starting container process caused: exec:
"/venv/bin/python": stat /venv/bin/python: no such file or directory:
unknown.
What should I change in my code?
The example you show doesn't need any OS-level dependencies for Python dependency builds. That simplifies things significantly: you can do things in a single Docker build stage, without a virtual environment, and there wouldn't be any particular benefit from splitting it up.
FROM python:3.7
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["./bot.py"]
The place where a multi-stage build with a virtual environment helps is if you need a full C toolchain to build Python libraries. In this case, in a first stage, you install the C toolchain and set up the virtual environment. In the second stage you need to COPY --from=... the entire virtual environment to the final image.
# Builder stage:
FROM python:3.7 AS builder
# Install OS-level dependencies
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
build-essential
# libmysql-client-dev, for example
# Create the virtual environment
RUN python3 -m venv /venv
ENV PATH=/venv/bin:$PATH
# Install Python dependencies
WORKDIR /app
COPY requirements.txt .
RUN pip3 install -r requirements.txt
# If your setup.py/setup.cfg has a console script entry point,
# install the application too
# COPY . .
# RUN pip3 install .
# Final stage:
FROM python:3.7 # must be _exactly_ the same image as the builder
# Install OS-level dependencies if needed (libmysqlclient, not ...-dev)
# RUN apt-get update && apt-get install ...
# Copy the virtual environment; must be _exactly_ the same path
COPY --from=builder /venv /venv
ENV PATH=/venv/bin:$PATH
# Copy in the application (if it wasn't `pip install`ed into the venv)
WORKDIR /app
COPY . .
# Say how to run it
EXPOSE 8000
CMD ["./bot.py"]
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" ]
I rewrote some sync python lib to async. How do I integrate it into my project?
I did the following:
clone it from github and rewrited
build the lib using python3 setup.py bdist_wheel --universal and
got the file .whl file
How can I integrate it to my project?
Currently I have the following docker file:
FROM python:3.6
MAINTAINER ...
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . $APP_DIR
EXPOSE 8080
CMD python app.py
How do I copy the .whl file into to container and install it using pip3 install {...}.whl?
First add WORKDIR /app before COPY requirements.txt to specify your app working directory inside the container, then if you have xxx.whl in the same folder as requirements.txt just copy it COPY xxx.whl /app then RUN pip install xxx.whl
like this:
FROM python:3.6
MAINTAINER ...
# specify workdir
WORKDIR /app
COPY requirements.txt /app
# copy xxx.whl from host to container
COPY xxx.whl /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# install xxx.whl
RUN pip install xxx.whl
COPY . /app
EXPOSE 8080
CMD python app.py