GithubHelp home page GithubHelp logo

nickjj / docker-flask-example Goto Github PK

View Code? Open in Web Editor NEW
563.0 9.0 98.0 1.59 MB

A production ready example Flask app that's using Docker and Docker Compose.

License: MIT License

Shell 27.81% Dockerfile 7.83% CSS 0.85% JavaScript 3.00% HTML 21.08% Python 37.62% Mako 1.80%
flask postgres redis tailwindcss docker celery docker-compose esbuild

docker-flask-example's People

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

docker-flask-example's Issues

cannot create directory ‘public/js’: Permission denied - Rootless docker

Hey Nick, first of all, thank you so much for this amazing repo, man, it is helping me a lot.

I just want to check with you if it is possible to run this repo in an env where docker is installed as rootless.

When I run docker compose up in my nixos, with "root-full" docker, everything works fine, but doesn`t work if I run in a rootless docker.

Will share here some outputs.

[beto@nixos:~/test/ruby_projects/docker-flask-example]$ docker compose run web mkdir public/js
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
[+] Creating 2/0
 ✔ Container helloflask-redis-1     Created                                                                                                                                                            0.0s 
 ✔ Container helloflask-postgres-1  Created                                                                                                                                                            0.0s 
[+] Running 2/2
 ✔ Container helloflask-redis-1     Started                                                                                                                                                            0.3s 
 ✔ Container helloflask-postgres-1  Started                                                                                                                                                            0.3s 
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
mkdir: cannot create directory ‘public/js’: Permission denied


[beto@nixos:~/test/ruby_projects/docker-flask-example]$ docker compose run web id
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
[+] Creating 2/0
 ✔ Container helloflask-redis-1     Running                                                                                                                                                            0.0s 
 ✔ Container helloflask-postgres-1  Running                                                                                                                                                            0.0s 
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
uid=1000(python) gid=1000(python) groups=1000(python)


[beto@nixos:~/test/ruby_projects/docker-flask-example]$ docker compose run js id
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
uid=1000(node) gid=1000(node) groups=1000(node)


[beto@nixos:~/test/ruby_projects/docker-flask-example]$ id
uid=1000(beto) gid=100(users) groups=100(users),1(wheel),57(networkmanager),1000(beto)


[beto@nixos:~/test/ruby_projects/docker-flask-example]$ docker compose run css id
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
[+] Building 0.0s (0/0)                                                                                                                                                                      docker:default
uid=1000(node) gid=1000(node) groups=1000(node)


[beto@nixos:~/test/ruby_projects/docker-flask-example]$ ls -ln
total 104
-rw-r--r-- 1 1000 1000  1936 jan 17 15:18 alembic.ini
drwxr-xr-x 5 1000 1000  4096 jan 17 15:18 assets
drwxr-xr-x 2 1000 1000  4096 jan 17 15:18 bin
-rw-r--r-- 1 1000 1000 13853 jan 17 15:18 CHANGELOG.md
drwxr-xr-x 2 1000 1000  4096 jan 17 15:18 config
drwxr-xr-x 3 1000 1000  4096 jan 17 15:18 db
-rw-r--r-- 1 1000 1000  3278 jan 17 20:35 docker-compose.yml
-rw-r--r-- 1 1000 1000  2145 jan 17 15:18 Dockerfile
drwxr-xr-x 5 1000 1000  4096 jan 17 15:18 hello
drwxr-xr-x 2 1000 1000  4096 jan 17 15:18 lib
-rw-r--r-- 1 1000 1000  1108 jan 17 15:18 LICENSE
drwxr-xr-x 2 1000 1000  4096 jan 17 15:18 public
-rw-r--r-- 1 1000 1000   104 jan 17 15:18 pyproject.toml
-rw-r--r-- 1 1000 1000 18044 jan 17 15:18 README.md
-rw-r--r-- 1 1000 1000   850 jan 17 15:18 requirements-lock.txt
-rw-r--r-- 1 1000 1000   357 jan 17 15:18 requirements.txt
-rwxr-xr-x 1 1000 1000  4243 jan 17 15:18 run
drwxr-xr-x 3 1000 1000  4096 jan 17 15:18 test

I ask for your help, please.

Thanks in advance.

Flask / Werkzeug 3.0 and Python 3.12 updates

These are updates I'd like to perform but there's currently a few blockers from 3rd party dependencies:

If anyone is using other popular 3rd party libraries that aren't working with either Flask 3.0 or Python 3.12 please post them here.

Celery stuff

I've been using this code template to learn. Great stuff, this has been very helpful! I have a few notes regarding the Celery implementation in particular.

  • PyCharm yells at me if metaclass=ABCMeta isn't inside the custom task. I was using from celery import Task which is worse than celery_app.Task, so ignore.

  • TaskBase.__call__(self, *args, **kwargs) seems less pythonic than super().__call__(*args, **kwargs)

  • As of Celery 5.0, the Celery CLI is actually a Click command, so you can do something like this:

    from celery.bin.celery import celery as celery_cmd
    app.cli.add_command(celery_cmd)

    The major benefit of this is that you no longer need to modify celery -A "hello.app.celery_app" worker, and simply flask celery worker works. So it becomes agnostic to where the celery app is defined, and removes the need to update this part of the script for a real app not named hello. The reason why it becomes agnostic seems to be: because it's called via the flask CLI, it guarantees that the normal Flask app initialization code is run prior to the Celery command being run, so it knows that Celery was called inside hello.app.celery_app. (Another benefit could potentially be decorating the command with flask.cli.with_appcontext, but I've not found a reason for that.)

WDYT?

Error when doing docker compose

I am on Windows.
After docker compose up --build I get:

[+] Running 21/21
 - redis Pulled                                                                                                   62.9s
   - bb263680fed1 Pull complete                                                                                   37.5s
   - ac509f65c3e9 Pull complete                                                                                   37.7s
   - 51afc2cce3df Pull complete                                                                                   38.0s
   - 817f7e347ebd Pull complete                                                                                   38.9s
   - ab1a1215d5f9 Pull complete                                                                                   39.0s
   - db7c27bf3552 Pull complete                                                                                   39.2s
 - postgres Pulled                                                                                                61.8s
   - 3f9582a2cbe7 Already exists                                                                                   0.0s
   - 0d9d08fc1a1a Pull complete                                                                                    4.4s
   - ecae4ccb4d1b Pull complete                                                                                    5.1s
   - e75693e0d7a5 Pull complete                                                                                    6.5s
   - 1b6d5aead1a8 Pull complete                                                                                   11.0s
   - f2aa67d9a6b2 Pull complete                                                                                   13.2s
   - 7a3ec0371e36 Pull complete                                                                                   13.6s
   - 704d9d1b662d Pull complete                                                                                   14.1s
   - a6e09efc43e8 Pull complete                                                                                   57.1s
   - cb87a0a6528d Pull complete                                                                                   57.5s
   - 3e290cb732cd Pull complete                                                                                   57.6s
   - d44d65eaede3 Pull complete                                                                                   57.8s
   - 0c2430d596bb Pull complete                                                                                   58.0s
[+] Building 10.0s (11/11) FINISHED
 => [helloflask_web internal] load build definition from Dockerfile                                                1.2s
 => => transferring dockerfile: 2.26kB                                                                             0.9s
 => [helloflask_worker internal] load build definition from Dockerfile                                             1.2s
 => => transferring dockerfile: 2.26kB                                                                             0.9s
 => [helloflask_js internal] load build definition from Dockerfile                                                 3.3s
 => => transferring dockerfile: 2.26kB                                                                             0.9s
 => [helloflask_css internal] load build definition from Dockerfile                                                3.3s
 => => transferring dockerfile: 2.26kB                                                                             0.9s
 => [helloflask_web internal] load .dockerignore                                                                   3.2s
 => => transferring context: 210B                                                                                  0.7s
 => [helloflask_worker internal] load .dockerignore                                                                3.1s
 => => transferring context: 210B                                                                                  2.6s
 => [helloflask_js internal] load .dockerignore                                                                    3.2s
 => => transferring context: 210B                                                                                  2.5s
 => [helloflask_css internal] load .dockerignore                                                                   2.9s
 => => transferring context: 210B                                                                                  0.4s
 => ERROR [helloflask_worker internal] load metadata for docker.io/library/python:3.11.2-slim-bullseye             5.7s
 => ERROR [helloflask_js internal] load metadata for docker.io/library/node:18.10.0-bullseye-slim                  6.0s
 => [auth] library/python:pull token for registry-1.docker.io                                                      0.0s
------
 > [helloflask_worker internal] load metadata for docker.io/library/python:3.11.2-slim-bullseye:
------
------
 > [helloflask_js internal] load metadata for docker.io/library/node:18.10.0-bullseye-slim:
------
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: 
failed to authorize: rpc error: code = Unknown desc = failed to fetch oauth token: Post "https://auth.docker.io/token": dial 
tcp: lookup auth.docker.io: no such host

Tips on deploying

Hi,
First of all thank you for the great repository. I am new to docker and this repository is very helpful to me.

I have used the repository to dockerise one of my flask APIs and I am trying to deploy it on a VPS (racknerd to be more specific).
I want to expose the API endpoint to the public so that anyone over the internet can use it.

So if I have to use localhost:8000/api_endpoint to hit the endpoint on my own system, the internet users should be able to use public.ip.address.of.vps:8000/api_endpoint. However I am unable to do this.

What have I tried?

  1. In the dockerfile, I changed the CMD gunicorn line to
    CMD ["gunicorn", "-c", "python:config.gunicorn", "-b", "0.0.0.0", "hello.app:create_app()"]

  2. In config/settings.py, I changed the ip in the SERVER_NAME from localhost to 0.0.0.0
    "SERVER_NAME", "0.0.0.0:{0}".format(os.getenv("PORT", "8000"))

After doing the above changes, locally if I do 0.0.0.0:8000/api_endpoint it works fine. But if I try to do < public ip address of vps >:8000/api_endpoint, it gives the following 404 error on the browser:

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

And in the ssh terminal where docker-compose up is running, the logs say this as a warning:

home/python/.local/lib/python3.10/site-packages/flask/app.py:1781: UserWarning: Current server name '<public ip of vps>:8000' doesn't match configured server name '0.0.0.0:8000'
and also
/home/python/.local/lib/python3.10/site-packages/flask/sessions.py:228: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.

After this I tried changing the 0.0.0.0 to in settings.py (also tried in the dockerfile gunicorn command, but that gave an error Invalid Address, so in the dockerfile gunicorn command the host was 0.0.0.0). This gave the following error on the browser:

Bad Request
The browser (or proxy) sent a request that this server could not understand.

and the following warning in the ssh terminal running docker-compose:
/home/python/.local/lib/python3.10/site-packages/flask/sessions.py:228: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.

This is most probably just my lack of knowledge and I do not know what to tweak to get the app on the VPS exposed to the internet. Please guide me in the right direction.

Thank you

models usage

Hi,

How can i define models, create tables and interact with the data with this setup? slightly confused as you've added a seeds.py but no template models

assistance understanding assets and es build in production

Hi Nick,

It's me again, I'm new to ES build / Flask and fancy node based frontend stuff...

I've got an app in production and I'm seeing my logs continuesly state that it's rebuilding CSS and JS

css-1    |
css-1    | Done in 1439ms.
css-1    |
css-1    | Task completed in 0m2.511s
css-1    | Browserslist: caniuse-lite is outdated. Please run:
css-1    |   npx update-browserslist-db@latest
css-1    |   Why you should do it regularly: https://github.com/browserslist/update-db#readme
css-1    |
css-1    | Rebuilding...
css-1    |
css-1    | Done in 1526ms.
css-1    |
css-1    | Task completed in 0m2.515s
js-1     |
js-1     | Task completed in 0m0.352s
css-1    | Browserslist: caniuse-lite is outdated. Please run:
css-1    |   npx update-browserslist-db@latest
css-1    |   Why you should do it regularly: https://github.com/browserslist/update-db#readme
css-1    |
css-1    | Rebuilding...
css-1    |
css-1    | Done in 1534ms.
css-1    |
css-1    | Task completed in 0m2.572s```

