Docker

432 views
Skip to first unread message

Manuele Pesenti

unread,
Aug 27, 2021, 4:06:46 AM8/27/21
to py4web
hi! just a quick question, I have still no experience with docker, I read in the documentation that python web apps can be dockerized but the example in the documentation is made with flask... Des anybody has experience with py4web? 
thanks a lot
Cheers
    Manuele

Maurizio Brilli

unread,
Aug 27, 2021, 6:27:03 AM8/27/21
to py4web
Hi Manuele.

I currently have a working instance of web2py in docker, while I had made a few experiments with py4web last year with no problems at all.

My configuration is as follows:

Dockerfile:
FROM ubuntu:latest
ARG user=py4web
ARG password=none
RUN apt update && apt install -y git python3 python3-pip libsasl2-dev python3-dev libldap2-dev libssl-dev memcached unixodbc unixodbc-dev
RUN service memcached restart
COPY ibm-iaccess-1.1.0.12-1.0.amd64.deb /
RUN dpkg --install /ibm-iaccess-1.1.0.12-1.0.amd64.deb && \
  rm -rf /ibm-iaccess-1.1.0.12-1.0.amd64.deb
COPY odbcinst.ini /etc/
COPY odbc.ini /etc/
RUN groupadd -r $user && useradd -m -r -g $user $user
RUN python3 -m pip install -U py4web pyodbc python-ldap
USER $user
COPY password.txt /home/$user/
RUN cd /home/$user/ && \
    if [ "$password" = "none" ]; then echo "no admin"; else py4web set_password < "$password"; fi
EXPOSE 8001
WORKDIR /home/$user/
CMD py4web run --password_file password.txt --host 0.0.0.0 --port 8001 apps


