Failed to parse 'app.server' as an attribute name or function call when running docker

Viewed 4504

I am trying to run my app from docker and I am getting this error message from docker logs for my dash. I have searched a lot but could not find anything. Please help!

The log message from docker logs for dash image

[2020-08-22 06:44:32 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-08-22 06:44:32 +0000] [1] [INFO] Listening at: http://0.0.0.0:8050 (1)
[2020-08-22 06:44:32 +0000] [1] [INFO] Using worker: sync
[2020-08-22 06:44:32 +0000] [8] [INFO] Booting worker with pid: 8
Failed to parse 'app.server' as an attribute name or function call.
[2020-08-22 06:44:33 +0000] [8] [INFO] Worker exiting (pid: 8)
[2020-08-22 06:44:33 +0000] [1] [INFO] Shutting down: Master
[2020-08-22 06:44:33 +0000] [1] [INFO] Reason: App failed to load.

My API is working fine

[2020-08-22 06:44:31 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-08-22 06:44:31 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2020-08-22 06:44:31 +0000] [1] [INFO] Using worker: sync
[2020-08-22 06:44:31 +0000] [8] [INFO] Booting worker with pid: 8

This is my Dash app. File name is app.py inside dash folder.

app = dash.Dash(
    __name__,
    external_stylesheets=external_stylesheets,
    meta_tags=[
        {"name": "viewport", "content": "width=device-width, initial-scale=1"}
    ],
    suppress_callback_exceptions=True
)

if __name__ == '__main__':
    ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev")
    DEBUG = ENVRIONMENT == "dev"
    HOST = '0.0.0.0' if ENVIRONMENT == "prod" else 'localhost'
    app.run_server(debug=DEBUG, host=HOST)

This is my Dockerfile for Dash

FROM python:3.7

ADD requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt

ADD . /app

EXPOSE 8050

CMD ["gunicorn", "-b", "0.0.0.0:8050", "app:app.server"]

and this is my docker-compose.yml file

version: '3'
services:
  api:
    build:
      context: src/api
      dockerfile: Dockerfile
    environment:
      - ENVIRONMENT=prod
    restart: always
  dash:
    build:
      context: src/dash
      dockerfile: Dockerfile
    ports:
      - "8050:8050"
    environment:
      - ENVIRONMENT=prod
      - API_URL=http://api:5000/api
    depends_on:
      - api
    restart: always

This is my api code as app.py file in api folder

app = Flask(__name__)
api = Blueprint('api', __name__)

app.register_blueprint(api, url_prefix='/api')

if __name__ == "__main__":
    ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev")
    DEBUG = ENVIRONMENT == "dev"
    HOST = '0.0.0.0' if ENVIRONMENT == "prod" else 'localhost'
    app.run(debug=DEBUG, host=HOST)

This is my Dockerfile for api

FROM python:3.7

ADD requirements.txt /app/
WORKDIR /app

RUN pip install -r requirements.txt

ADD . /app

EXPOSE 5000

CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]

This is what i get after running docker-compose up --build -d

Step 5/7 : ADD . /app
 ---> 5f1b89f2daeb
Step 6/7 : EXPOSE 5000
 ---> Running in 74a03831d03e
Removing intermediate container 74a03831d03e
 ---> cab01af5bfee
Step 7/7 : CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]
 ---> Running in 606197aae6fd
Removing intermediate container 606197aae6fd
 ---> 35be6754e766
Successfully built 35be6754e766
Successfully tagged appname_api:latest
Building dash
.
.
.

Step 5/7 : ADD . /app
 ---> c1111dca7a60
Step 6/7 : EXPOSE 8050
 ---> Running in ef1a216db216
Removing intermediate container ef1a216db216
 ---> a84d1d8ce503
Step 7/7 : CMD ["gunicorn", "-b", "0.0.0.0:8050", "app:app.server"]
 ---> Running in afc121660ddc
Removing intermediate container afc121660ddc
 ---> 600011a005ca
Successfully built 600011a005ca
Successfully tagged appname_dash:latest
Creating appname_api_1 ... done
Creating appname_dash_1 ... done
3 Answers

The issue is with the arguments you're passing to gunicorn. Based on what you're showing here, I suggest you try changing:

CMD ["gunicorn", "-b", "0.0.0.0:8050", "app:app.server"] to:

CMD ["gunicorn", "-b", "0.0.0.0:8050", "app:server"]

I fixed this error on a coworker's app where they had the following in app.py:

server = Flask(__name__) # define flask app.server
app = dash.Dash(
    __name__,
    server=server,
)

And then in an index.py they were doing:

from app import app

server = app.server # I add this part here

And then the Docker CMD was:

CMD ["gunicorn", "index:server", "-b", ":8050"]

And it worked.

I had a slightly different error but google led me here so I am posting for others in a similar situation. I had the below error:

Failed to parse '' as an attribute name or function call.

My problem was that in my Procfile I had a space between "wsgi:" and "app". The below was the correct syntax for the Procfile.

web: gunicorn wsgi:app

Related