GithubHelp home page GithubHelp logo

testdrivenio / flask-on-docker Goto Github PK

View Code? Open in Web Editor NEW
329.0 12.0 159.0 146 KB

Flask + Docker

License: MIT License

Dockerfile 19.29% Shell 13.61% Python 67.10%
docker flask flask-docker flask-docker-nginx-gunicorn

flask-on-docker's Introduction

Dockerizing Flask with Postgres, Gunicorn, and Nginx

Want to learn how to build this?

Check out the tutorial.

Want to use this project?

Development

Uses the default Flask development server.

  1. Rename .env.dev-sample to .env.dev.

  2. Update the environment variables in the docker-compose.yml and .env.dev files.

    • (M1 chip only) Remove -slim-buster from the Python dependency in services/web/Dockerfile to suppress an issue with installing psycopg2
  3. Build the images and run the containers:

    $ docker-compose up -d --build

    Test it out at http://localhost:5001. The "web" folder is mounted into the container and your code changes apply automatically.

Production

Uses gunicorn + nginx.

  1. Rename .env.prod-sample to .env.prod and .env.prod.db-sample to .env.prod.db. Update the environment variables.

  2. Build the images and run the containers:

    $ docker-compose -f docker-compose.prod.yml up -d --build

    Test it out at http://localhost:1337. No mounted folders. To apply changes, the image must be re-built.

flask-on-docker's People

Contributors

kimjune01 avatar mjhea0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-on-docker's Issues

redirect(url_for('') issue

when i use redirect(url_for('')) the port 1337 will missing.

how to fix this issue?

I set proxy_set_header Host $host:$server_port;
but not working.

Volume for static files doesn't receive file updates

Hey!

I've been following the great guide for this repo and was wondering about the static_volume defined for both web and nginx services here

The tutorial sort of just doesn't mention this, but the way it is set up the files in the volume are never updated on rebuild even if they change (unless destroying the volume), or am I misunderstanding the setup?

I wonder how one could get file changes to be reflected in the volume? And I don't mean a mapping like for the dev setup, but say you develop some static assets in the app and release the next version in production... currently no files are updated. This seems like quite a crucial bit missing from the setup that I couldn't figure out.

Build fails

The command in Dockerfile.prod: RUN flake8 --ignore=E501,F401 . causes the build to fail with the following error:
ERROR: Service 'web' failed to build: The command '/bin/sh -c flake8 --ignore=E501,F401 .' returned a non-zero code: 1

Unable to sign-in/up for testdriven.io

Hi there,

not strictly an issue with this course, but your email system appears down, that or i tried signing in with github, but may already have in the dim distant past had an account with the same email?!

Thanks,

Stephen

Gateway Timeout

Hey there,

I followed the tutorial and everything seemed to work out quite well except for the part that I cannot access the site and it's simply giving me a Gateway Timeout when I try to access the configured subdomain.

Please note that I do not have local access to the mydomain.com server so testing via localhost:5001 is not possible for me.
However, this kind of subdomain-to-service configuration via labels is definitely working for several other containers.

docker-compose.prod.yaml

version: '3.3'

services:
  nginx-csc:
    build: ./nginx
    container_name: prod_nginx
    volumes:
      - static_volume:/home/app/web/project/static
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.nginx-csc.entrypoints=http"
      - "traefik.http.routers.nginx-csc.rule=Host(`test.mydomain.com`)"
      - "traefik.http.routers.nginx-csc-secure.entrypoints=https"
      - "traefik.http.routers.nginx-csc-secure.rule=Host(`test.mydomain.com`)"
      - "traefik.http.routers.nginx-csc-secure.tls=true"
      - "traefik.http.routers.nginx-csc-secure.tls.certresolver=http"
      - "traefik.http.routers.nginx-csc-secure.service=nginx-csc"
      - "traefik.http.routers.nginx-csc-secure.middlewares=secHeaders@file,httpsRedirect@file"
      - "traefik.http.services.nginx-csc.loadbalancer.server.port=80"
    depends_on:
      - web
  web:
    build:
      context: ./web
      dockerfile: Dockerfile.prod
    container_name: prod_web
    command: gunicorn --bind 0.0.0.0:5000 manage:app
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:13
    container_name: prod_db
    volumes:
      - postgres_data_prod:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db

volumes:
  postgres_data_prod:
  static_volume:

nginx.conf

upstream hello_flask {
    server web:5000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_flask;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /home/app/web/project/static/;
    }

}

I start the stack via docker-compose -f docker-compose.prod.yaml up -d --build.

According to the log files, everything is running without problem:

web service

Waiting for postgres...
PostgreSQL started
[2023-08-04 16:02:55 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2023-08-04 16:02:55 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2023-08-04 16:02:55 +0000] [1] [INFO] Using worker: sync
[2023-08-04 16:02:55 +0000] [49] [INFO] Booting worker with pid: 49

db service

2023-08-04 16:02:51.294 UTC [1] LOG:  starting PostgreSQL 13.11 (Debian 13.11-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2023-08-04 16:02:51.300 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-08-04 16:02:51.302 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-08-04 16:02:51.305 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-08-04 16:02:51.319 UTC [61] LOG:  database system was shut down at 2023-08-04 16:02:51 UTC
2023-08-04 16:02:51.882 UTC [1] LOG:  database system is ready to accept connections

nginx service

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/08/04 16:02:53 [notice] 1#1: using the "epoll" event method
2023/08/04 16:02:53 [notice] 1#1: nginx/1.25.1
2023/08/04 16:02:53 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023/08/04 16:02:53 [notice] 1#1: OS: Linux 5.4.0-155-generic
2023/08/04 16:02:53 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/08/04 16:02:53 [notice] 1#1: start worker processes
2023/08/04 16:02:53 [notice] 1#1: start worker process 19

I don't even get a HTTP status code, just a Gateway Timeout. Does anybody have an idea why?

F541 f-string is missing placeholders

Step 9/28 : RUN flake8 --ignore=E501,F401 .
---> Running in 8c52c55c4d5b
./project/init.py:52:12: F541 f-string is missing placeholders
ERROR: Service 'web' failed to build: The command '/bin/sh -c flake8 --ignore=E501,F401 .' returned a non-zero code: 1

Probably it is some python 3.8 issue.

The temporary solution is to remove the f-string (f mark) or add F541 to flake8 ignore parameter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.