I thought in production this was to be build once and not again, it seems the `production` and debug flags are a bit off too, when I put things in production I still had to modify the docker container to set the values to `False` vs `false` in the config.settings.py

Any advise on better understand prod vs dev? 

unbound variable on macos ventura

❯ ./run quality
./run: line 55: @: unbound variable

❯ ./run lint:dockerfile
./run: line 44: @: unbound variable

Solution

brew install bash

this currently takes you from:
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22)
to
GNU bash, version 5.2.2(1)-release (aarch64-apple-darwin22.1.0)

Getting Error with docker compose => ERROR [helloflask_web app 6/9] RUN chmod 0755 bin/* && bin/pip3-install

My steps:

  1. git clone https://github.com/nickjj/docker-flask-example backend
  2. cd backend
  3. cp .env.example .env
  4. cp docker-compose.override.yml.example docker-compose.override.yml
  5. docker-compose up --build
  6. ERROR (see below for full log)

[+] Building 13.7s (20/44)
=> [helloflask_web internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 32B 0.0s
=> [helloflask_webpack internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [helloflask_worker internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [helloflask_web internal] load .dockerignore 0.0s
=> => transferring context: 35B 0.0s
=> [helloflask_worker internal] load .dockerignore 0.1s
=> => transferring context: 35B 0.0s
=> [helloflask_webpack internal] load .dockerignore 0.5s
=> => transferring context: 35B 0.0s
=> [helloflask_web internal] load metadata for docker.io/library/python:3.9 11.4s
=> [helloflask_webpack internal] load metadata for docker.io/library/node:1 11.0s
=> [helloflask_webpack internal] load build context 0.6s
=> => transferring context: 1.71kB 0.1s
=> [helloflask_worker webpack 1/8] FROM docker.io/library/node:14.17.5-bulls 0.0s
=> CACHED [helloflask_worker webpack 2/8] WORKDIR /app/assets 0.0s
=> CANCELED [helloflask_webpack webpack 3/8] RUN apt-get update && apt-get 1.9s
=> [helloflask_web internal] load build context 0.6s
=> => transferring context: 3.16kB 0.1s
=> [helloflask_worker internal] load build context 0.6s
=> => transferring context: 194.00kB 0.1s
=> [helloflask_web app 1/9] FROM docker.io/library/python:3.9.6-slim-bullsey 0.0s
=> CACHED [helloflask_web app 2/9] WORKDIR /app 0.0s
=> CACHED [helloflask_web app 3/9] RUN apt-get update && apt-get install - 0.0s
=> CACHED [helloflask_web app 4/9] COPY --chown=python:python requirements*. 0.0s
=> CACHED [helloflask_web app 5/9] COPY --chown=python:python bin/ ./bin 0.0s
=> ERROR [helloflask_web app 6/9] RUN chmod 0755 bin/* && bin/pip3-install 1.1s


[helloflask_web app 6/9] RUN chmod 0755 bin/* && bin/pip3-install:
#24 1.099 /bin/sh: 1: bin/pip3-install: not found


failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c chmod 0755 bin/* && bin/pip3-install]: exit code: 127

Any idea how to resolve this?

Issues running in production executing the application

my app works locally via docker no problem with the typical docker workflow.

I've moved it to my VM and attempting to build the app
my requirements.txt file

Flask==3.0.2
werkzeug==3.0.1
jinja2==3.1.3
gunicorn==21.2.0

psycopg==3.1.18
SQLAlchemy==2.0.28
SQLAlchemy-Utils==0.41.1
Flask-SQLAlchemy==3.1.1
alembic==1.13.1

redis==5.0.3
celery==5.3.6

pytest==8.1.1
pytest-cov==4.1.0
flake8==7.0.0
isort==5.13.2
black==24.2.0

flask-debugtoolbar==0.14.1
Flask-Static-Digest==0.4.0
Flask-Secrets==0.1.0
Flask-DB==0.4.1

## mine added
pydantic
requests
pandas
datetime
ujson
lxml
beautifulsoup4
pyyaml
debugpy
gspread
python-dateutil
playwright

It's failing to build docker compose build
with this message

 => [web app  9/11] COPY --chown=python:python --from=assets /app/public /public                                                                                                                                                     1.0s
 => [web app 10/11] COPY --chown=python:python . .                                                                                                                                                                                   0.9s
 => ERROR [web app 11/11] RUN if [ "false" != "true" ]; then   ln -s /public /app/public && flask digest compile && rm -rf /app/public; fi                                                                                           6.4s
------
 > [web app 11/11] RUN if [ "false" != "true" ]; then   ln -s /public /app/public && flask digest compile && rm -rf /app/public; fi:
5.582 Illegal instruction (core dumped)
------
failed to solve: process "/bin/sh -c if [ \"${FLASK_DEBUG}\" != \"true\" ]; then   ln -s /public /app/public && flask digest compile && rm -rf /app/public; fi" did not complete successfully: exit code: 132

in frustration I've cloned the repo in a different folder, added my same requirements.txt and without issue the hello flask builds and runs perfectly.

Any tips on tracking down what about my code / includes are causing the Illegal instruction (core dumped) I figured it was a python package. Everything I'm finding points to older CPU support, which if that was the case I'd expect the vanilla hello flask app not to build on the same host machine.

Thoughts? thanks in advance

Frontend folder creation permissions issue

Hi there, thanks for the wonderful project! When setting up and running the app locally, I'm getting an HTML page with no styles. It appears that none of the frontend public/ dirs are getting created.

I'm running on Pop_OS 22.04.

helloflask-css-1       | mkdir: cannot create directory '../public/css': Permission denied
helloflask-js-1        | mkdir: cannot create directory '../public/js': Permission denied

I am exporting linux ids in the env file as recommended:

export UID=1000
export GID=1000

I've also tried modifying the folder creation code in the Dockerfile as follows:

RUN mkdir -p /app/public \
  && chown node:node -R /app/public \
  && if [ "${NODE_ENV}" != "development" ]; then \
  ../run yarn:build:js && ../run yarn:build:css; fi

Since it seems to be a permissions error with the node user, I thought this might fix it but am unfortunately seeing the same error.

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.