From d071bf232db3b4b2a1756944e7253587f4861986 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Thu, 5 Mar 2020 21:39:03 +0530 Subject: [PATCH] feat: development docker --- .devcontainer/devcontainer.json | 12 +++ .devcontainer/docker-compose.yml | 30 ++++++++ .gitignore | 2 + .travis.yml | 13 +++- build/bench/Dockerfile | 58 +++++++++++++++ development/.vscode/launch.json | 22 ++++++ development/.vscode/settings.json | 4 + development/README.md | 118 ++++++++++++++++++++++++++++++ 8 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.yml create mode 100644 build/bench/Dockerfile create mode 100644 development/.vscode/launch.json create mode 100644 development/.vscode/settings.json create mode 100644 development/README.md diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..4bab99fc --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,12 @@ +{ + "name": "Frappe Bench", + "appPort": [8000, 9000, 6787], + "remoteUser": "frappe", + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + }, + "dockerComposeFile": "./docker-compose.yml", + "service": "frappe", + "workspaceFolder": "/workspace/development", + "shutdownAction": "stopCompose" +} diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 00000000..2d43b0c6 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,30 @@ +version: "3.7" +services: + mariadb: + image: mariadb:10.3 + environment: + - MYSQL_ROOT_PASSWORD=123 + - MYSQL_USER=root + volumes: + - ../installation/frappe-mariadb.cnf:/etc/mysql/conf.d/frappe.cnf + - mariadb-vol:/var/lib/mysql + + redis-cache: + image: redis:alpine + + redis-queue: + image: redis:alpine + + redis-socketio: + image: redis:alpine + + frappe: + image: frappe/bench:latest + command: sleep infinity + volumes: + - ..:/workspace:cached + ports: + - "8000:8000" + +volumes: + mariadb-vol: diff --git a/.gitignore b/.gitignore index 502c3673..cb373cae 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ # mounted volume sites + +development diff --git a/.travis.yml b/.travis.yml index 7042d7b0..d8d22dff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,12 +20,17 @@ after_success: matrix: include: - - name: "Build Frappe python environment (edge)" + - name: "Build Frappe bench development environment(latest)" if: branch = develop AND type != pull_request script: - - ./travis.py frappe --worker --tag edge - - ./travis.py frappe --worker --tag develop --tag-only - - name: "Build Frappe nginx + static assets (edge)" + - docker build -t frappe/bench:latest -f build/bench/Dockerfile . + - docker push frappe/bench:latest + - name: "Build Frappe python environment (edge)" + if: branch = develop AND type != pull_request + script: + - ./travis.py frappe --nginx --tag edge + - ./travis.py frappe --nginx --tag develop--tag-only + - name: "Build Frappe nginx + static assets (edge)" if: branch = develop AND type != pull_request script: - ./travis.py frappe --nginx --tag edge diff --git a/build/bench/Dockerfile b/build/bench/Dockerfile new file mode 100644 index 00000000..3f153bc7 --- /dev/null +++ b/build/bench/Dockerfile @@ -0,0 +1,58 @@ +# Frappe Bench Dockerfile + +FROM debian:9.6-slim +LABEL author=frappé + +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends locales \ + && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \ + && dpkg-reconfigure --frontend=noninteractive locales \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Set locale en_us.UTF-8 for mariadb and general locale data +ENV PYTHONIOENCODING=utf-8 +ENV LANGUAGE=en_US.UTF-8 +ENV LANG=en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 + +# Install all neccesary packages +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-suggests --no-install-recommends \ + build-essential cron curl git libffi-dev liblcms2-dev libldap2-dev libmariadbclient-dev libsasl2-dev libssl1.0-dev libtiff5-dev \ + libwebp-dev mariadb-client iputils-ping python3-dev python3-pip python3-setuptools python3-tk redis-tools rlwrap \ + software-properties-common sudo tk8.6-dev vim xfonts-75dpi xfonts-base wget wkhtmltopdf fonts-cantarell \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install nvm with node +ENV NVM_DIR /usr/local/nvm +ENV NODE_VERSION 12.16.1 +RUN mkdir /usr/local/nvm \ + && wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash \ + && . $NVM_DIR/nvm.sh \ + && nvm install $NODE_VERSION \ + && nvm install 10.19.0 \ + && nvm alias default $NODE_VERSION \ + && nvm use default \ + && npm install yarn -g +ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules +ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH + +# Install wkhtmltox correctly +RUN wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb +RUN dpkg -i wkhtmltox_0.12.5-1.stretch_amd64.deb && rm wkhtmltox_0.12.5-1.stretch_amd64.deb + + +# Add frappe user and setup sudo +RUN groupadd -g 1000 frappe \ + && useradd -ms /bin/bash -u 1000 -g 1000 -G sudo frappe \ + && chown -R 1000:1000 /home/frappe \ + && echo '. "$NVM_DIR/nvm.sh"' >> /home/frappe/.bashrc + +# Install bench +RUN pip3 install -e git+https://github.com/frappe/bench.git#egg=bench --no-cache + +USER frappe + +WORKDIR /home/frappe/frappe-bench + +EXPOSE 8000 9000 6787 + +VOLUME [ "/home/frappe/frappe-bench" ] diff --git a/development/.vscode/launch.json b/development/.vscode/launch.json new file mode 100644 index 00000000..21867b35 --- /dev/null +++ b/development/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Bench", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", + "args": [ + "frappe", "serve", "--port", "8000", "--noreload", "--nothreading" + ], + "pythonPath": "${workspaceFolder}/frappe-bench/env/bin/python", + "cwd": "${workspaceFolder}/frappe-bench/sites", + "env": { + "DEV_SERVER": "1" + } + } + ] +} \ No newline at end of file diff --git a/development/.vscode/settings.json b/development/.vscode/settings.json new file mode 100644 index 00000000..17a5d56d --- /dev/null +++ b/development/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.pythonPath": "frappe-bench/env/bin/python", + "debug.node.autoAttach": "on" +} \ No newline at end of file diff --git a/development/README.md b/development/README.md new file mode 100644 index 00000000..ba2aa311 --- /dev/null +++ b/development/README.md @@ -0,0 +1,118 @@ +# Getting Started + +## Prerequisites + +- Docker +- docker-compose +- user added to docker group + +### Bootstrap Containers for development + +Clone and change working directory to frappe_docker directory + +```shell +git clone https://github.com/frappe/frappe_docker.git +cd frappe_docker +``` + +#### Manually start containers + +```shell +docker-compose -f .devcontainer/docker-compose.yml up -d +``` + +Enter the bench container + +```shell +docker exec -e "TERM=xterm-256color" -w /workspace/development -it frappe bash +``` + +#### Use VSCode Remote Containers extension + +- Install Remote Development Pack / Remote Containers extension +- Install VSCode Python extension +- Open frappe_docker in VSCode +- From Command Palette (Ctrl + Shift + P) Execute Remote Containers : Reopen in Container + +### Setup Docker specific bench environment + +Notes: + +- `development` directory is ignored by it. It is mounted and available in container. Create all your benches inside this directory +- Execute these commands from container +- nvm with node v12 and v10 is installed. Check with `nvm ls`. Node v12 is default + +#### Setup first bench + +```shell +bench init --skip-redis-config-generation --frappe-branch version-12 frappe-bench +cd frappe-bench +``` + +#### Set hosts + +```shell +bench set-mariadb-host mariadb +bench set-redis-cache-host redis-cache:6379 +bench set-redis-queue-host redis-queue:6379 +bench set-redis-socketio-host redis-socketio:6379 +``` + +#### Changes related to bench start / honcho / Procfile + +- honcho/Procfile starts processes required for development. +- By default Procfile has 3 redis processes that it starts. Comment (`#`) or remove these lines and then run `bench start`. +- Another option is to run following command + +```shell +honcho start \ + web \ + socketio \ + watch \ + schedule \ + worker_short \ + worker_long \ + worker_default +``` + +#### Changes related to MariaDB + +Notes: + +- `bench new-site` command creates a user in mariadb with container IP as host +- After rebuilding container there is a chance that new bench container will not be able to access mariadb +- `'db_name'@'%'` needs to be set in mariadb and permission to the site database be given to the user +- Replace `db_name` and `db_password` from site's `site_config.json` +- MariaDB root password is 123 + +Enter mariadb shell + +```shell +mysql -uroot -p123 -hmariadb +``` + +Execute following queries + +```sql +UPDATE mysql.user SET Host = '%' where User = 'db_name'; FLUSH PRIVILEGES; +SET PASSWORD FOR 'db_name'@'%' = PASSWORD('db_password'); FLUSH PRIVILEGES; +GRANT ALL PRIVILEGES ON `db_name`.* TO 'db_name'@'%'; FLUSH PRIVILEGES; +``` + +### Visual Studio Code Python Debugging + +- Install VSCode Python Extension once in remote container +- Reload VSCode +- Do not start `web` process with honcho + +```shell +honcho start \ + socketio \ + watch \ + schedule \ + worker_short \ + worker_long \ + worker_default +``` + +- On debugger tab, Connect debugger. This will start the web process with debugger connected