As you can see my configuration is slightly complicated by the fact that I am connecting to a legacy DB2 database on an IBM i system and to Windows Active Directory for LDAP authentication and authorization. I surely copied this Dockerfile from somewhere (though I cannot remember where it was, presumably py4web's github repository). It offers you two options about py4web's admin password: either setting the "password" env variable, or putting a "password.txt" file in the same directory as the Dockerfile. I don't know if last command line (the one that actually starts py4web) is still valid with the latest updates, but at the time it used to work.

Next step is running the container, which I do through docker-compose.

docker-compose.yaml
version: "3"
services:
  web:
    build: .
    ports:
      - "8001:8001"
    volumes:
       - ../py4web/apps:/home/py4web/apps
    environment:
     - PYDAL_URI=postgres://foo:bar@postgres:5432/baz
    stdin_open: true
    tty: true
    depends_on:
      - postgres

  postgres:
    restart: always
    image: postgres
    environment:
      - POSTGRES_USER=foo
      - POSTGRES_PASSWORD=bar
      - POSTGRES_DB=baz
      - POSTGRES_PORT=5432
    ports:
      - "5432:5432"
    volumes:
       - ../py4web/data/postgres:/var/lib/postgresql/data

In this case I also setup a Postgres database for other sorts of experiments I was working on. I also chose bind volumes so I could access the "apps" directory and the postgres database from the host's shell

Unfortunately lately (I mean in the last year or so) I've been too busy to carry on with porting my web2py app to py4web, but I can tell you I have had no particular issues in running both on docker.

Ciao
Maurizio

Manuele Pesenti

unread,
Aug 27, 2021, 6:42:45 AM8/27/21
to py4web
great news! Thanks Maurizio for sharing your experience! ☺️
Ciao
    Manuele

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/8773cc11-939e-4424-aeb2-c9003d98655cn%40googlegroups.com.

Massimo DiPierro

unread,
Aug 29, 2021, 12:20:39 PM8/29/21
to Manuele Pesenti, py4web
Look into this

Also look into podman it is syntax compatible with docker but much better. Does not need a demon running.

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.

Francesco Bursi

unread,
Oct 6, 2021, 10:56:30 AM10/6/21
to py4web
Hi to everyone and thank you for your work and for the insights about docker. 
I'm a newbie in both Docker and Py4web, I simply tried to run:

docker-compose up -d

with this (https://github.com/web2py/py4web/tree/master/deployment_tools/docker) set up and I'm ending up with this:

web_1       | Traceback (most recent call last):
web_1       |   File "/usr/local/bin/py4web", line 5, in <module>
web_1       |     from py4web.core import cli
web_1       |   File "/usr/local/lib/python3.8/dist-packages/py4web/__init__.py", line 17, in <module>
web_1       |     from .core import (
web_1       |   File "/usr/local/lib/python3.8/dist-packages/py4web/core.py", line 26, in <module>
web_1       |     import rocket3
web_1       | ModuleNotFoundError: No module named 'rocket3'

Probably I'm doing some trivial errors, but I can't find anything at first sight.
Another question, but this is just curiosity, is there a reason for using an Ubuntu  as base image instead of something lighter (as alpine)? 


Thank you in advance anyway.
Francesco

Massimo

unread,
Oct 6, 2021, 11:02:48 AM10/6/21
to py4web
please try again. I had messed up the last setup.py

Francesco Bursi

unread,
Oct 6, 2021, 11:35:41 AM10/6/21
to py4web
Maybe I didn't understand, I tryed another time after deleting the images, but the result and the message is the same. Yes, I saw that some some changes involving rocket3 were made in the last few commits.

Thanks,
Francesco

zejd...@gmail.com

unread,
Oct 30, 2021, 6:14:28 AM10/30/21
to py4web
Hi, I have a setup for developing dockerized py4web apps in VS Code. Had to figure out some volume and file permission related issues, etc. Now I can edit the sources both in directories on the host machine as well as directly within the containers, whatever is more convenient. VS Code debugger is working directly within the containers - breakpoints, console messages displayed in the debug console of VS Code, etc. Some of my projects are composed of several dockerized py4web microservices with independent databases if needed, connected via APIs, most microservices session-less. If you find it useful, I might try to summarize the key steps and configs.

David

zejd...@gmail.com

unread,
Dec 5, 2021, 6:10:16 PM12/5/21
to py4web
Hi, just in the case you would find the configs useful.

Notes:
  • The base image py4web_light. Individual projects on top of that.
  • Developing on Linux, user with uid 1000 & gid 1000 on the local machine => same uid and gid for py4web user in the docker images to avoid permission issues with bind-style volumes.
  • Debugging in the "attach" debugpy mode which works also within containers.
  • VS Code extensions: Docker, Jupyter, Pylance, Python, Remote - Conteiners, Remote - SSH (the last one for debugging remotely deployed containers).
  • Examples app is retained as a handy source of inspiration.
  • Troubles with newer versions gevent (debuger was not stopping on breakpoints) => avoid GEVENT_SUPPORT=True.
  • I have a customized scaffolding app which loads the .env variables, includes custom modules shared among more projects, scripts for pushing images to docker repositories, other deployment-related stuff such as SSL sidecar https://github.com/jwulf/letsencrypt-nginx-sidecar.git, etc. Not included below - goes beyond the basic py4web+Docker+VSCode+debugpy functionality.

./py4web_light/Dockerfile

FROM ubuntu:latest
ENV TZ=Europe/Prague
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get install -y python3 python3-pip
ARG user=py4web uid=1000 gid=1000
RUN groupadd --system --gid $gid $user && useradd --create-home --system --gid $gid --uid $uid $user && usermod -a -G $user $user
USER $user
ENV PATH="/home/py4web/.local/bin:${PATH}"
RUN python3 -m pip install -U py4web psycopg2-binary bottle
# psycopg2-binary for Postgres
WORKDIR /home/$user/
RUN py4web setup --yes apps && rm -rf apps/_minimal; rm -rf apps/_default; rm -rf apps/_scaffold; rm -rf apps/_documentation
EXPOSE 8000

./app_name/Dockerfile

FROM py4web_light:latest
USER root
ARG app=app_name
ARG user=py4web
USER $user
# support for debugging (remove in production Dockerfile)
RUN python3 -m pip install debugpy
EXPOSE 5678
# app sources
USER $user
WORKDIR /home/$user/
COPY --chown=$user:$user ./files_copy .
RUN mkdir apps/$app && mkdir apps/$app/databases && mkdir apps/$app/uploads && chown $user apps/$app apps/$app/databases apps/$app/uploads
CMD ./mypy4web.py run --password_file /home/py4web/encrypted_password.txt --host 0.0.0.0 --port 8000 --watch=lazy apps

./app_name/mypy4web.py

#!/usr/bin/python3
import debugpy
from py4web.core import cli
print("Debugpy launched ...")
debugpy.listen(("0.0.0.0", 5678))
print("Py4web starting ...")
cli()

./app_name/docker_compose.yml

version: "3.4"

services:

  app_name:
    image: ${APP_NAME}:latest
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - "127.0.0.1:${APP_EXP_PORT}:8000"
    env_file:
      - .env

    stdin_open: true
    tty: true
    depends_on:
      - postgres
    volumes:
      - app_source_vol:/home/py4web/apps/${APP_NAME}
      - app_vscode_vol:/home/py4web/.vscode
      - app_db_vol:/home/py4web/apps/${APP_NAME}/databases
      - app_upload_vol:/home/py4web/apps/${APP_NAME}/uploads
      # todo: logs

  postgres:
    #restart: always
    image: postgres
    env_file:
      - .env
    ports:
      - "127.0.0.1:${POSTGRES_EXP_PORT}:5432"
    volumes:
      - postgresvol:/var/lib/postgresql/data

volumes:
  postgresvol:
    driver: local
  app_source_vol:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${BASEDIR}/${APP_NAME}/files_src
  app_vscode_vol:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${BASEDIR}/${APP_NAME}/.vscode_remote
  app_db_vol:
    driver: local
  app_upload_vol:
    driver: local
   
./app_name/.env

APP_NAME=app_name
APP_EXP_PORT=9901
APP_SESSION_SECRET_KEY=48fg7aaf-adac-11eb-b3d6-ffa44fa48733
POSTGRES_EXP_PORT=5901
POSTGRES_DB=app_name
APP_PYDAL_URI=postgres://db_user:db_pwd@postgres:5432/app_name

#appsettings
APP_USE_AUTH=False
APP_DB_POOL_SIZE=1
APP_DB_MIGRATE=True
APP_DB_FAKE_MIGRATE=False
APP_VERIFY_EMAIL=True
APP_REQUIRES_APPROVAL=False
APP_DEFAULT_LOGIN_ENABLED=False
APP_OAUTH2GOOGLE_CLIENT_ID=None
APP_OAUTH2GOOGLE_CLIENT_SECRET=None
APP_OAUTH2OKTA_CLIENT_ID=None
APP_OAUTH2OKTA_CLIENT_SECRET=None
APP_OAUTH2FACEBOOK_CLIENT_ID=None
APP_OAUTH2FACEBOOK_CLIENT_SECRET=None
APP_SESSION_TYPE=None
#APP_SESSION_TYPE = cookies / redis / memcache

#postgres
POSTGRES_USER=db_user
POSTGRES_PASSWORD=db_pwd
POSTGRES_PORT=5432

#main
SMTP_SERVER=smtp.domain.xyz:25
SMTP_LOGIN=mail_user:mail_pwd
SMTP_SENDER=ad...@domain.xyz
SMTP_TLS=False
SMTP_SSL=False
BASEDIR=/path/to/sources

./app_name/.vscode_remote/launch.json

{
    "version": "0.2.0",
    "configurations": [       
        {
            "name": "Python: Remote Attach via 5678",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "127.0.0.1",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/home/py4web"
                }
            ]
        },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
    ]
}

David

nmacneil

unread,
Sep 20, 2022, 2:00:02 AM9/20/22
to py4web
I've setup an example py4web stateless docker application that can be cloned and used as required. 

It's contains (full information contained in the repo README):
  1. Table structures and uploads stored directly in the database,
  2. New database upload functionality to ensure database uploads do not impact general performance for the primary table containing the upload field.
  3. Ready to run py4web docker application and corresponding database in just a few steps.
    • The docker container is also pre-setup/ready for debugging.

Hopefully this helps others avoid the headaches I had working through setting up a stateless docker application https://github.com/macneiln/docker-py4web-stateless-example.

- Nathan

Massimo DiPierro

unread,
Sep 20, 2022, 2:03:38 AM9/20/22
to zejd...@gmail.com, py4web
this is fantastic. should go into the docs and the repo.

Reply all
Reply to author
Forward
0 new messages