Virtualhost: Put online a Flask website with Docker

I try to put online a Flask website with Docker. So, I’ve created a Dockerfile, a docker-compose and now I can see my website online with my domain name.

Despite my Virtualhost, I can’t access to my webiste by using subdomain and without port “:5000”. This virtualhost seems to be totally ignored!

  • Using SSL on nginx in a Docker container hosted on Jelastic
  • Accessing microservice inside Docker container?
  • Error running erlang in Windows Container
  • Docker apt-get fails inside minikube
  • Docker Run Time Statistics ( Benchmarks)
  • Can Docker plugins inspect label metadata from starting image?
  • Dockerfile:

    FROM python:latest

    RUN apt-get update && apt-get install -y apache2 \
        libapache2-mod-wsgi \
        build-essential \
        python \
        python-dev \
        python-pip \
        vim \
    && apt-get clean \
    && apt-get autoremove
    
    RUN apt-get install -y wget
    
    COPY src /var/www/flask
    
    RUN apt-get install -y python-requests
    RUN pip install flask-user
    RUN pip install -r var/www/flask/myapp/requirements.txt
    
    COPY src/myapp/myapp.conf /etc/apache2/sites-available/myapp.conf
    RUN a2enmod rewrite
    RUN a2enmod wsgi
    RUN a2enmod proxy_http
    RUN a2ensite myapp.conf
    RUN service apache2 restart
    
    EXPOSE 80
    

    docker-compose.yml

    flask:
        image: flask:latest
        command: python /var/www/flask/myapp/__init__.py
        volumes:
        - "/root/workspace/python/src/:/var/www/flask"
        ports:
        - "5000:5000"
    

    Virtualhost

    <VirtualHost *:80>
        ServerName my.domain.guru
    
        WSGIDaemonProcess myapp
        WSGIScriptAlias / /var/www/flask/myapp/myapp.wsgi
    
        <Directory /var/www/flask/myapp>
            WSGIProcessGroup myapp
            WSGIApplicationGroup %{GLOBAL}
            Order deny,allow
            Allow from all
        </Directory>
    
            ProxyPass / http://localhost:5000/
            ProxyPassReverse / http://localhost:5000/
            ProxyPreserveHost On
    </VirtualHost>
    

    __init__.py

    from flask import Flask
    app = Flask(__name__)
    @app.route("/")
    def hello():
      return "test" 
    
    if __name__ == "__main__":
      app.run(host='0.0.0.0')
    

    With these URL I can see my website:

    • my.domain.guru:5000
    • domain.guru:5000” (why?)

    But, I want to access to my website only with this URL : “my.domain.guru“. If I try with this URL, I have the following error message:

    Forbidden

    You don’t have permission to access / on this server.

    How to do this?

    (I can’t use the port 80 because it is already used by other websites)

  • Docker mount socket volume
  • What does it mean for a Docker environment variable to not have a name?
  • ERROR: In file './docker-compose.yml', service 'volumes' must be a mapping not an array
  • X11 forwarding of GUI app in Docker container
  • docker-compose up via Windows bat file?
  • Docker doesn't save data in host mounted volume
  • One Solution collect form web for “Virtualhost: Put online a Flask website with Docker”

    This is port 80: “my.domain.guru”, So you need to configure the web server that currently runs at 80 to proxy you app in 5000.

    Also, you are never running the Apache because your command runs only __init__.py. Don’t think a container as a lightweight VM with is own service management. A container runs only the command/entrypoint that you define in Dockerfile or docker-compose.

    So my recommendation is either to change your command to run Apache (/usr/sbin/apache2 -DFOREGROUND) or point your current web server at port 80 to proxy the port 5000 of flask.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.