I am new to docker and trying to deploy this web app but cannot find solution. I'm running windows 10 and Docker Desktop. My docker-version is 19.03.5 and docker-compose version 1.25.4, build unknown.
My directory structure is:
-docker-compose.yml
-app
-static
js/
script.js
css/
style.css
-templates
-index.html
-app.py
-dockerfile
-requirements.txt
-db
-init.sql
docker-compose.yml
version: "2"
services:
app:
build: ./app
links:
- db
ports:
- "5000:5000"
db:
image: mysql:5.7
ports:
- "32000:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
dockerfile
FROM python:3.6
EXPOSE 5000
RUN mkdir -p /app
RUN ls /app
COPY static /app/static
COPY templates /app/templates
COPY app.py /app
COPY requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD python app.py
requirements.txt
flask
mysql-connector
app.py
from flask import Flask, render_template, request
import mysql.connector
import os
app = Flask(__name__, template_folder='/templates', static_folder='/static')
@app.route('/')
def index():
connection = mysql.connector.connect(
user = 'root',
password = 'root',
host= 'db',
port = '3306',
database = 'test'
)
cursor = connection.cursor()
cursor.execute("SELECT name from place")
names = cursor.fetchall()
return render_template('index.html', names = names)
if __name__ == '__main__':
app.run(host='0.0.0.0')
My docker command is –
docker-compose up
Then trying to access http://0.0.0.0:5000/ but get mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' and jinja2.exceptions.TemplateNotFound:
jinja2.exceptions.TemplateNotFound
Your flask folder paths are wrong, it should be either an absolute path like /app/templates
and /app/static
or a relative one like templates
abnd static
.
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306'
If your init.sql script is correct then there is nothing wrong with your database, you just have let it initialize properly (wait for the mysqld: ready for connections
message) before sending your first request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.