Home > OS >  Dockerfile specifying path of API
Dockerfile specifying path of API

Time:02-08

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

CodePudding user response:

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"]

CodePudding user response:

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"]
  •  Tags:  
  • Related