Erreur Docker web server python

Then why you copy it from the project root? :slight_smile:

WORKDIR /app

only changes the working directory in the build container, not on the host. You still need to copy /app/ServeurWeb.py

ok so instead of WORKDIR /app, what should I put?

Workdir is okay. The key is

COPY /app/ServeurWeb.py .

ok great now I can build but when I go to my docker hub, I always get this error :
python: can’t open file ‘/WebServer.py’: [Errno 2] No such file or directory

I am sorry I can’t follow the changes you make. Sometimes you mentioned ServeurWeb.py, sometimes WebServer.py. We can help you with Docker related topics like the syntax, functionalities, but you need to keep your code consistent and refer the files that you actually copied…

ok I’m sory it’s because i’m french and sometimes I use de translator and translate the name of my file but the correct name is “ServeurWeb.py” and in all my configuration it’s “ServeurWeb.py”
And now my dockerfile is like this:

FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY /app/ServeurWeb.py .
CMD [ “python”, “/ServeurWeb.py” ]

I think you should start to learn about Dcoker build from the beginning step by step. If you don’t know which instruction does what you can’t create a proper Dockerfile. Even if you manage to “accidentally” write a cdorrect Dockerfile, you will not understand why it works.

I feel it is pretty obvious that you refer to the wrong pythn file in the CMD instruction, but if you are still not sure how the WORKDIR affects the instructions you can use absolute paths, although you will have a longer file and when you change something you need to make sure that you change the references everywhere. Here is how I would do

FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
COPY /app/ServeurWeb.py /app/ServeurWeb.py
CMD [ "python", "/app/ServeurWeb.py" ]

Now you can also see that even though you store the requirements file in the project root, you still copy it to the app folder. It’s fine if it was your intention.

WORKDIR could seem useless now, but it isn’t, since when you run any command in the container started from the image that will be the working directory so you can refer to files using relative paths if you want to.

ok no problem too bad

I am not sure what you mean. Did it work?

no it doesn’t work I have new error:

Traceback (most recent call last):
File “/app/ServeurWeb.py”, line 5, in
from flask_restful import Api, Resource, reqparse
ModuleNotFoundError: No module named ‘flask_restful’

That is a python error because of a missing module which you will need to try to solve.

I know but I already try to solve this problem but all the module are good

Moderator note: The below comments were moved here from a new topic to continue the previous discussion. @rimelek


hello,
I want to dockerize my python web server but I have problem durieng start of my web server and I have this error on Docker Hub:

thanks for you response

My dockerfile:
image

my docker-compose.yml:

my requirements.txt:
image

Please do not share text content as image, share it as text in a “preformatted text” block instead like described in this post: How to format your forum posts , section “Dockerfiles, Compose files, logs and code”.

How is this different from the other topic?

The difference is that I start all over again, my dockerfile to change as well as my docker-compose and my requirements.txt. And I ask the question again to maybe have several reviews or if looking at my new code, something was going to be found.

I merged the two topics. We are still talking about the same issue. I honestly don’t know why you expect it to work. I have already explained it before. If you want to run a python file from the root folder, you need to copy it there. Now please, show me the line which is what you think should copy the file there.

I think the best would be if you started from the beginning and not continue before you understand what the copy command does. Leve out everything, except the copy and check the file in the container.