I created my python web server with the flask library and flask_restful and I want to dockerize my web server, so I created a docker-compose. yml file and then a Dockerfile, for my part everything seems to be correct but when I execute the command “docker-compose up -d” and I go to look at the docker hub, I get the following error: python: can’t open file ‘/app/ServeurWeb.py’: [Errno 2] No such file or directory. I can’t solve it.
thanks,
Dockerfile:
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY ServeurWeb.py .
CMD [ "python", "/ServeurWeb.py" ]
It seems like there might be a problem with the path to your ServeurWeb.py file in your Dockerfile.
In your Dockerfile, you have the line COPY ServeurWeb.py ., which copies the ServeurWeb.py file to the /app directory in the container. However, in your CMD line, you are trying to run /ServeurWeb.py, which does not exist in the container.
To fix this, you can change your CMD line to CMD [ "python", "ServeurWeb.py" ]. This will run the ServeurWeb.py file in the /app directory, which is where it was copied to in the Dockerfile.
Also, in your docker-compose.yml file, you have an incorrect environment variable specified. The line - ./app:/app should be removed from the environment section, as it is not a valid environment variable. It should be moved to the volumes section instead, like this:
Dockerfile looks good to me. Just ensure that you have specified it correctly
CMD [ "python", "ServeurWeb.py" ]
Basically, the WORKDIR /app line in Dockerfile sets the working directory inside the container to “/app”. As you’re copying the ServeurWeb.py file to /app, you can just specify the filename. That should be sufficient.
Traceback (most recent call last):
File “/app/ServeurWeb.py”, line 4, in
from flask import Flask, render_template, jsonify, url_for, request
File “/usr/local/lib/python3.9/site-packages/flask/init.py”, line 19, in
from jinja2 import Markup, escape
ImportError: cannot import name ‘Markup’ from ‘jinja2’ (/usr/local/lib/python3.9/site-packages/jinja2/init.py)
Did you change the error message or the content of Dockerfile when you posted it? The CMD line in the Dockerfile couldn’t have caused the error message. If the CMD line is what you actually had when you built the image, your error message would have been this:
python: can’t open file ‘/ServeurWeb.py’:
Try this command:
docker-compose up -d --build
When you first run docker-compose up -d and there is no already built image, Docker Compose will run the build first. If you already have a built image, the following docker-compose up -d will only run the services using the previously built image. It would explain why the error message is different.
If there were inconsistencies with the error messages it’s probably because I tried several things, I must have mixed up the screenshots, now I’ve tried the command:
I thought so. Do you have any VPN or system wide proxy enabled on your Windows? Since you were able to build the image before, something must have changed since then.
No, I didn’t activate anything after building but I will check if I have a proxy or a vpn,
I have disabled my proxy and now when I build I get this error:
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.