2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-11-08 14:21:05 +00:00
frappe_docker/README.md

490 lines
18 KiB
Markdown
Raw Normal View History

2020-04-19 00:18:21 +00:00
| Develop | [![Build Status](https://travis-ci.com/frappe/frappe_docker.svg?branch=master)](https://travis-ci.com/frappe/frappe_docker) |
|---------|-----------------------------------------------------------------------------------------------------------------------------|
| Master | [![Build Status](https://travis-ci.com/frappe/frappe_docker.svg?branch=develop)](https://travis-ci.com/frappe/frappe_docker) |
2020-03-05 16:39:32 +00:00
## Getting Started
2017-07-18 09:26:31 +00:00
The templates in this repository will help deploy Frappe/ERPNext docker in a production environment.
This docker installation takes care of the following:
2017-07-18 09:26:31 +00:00
* Setting up the desired version of Frappe/ERPNext.
* Setting up all the system requirements: eg. MariaDB, Node, Redis.
* [OPTIONAL] Configuring networking for remote access and setting up LetsEncrypt
2017-07-18 09:26:31 +00:00
2020-03-08 20:29:17 +00:00
For docker based development refer to this [README](development/README.md)
2020-03-05 16:39:32 +00:00
## Deployment
2020-03-05 16:39:32 +00:00
### Setting up Pre-requisites
This repository requires Docker and Git to be setup on the instance to be used.
2020-03-05 16:39:32 +00:00
### Cloning the repository and preliminary steps
Clone this repository somewhere in your system:
```sh
git clone https://github.com/frappe/frappe_docker.git
cd frappe_docker
```
Copy the example docker environment file to `.env`:
2020-03-05 16:39:32 +00:00
```sh
cp installation/env-example installation/.env
```
2020-03-05 16:39:32 +00:00
### Setup Environment Variables
To get started, copy the existing `env-example` file to `.env` inside the `installation` directory. By default, the file will contain the following variables:
- `VERSION=edge`
- In this case, `edge` corresponds to `develop`. To setup any other version, you may use the branch name or version specific tags. (eg. version-12, v11.1.15, v11)
- `MYSQL_ROOT_PASSWORD=admin`
2020-03-08 20:29:17 +00:00
- Bootstraps a MariaDB container with this value set as the root password. If a managed MariaDB instance is used, there is no need to set the password here.
- `MARIADB_HOST=mariadb`
2020-03-08 20:29:17 +00:00
- Sets the hostname to `mariadb`. This is required if the database is managed by the containerized MariaDB instance.
- In case of a separately managed database setups, set the value to the database's hostname/IP/domain.
- `SITES=site1.domain.com,site2.domain.com`
2020-03-08 20:29:17 +00:00
- List of sites that are part of the deployment "bench" Each site is separated by a comma(,).
- If LetsEncrypt is being setup, make sure that the DNS for all the site's domains correctly point to the current instance.
- `LETSENCRYPT_EMAIL=your.email@your.domain.com`
2020-03-05 16:39:32 +00:00
- Email for LetsEncrypt expiry notification. This is only required if you are setting up LetsEncrypt.
### Local deployment for testing
2020-03-05 16:39:32 +00:00
For trying out locally or to develop apps using ERPNext REST API port 80 must be published.
2020-03-08 20:29:17 +00:00
The first command will start the containers; the second command will publish the port of the *-nginx container.
2020-03-05 16:39:32 +00:00
2020-03-06 01:15:54 +00:00
For Erpnext:
2020-03-05 16:39:32 +00:00
```sh
2020-03-06 01:15:54 +00:00
# Start services
docker-compose \
2020-03-05 16:39:32 +00:00
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
2020-03-06 01:15:54 +00:00
--project-directory installation up -d
# Publish port
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
--project-directory installation run --publish 80:80 -d erpnext-nginx
2020-03-05 16:39:32 +00:00
```
For Frappe:
2020-03-06 01:15:54 +00:00
2020-03-05 16:39:32 +00:00
```sh
2020-03-06 01:15:54 +00:00
# Start services
docker-compose \
2020-03-05 16:39:32 +00:00
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-frappe.yml \
2020-03-06 01:15:54 +00:00
--project-directory installation up -d
# Publish port
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-frappe.yml \
--project-directory installation run --publish 80:80 -d frappe-nginx
2020-03-05 16:39:32 +00:00
```
Make sure to replace `<project-name>` with the desired name you wish to set for the project.
2020-03-05 16:39:32 +00:00
Notes:
2020-03-08 20:29:17 +00:00
- New site (first site) needs to be added after starting the services.
2020-03-08 20:29:17 +00:00
- The local deployment is for testing and REST API development purpose only
- A complete development environment is available [here](Development/README.md)
- The site names are limited to patterns matching \*.localhost by default
- Additional site name patterns can be added by editing /etc/hosts of your host machine
2020-03-05 16:39:32 +00:00
### Deployment for production
#### Setup Letsencrypt Nginx Proxy Companion
2020-03-06 01:15:54 +00:00
Letsencrypt Nginx Proxy Companion can optionally be setup to provide SSL. This is recommended for instances accessed over the internet.
2020-03-05 16:39:32 +00:00
2020-03-08 20:29:17 +00:00
Your DNS will need to be configured correctly for Letsencrypt to verify your domain.
2020-03-05 16:39:32 +00:00
To setup the proxy companion, run the following commands:
```sh
cd $HOME
git clone https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion.git
cd docker-compose-letsencrypt-nginx-proxy-companion
cp .env.sample .env
./start.sh
```
For more details, see the [Letsencrypt Nginx Proxy Companion github repo](https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion). Letsencrypt Nginx Proxy Companion github repo works by automatically proxying to containers with the `VIRTUAL_HOST` environmental variable.
#### Start Frappe/ERPNext Services
2017-07-18 09:26:31 +00:00
2020-03-05 16:39:32 +00:00
To start the Frappe/ERPNext services for production, run the following command:
2017-07-18 09:26:31 +00:00
```sh
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
-f installation/docker-compose-networks.yml \
--project-directory installation up -d
```
2017-07-18 09:26:31 +00:00
Make sure to replace `<project-name>` with any desired name you wish to set for the project.
Notes:
- Use `docker-compose-frappe.yml` in case you need only Frappe without ERPNext.
- New site (first site) needs to be added after starting the services.
2020-03-05 16:39:32 +00:00
2020-04-22 09:11:15 +00:00
#### Using Amazon RDS (or any other DBaaS)
To configure usage of RDS, `common_site_config.json` in your `sites-vol` volume has to be edited using:
```sh
docker run \
-it \
-v <project-name>_sites-vol:/sites \
alpine vi /sites/common_site_config.json
```
Instead of `alpine` you can use any image you like.
For full instructions, refer to the [wiki](https://github.com/frappe/frappe/wiki/Using-Frappe-with-Amazon-RDS-(or-any-other-DBaaS). Common question can be found in Issues and on forum.
2020-03-05 16:39:32 +00:00
### Docker containers
2020-03-08 20:29:17 +00:00
This repository contains the following docker-compose files, each one containing the described images:
2020-03-05 16:39:32 +00:00
* docker-compose-common.yml
* redis-cache
* volume: redis-cache-vol
* redis-queue
* volume: redis-queue-vol
* redis-socketio
* volume: redis-socketio-vol
* mariadb: main database
* volume: mariadb-vol
* docker-compose-erpnext.yml
2020-03-06 01:15:54 +00:00
* erpnext-nginx: serves static assets and proxies web request to the appropriate container, allowing to offer all services on the same port.
2020-04-20 09:05:51 +00:00
* volume: assets-vol
2020-03-05 16:39:32 +00:00
* erpnext-python: main application code
2020-03-06 01:15:54 +00:00
* frappe-socketio: enables realtime communication to the user interface through websockets
2020-03-05 16:39:32 +00:00
* frappe-worker-default: background runner
* frappe-worker-short: background runner for short-running jobs
* frappe-worker-long: background runner for long-running jobs
* frappe-schedule
* docker-compose-frappe.yml
2020-03-06 01:15:54 +00:00
* frappe-nginx: serves static assets and proxies web request to the appropriate container, allowing to offer all services on the same port.
2020-04-20 09:05:51 +00:00
* volume: assets-vol, sites-vol
2020-03-05 16:39:32 +00:00
* erpnext-python: main application code
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-06 01:15:54 +00:00
* frappe-socketio: enables realtime communication to the user interface through websockets
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-05 16:39:32 +00:00
* frappe-worker-default: background runner
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-05 16:39:32 +00:00
* frappe-worker-short: background runner for short-running jobs
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-05 16:39:32 +00:00
* frappe-worker-long: background runner for long-running jobs
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-05 16:39:32 +00:00
* frappe-schedule
2020-04-20 09:05:51 +00:00
* volume: sites-vol
2020-03-05 16:39:32 +00:00
2020-03-08 20:29:17 +00:00
* docker-compose-networks.yml: this yaml define the network to communicate with *Letsencrypt Nginx Proxy Companion*.
2020-03-05 16:39:32 +00:00
### Site operations
2017-07-18 09:26:31 +00:00
#### Setup New Sites
Note:
2020-03-08 20:29:17 +00:00
- Wait for the MariaDB service to start before trying to create a new site.
- If new site creation fails, retry after the MariaDB container is up and running.
- If you're using a managed database instance, make sure that the database is running before setting up a new site.
- Use `.env` file or environment variables instead of passing secrets as command arguments.
```sh
# Create ERPNext site
docker exec -it \
-e "SITE_NAME=$SITE_NAME" \
-e "DB_ROOT_USER=$DB_ROOT_USER" \
-e "MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD" \
-e "ADMIN_PASSWORD=$ADMIN_PASSWORD" \
-e "INSTALL_APPS=erpnext" \
<project-name>_erpnext-python_1 docker-entrypoint.sh new
```
Environment Variables needed:
- `SITE_NAME`: name of the new site to create.
- `DB_ROOT_USER`: MariaDB Root user.
2020-03-08 20:29:17 +00:00
- `MYSQL_ROOT_PASSWORD`: In case of the MariaDB docker container use the one set in `MYSQL_ROOT_PASSWORD` in previous steps. In case of a managed database use the appropriate password.
- `MYSQL_ROOT_PASSWORD_FILE` - When the MariaDB root password is stored using docker secrets.
2020-03-08 20:29:17 +00:00
- `ADMIN_PASSWORD`: set the administrator password for the new site.
- `ADMIN_PASSWORD_FILE`: set the administrator password for the new site using docker secrets.
- `INSTALL_APPS=erpnext`: available only in erpnext-worker and erpnext containers (or other containers with custom apps). Installs ERPNext (and/or the specified apps, comma-delinieated) on this new site.
2020-03-08 20:29:17 +00:00
- `FORCE=1`: optional variable which force installation of the same site.
#### Backup Sites
Environment Variables
2020-03-08 20:29:17 +00:00
- `SITES` is list of sites separated by `:` colon to migrate. e.g. `SITES=site1.domain.com` or `SITES=site1.domain.com:site2.domain.com` By default all sites in bench will be backed up.
- `WITH_FILES` if set to 1, it will backup user-uploaded files.
- By default `backup` takes mariadb dump and gzips it. Example file, `20200325_221230-test_localhost-database.sql.gz`
- If `WITH_FILES` is set then it will also backup public and private files of each site as uncompressed tarball. Example files, `20200325_221230-test_localhost-files.tar` and `20200325_221230-test_localhost-private-files.tar`
2020-04-20 09:11:38 +00:00
- All the files generated by backup are placed at volume location `sites-vol:/{site-name}/private/backups/*`
```sh
docker exec -it \
-e "SITES=site1.domain.com:site2.domain.com" \
-e "WITH_FILES=1" \
<project-name>_erpnext-python_1 docker-entrypoint.sh backup
```
2020-04-20 09:11:38 +00:00
The backup will be available in the `sites-vol` volume.
#### Push backup to s3 compatible storage
Environment Variables
- `BUCKET_NAME`, Required to set bucket created on S3 compatible storage.
- `ACCESS_KEY_ID`, Required to set access key.
- `SECRET_ACCESS_KEY`, Required to set secret access key.
- `ENDPOINT_URL`, Required to set URL of S3 compatible storage.
- `BUCKET_DIR`, Required to set directory in bucket where sites from this deployment will be backed up.
- `BACKUP_LIMIT`, Optionally set this to limit number of backups in bucket directory. Defaults to 3.
```sh
docker run \
-e "BUCKET_NAME=backups" \
-e "ACCESS_KEY_ID=access_id_from_provider" \
-e "SECRET_ACCESS_KEY=secret_access_from_provider" \
-e "ENDPOINT_URL=https://region.storage-provider.com" \
-e "BUCKET_DIR=frappe-bench-v12" \
2020-04-22 08:53:37 +00:00
-v <project-name>_sites-vol:/home/frappe/frappe-bench/sites \
--network <project-name>_default \
frappe/frappe-worker:v12 push-backup
```
Note:
- Above example will backup files in bucket called `backup` at location `frappe-bench-v12/site.name.com/DATE_TIME/DATE_TIME-site_name_com-{filetype}.{extension}`,
- example DATE_TIME: 20200325_042020.
- example filetype: database, files or private-files
- example extension: sql.gz or tar
#### Updating and Migrating Sites
Switch to the root of the `frappe_docker` directory before running the following commands:
```sh
# Update environment variable VERSION
nano .env
# Pull new images
docker-compose \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
pull
# Restart containers
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
-f installation/docker-compose-networks.yml \
--project-directory installation up -d
docker exec -it \
-e "MAINTENANCE_MODE=1" \
<project-name>_erpnext-python_1 docker-entrypoint.sh migrate
```
#### Restore backups
Environment Variables
- `MYSQL_ROOT_PASSWORD` or `MYSQL_ROOT_PASSWORD_FILE`(when using docker secrets), Required to restore mariadb backups.
- `BUCKET_NAME`, Required to set bucket created on S3 compatible storage.
- `ACCESS_KEY_ID`, Required to set access key.
- `SECRET_ACCESS_KEY`, Required to set secret access key.
- `ENDPOINT_URL`, Required to set URL of S3 compatible storage.
- `BUCKET_DIR`, Required to set directory in bucket where sites from this deployment will be backed up.
```sh
docker run \
-e "MYSQL_ROOT_PASSWORD=admin" \
-e "BUCKET_NAME=backups" \
-e "ACCESS_KEY_ID=access_id_from_provider" \
-e "SECRET_ACCESS_KEY=secret_access_from_provider" \
-e "ENDPOINT_URL=https://region.storage-provider.com" \
-e "BUCKET_DIR=frappe-bench-v12" \
2020-04-22 08:53:37 +00:00
-v <project-name>_sites-vol:/home/frappe/frappe-bench/sites \
-v ./backups:/home/frappe/backups \
--network <project-name>_default \
frappe/frappe-worker:v12 restore-backup
```
Note:
- Volume must be mounted at location `/home/frappe/backups` for restoring sites
- If no backup files are found in volume, it will use s3 credentials to pull backups
- Backup structure for mounted volume or downloaded from s3:
- /home/frappe/backups
- site1.domain.com
- 20200420_162000
- 20200420_162000-site1_domain_com-*
- site2.domain.com
- 20200420_162000
- 20200420_162000-site2_domain_com-*
2020-03-06 06:12:11 +00:00
### Custom apps
2020-03-08 20:29:17 +00:00
To add your own Frappe/ERPNext apps to the image, we'll need to create a custom image with the help of a unique wrapper script
2020-03-06 06:12:11 +00:00
> For the sake of simplicity, in this example, we'll be using a place holder called `[custom]`, and we'll be building off the edge image.
2020-03-06 06:12:11 +00:00
Create two directories called `[custom]-worker` and `[custom]-nginx` in the `build` directory.
2020-03-06 06:12:11 +00:00
```shell
cd frappe_docker
mkdir ./build/[custom]-worker ./build/[custom]-nginx
```
2020-03-06 06:12:11 +00:00
Create a `Dockerfile` in `./build/[custom]-worker` with the following content:
2020-03-06 06:12:11 +00:00
```Dockerfile
FROM frappe/erpnext-worker:edge
2020-03-06 06:12:11 +00:00
RUN install_app [custom] https://github.com/[username]/[custom] [branch]
# Only add the branch if you are using a specific tag or branch.
```
2020-03-06 06:12:11 +00:00
Create a `Dockerfile` in `./build/[custom]-nginx` with the following content:
2020-03-06 06:12:11 +00:00
```Dockerfile
FROM bitnami/node:12-prod
2020-03-06 06:12:11 +00:00
COPY build/[custom]-nginx/install_app.sh /install_app
2020-03-06 06:12:11 +00:00
RUN /install_app [custom] https://github.com/[username]/[custom]
2020-03-06 06:12:11 +00:00
FROM frappe/erpnext-nginx:edge
2020-03-06 06:12:11 +00:00
COPY --from=0 /home/frappe/frappe-bench/sites/ /var/www/html/
COPY --from=0 /rsync /rsync
RUN echo -n "\n[custom]" >> /var/www/html/apps.txt
2020-03-06 06:12:11 +00:00
VOLUME [ "/assets" ]
2020-03-06 06:12:11 +00:00
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
```
2020-03-06 06:12:11 +00:00
Copy over the `install_app.sh` file from `./build/erpnext-nginx`
2020-03-06 06:12:11 +00:00
```shell
cp ./build/erpnext-nginx/install.sh ./build/[custom]-nginx
```
2020-03-06 06:12:11 +00:00
Open up `./installation/docker-compose-custom.yml` and replace all instances of `[app]` with the name of your app.
2020-03-06 06:12:11 +00:00
```shell
sed -i "s#\[app\]#[custom]#" ./installation/docker-compose-custom.yml
```
2020-03-06 06:12:11 +00:00
2020-03-08 20:29:17 +00:00
Install like usual, except that when you set the `INSTALL_APPS` variable to `erpnext,[custom]`.
2020-03-06 06:12:11 +00:00
2020-03-05 16:39:32 +00:00
## Troubleshoot
### Failed migration after image upgrade
Issue: After upgrade of the containers, the automatic migration fails.
Solution: Remove containers and volumes, and clear redis cache:
```shell
# change to repo root
cd $HOME/frappe_docker
# Stop all bench containers
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
-f installation/docker-compose-networks.yml \
--project-directory installation stop
# Remove redis containers
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
-f installation/docker-compose-networks.yml \
--project-directory installation rm redis-cache redis-queue redis-socketio
# Clean redis volumes
docker volume rm \
<project-name>_redis-cache-vol \
<project-name>_redis-queue-vol \
<project-name>_redis-socketio-vol
# Restart project
docker-compose \
--project-name <project-name> \
-f installation/docker-compose-common.yml \
-f installation/docker-compose-erpnext.yml \
-f installation/docker-compose-networks.yml \
--project-directory installation up -d
```
### ValueError: There exists an active worker named XXX already
Issue: You have the following error during container restart
```
frappe-worker-short_1 | Traceback (most recent call last):
frappe-worker-short_1 | File "/home/frappe/frappe-bench/commands/worker.py", line 5, in <module>
frappe-worker-short_1 | start_worker(queue, False)
frappe-worker-short_1 | File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 147, in start_worker
frappe-worker-short_1 | Worker(queues, name=get_worker_name(queue)).work(logging_level = logging_level)
frappe-worker-short_1 | File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/rq/worker.py", line 474, in work
frappe-worker-short_1 | self.register_birth()
frappe-worker-short_1 | File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/rq/worker.py", line 261, in register_birth
frappe-worker-short_1 | raise ValueError(msg.format(self.name))
frappe-worker-short_1 | ValueError: There exists an active worker named '8dfe5c234085.10.short' already
```
Solution: Clear redis cache using `docker exec` command (take care of replacing `<project-name>` accordingly):
```sh
# Clear the cache which is causing problem.
docker exec -it <project-name>_redis-cache_1 redis-cli FLUSHALL
docker exec -it <project-name>_redis-queue_1 redis-cli FLUSHALL
docker exec -it <project-name>_redis-socketio_1 redis-cli FLUSHALL
```
2020-03-08 20:29:17 +00:00
Note: Environment variables from `.env` file located at the current working directory will be used.
## Development
This repository includes a complete setup to develop with Frappe/ERPNext and Bench, Including the following features:
- VSCode containers integration
- VSCode Python debugger
- Pre-configured Docker containers for an easy start
A complete Readme is available in [development/README.md](development/README.md)