Home>
Images are not displayed in the flask app launched with docker

I launched the flask app using docker, but the image is not displayed.
If i don't use docker, it will be displayed properly, but I don't understand the cause.
Below are the files for building the docker environment and the html files for the problematic pages.

docker-compose.yml

version: '3'
services: services:
  flask:
    build: ..
    command: python3 app/app.py
    ports: ports:
      -"5000: 5000"
    volumes:
      --./app:/app
    tty: true
    container_name: CNN-app-with-flask

Dockerfile

FROM python: 3.7
COPY requirements.txt.
RUN pip install --upgrade pip
RUN pip install -r ./requirements.txt
RUN mkdir/app


result.html

{% extends "layout.html"%}
{% block content%}

  
     Judgment result 
    {{result}}
   
  <form method = "get" action = "/">
    <button>Back</button>
  </form>
  

{% endblock%}

Line 7 of this filefilepathThe file that contains the image firmly is specified in.

I would like somebody to teach.

  • Answer # 1

    I don't know where the image file is placed, but isn't it okay if I dig static or DIR in the text and place it there?

    The information presented is too half-hearted, so I think it's easier to get an answer if you show the minimum configuration and procedure that will cause the problem.

    By the way, I tried the following in my environment, but the image was displayed without any problem.

    ◆ Execution result

    $uname -a
    Linux localhost.localdomain 3.10.0-1127.el7.x86_64 # 1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    $cat/etc/redhat-release
    CentOS Linux release 7.8.2003 (Core)
    $docker -v
    Docker version 19.03.13, build 4484c46d9d
    $docker-compose -v
    docker-compose version 1.18.0, build 8dd22a9
    $ip -f inet addr show dev enp0s8
    3: enp0s8:<BROADCAST, MULTICAST, UP, LOWER_UP>mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute dynamic enp0s8
           valid_lft 472sec preferred_lft 472sec

    -+
     +-Dockerfile
     +-app
     | +-app.py
     | +-static
     | | +-stackoverflow.png
     | +-Templates
     | +-layout.html
     | +--result.html
     + --docker-compose.yml
     + --requirements.txt
    $cat Dockerfile
    FROM python: 3.7
    COPY requirements.txt.
    RUN pip install --upgrade pip
    RUN pip install -r ./requirements.txt
    RUN mkdir/app
    $cat docker-compose.yml
    version: '3'
    services: services:
      flask:
        build: ..
        command: python3 app/app.py
        ports: ports:
          -"5000: 5000"
        volumes:
          --./app:/app
        tty: true
        container_name: CNN-app-with-flask
    $cat requirements.txt
    flask
    $cat app/app.py
    #!/usr/bin/python
    from flask import Flask, render_template
    app = Flask (__ name__)
    @ app.route ('/')
    def hello ():
        filepath = "/static/stackoverflow.png"
        return render_template ('result.html', title ='flask test', filepath = filepath)
    if __name__ == "__main__":
        app.run (debug = True, host = '0.0.0.0')
    $cat app/templates/layout.html
    <! doctype html>
    <html>
    <head>
    <title>{{title}}</title>
    </head>
    <body>
    {% block content%}
    <!-Main contents->
    {% endblock%}
    </body>
    </html>
    $cat app/templates/result.html
    {% extends "layout.html"%}
    {% block content%}
      
         Judgment result 
        {{result}}
       
      <form method = "get" action = "/">
        <button>Back</button>
      </form>
      
    {% endblock%}

    # docker-compose build
    # docker-compose up


    stackoverflow.png is the stackoverflow logo file.

Trends