Erreur Docker web server python

Hello,

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

docker-compose.yml:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      - MONGO_URI=mongodb://mongo:27017/mydb
      - ./app:/app
    depends_on:
      - mongo
  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"

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:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      - MONGO_URI=mongodb://mongo:27017/mydb
    volumes:
      - ./app:/app
    depends_on:
      - mongo
  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"

With these changes, you should be able to run docker-compose up -d without any issues.

Bonjour,
J’ai modifié mon fichier docker-compose.yml mais je n’ai pas compris ce que vous vouliez que je change dans mon Dockerfile.

Thanks,

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.

ok but my “WebServer.py” file must be in the same directory as my docker-compose.yml, Dockerfile files.


Et maintenant j’ai cette erreur:


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)


Thanks.

In the first post the error message is

Yet the CMD line in the Dockerfile was

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:

docker-compose up -d --build

But I get this error:

Thanks.

Please, be careful what you share so we won’t spend a lot of time fixing an issue that does not exist :slight_smile:

It looks like a network issue. Please share the output of the following commands:

docker info
docker version

Docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.9.1)
  compose: Docker Compose (Docker Inc., v2.10.2)
  extension: Manages Docker extensions (Docker Inc., v0.2.9)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.19.0)

Server:
 Containers: 15
  Running: 7
  Paused: 0
  Stopped: 8
 Images: 9
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.102.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 16
 Total Memory: 7.476GiB
 Name: docker-desktop
 ID: CMWM:EFVY:FUUC:Z7LU:R6PI:O5GW:6VDF:F754:2RZX:DPFP:T7RV:KXIF
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support

docker version:

Docker version 20.10.17, build 100c701

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:

So the file is not in the same folder as the Dockerfile. Isn’t it in /app?

No my file “ServeurWeb.py” is in a folder “app” and my dockerfile is with the file “app” like this:

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.