docker-ckan/README.md

171 lines
7.7 KiB
Markdown
Raw Normal View History

2022-07-18 10:48:08 +02:00
# Docker Compose setup for CKAN
* [Overview](#overview)
* [Quick start](#quick-start)
* [Development mode](#development-mode)
* [Create an extension](#create-an-extension)
* [CKAN images](#ckan-images)
* [Extending the base images](#extending-the-base-images)
* [Applying patches](#applying-patches)
2022-07-19 16:09:55 +02:00
* [Debugging with pdb](#pdb)
2022-08-16 14:02:49 +02:00
* [NGINX](#nginx)
2022-07-18 10:48:08 +02:00
* [Known Issues](#known-issues)
## Overview
2022-07-18 11:02:42 +02:00
This is a set of configuration and setup files to run a CKAN site.
2022-07-18 10:48:08 +02:00
2022-07-26 15:56:43 +02:00
The CKAN images used are from the official CKAN [ckan-docker](https://github.com/ckan/ckan-docker-base) repo
2022-07-18 10:48:08 +02:00
2022-07-18 11:02:42 +02:00
The non-CKAN images are as follows:
2022-07-18 10:48:08 +02:00
2022-12-12 10:37:08 +01:00
* DataPusher: CKAN's [pre-configured DataPusher image](https://github.com/ckan/ckan-base/tree/main/datapusher).
2022-07-18 10:48:08 +02:00
* PostgreSQL: Official PostgreSQL image. Database files are stored in a named volume.
* Solr: CKAN's [pre-configured Solr image](https://github.com/ckan/ckan-solr). Index data is stored in a named volume.
* Redis: standard Redis image
2022-12-12 10:37:08 +01:00
* NGINX: latest stable nginx image that includes SSL and Non-SSL endpoints
2022-07-18 10:48:08 +02:00
2022-12-12 10:37:08 +01:00
The site is configured using environment variables that you can set in the `.env` file.
2022-07-18 10:48:08 +02:00
## Quick start
2022-12-12 10:37:08 +01:00
Copy the included `.env.example` and rename it to `.env` - modify it depending on your own needs.
2022-07-18 10:48:08 +02:00
2022-07-18 14:51:17 +02:00
Using the default values on the `.env.example` file will get you a working CKAN instance. There is a sysadmin user created by default with the values defined in `CKAN_SYSADMIN_NAME` and `CKAN_SYSADMIN_PASSWORD`(`ckan_admin` and `test1234` by default). This should be obviously changed before running this setup as a public CKAN instance.
2022-07-18 10:48:08 +02:00
To build the images:
docker-compose build
To start the containers:
docker-compose up
2022-12-12 10:37:08 +01:00
At the end of the container start sequence there should be 6 containers running
2022-12-12 10:38:58 +01:00
![Screenshot 2022-12-12 at 10 36 21 am](https://user-images.githubusercontent.com/54408245/207012236-f9571baa-4d99-4ffe-bd93-30b11c4829e0.png)
2022-07-18 10:48:08 +02:00
## Development mode
To develop local extensions use the `docker-compose.dev.yml` file:
To build the images:
docker-compose -f docker-compose.dev.yml build
To start the containers:
docker-compose -f docker-compose.dev.yml up
See [CKAN Images](#ckan-images) for more details of what happens when using development mode.
### Create an extension
2022-07-18 14:51:17 +02:00
You can use the ckan [extension](https://docs.ckan.org/en/latest/extensions/tutorial.html#creating-a-new-extension) instructions to create a CKAN extension, only executing the command inside the CKAN container and setting the mounted `src/` folder as output:
2022-07-18 10:48:08 +02:00
docker-compose -f docker-compose.dev.yml exec ckan-dev /bin/bash -c "ckan generate extension --output-dir /srv/app/src_extensions"
2022-07-18 14:51:17 +02:00
The new extension files and directories will be created in the `src/` folder. You might need to change the owner of its folder to have the appropiate permissions.
2022-07-18 10:48:08 +02:00
## CKAN images
2022-12-12 13:19:51 +01:00
![ckan images](https://user-images.githubusercontent.com/54408245/207043322-1f7eb16b-1006-4664-a87b-11320a3c94c8.png)
2022-07-18 10:48:08 +02:00
2022-12-12 16:02:05 +01:00
The Docker image config files used to build your CKAN project are located in the `ckan/` folder. There are two Docker files:
2022-07-18 10:48:08 +02:00
2022-07-18 14:28:50 +02:00
* `Dockerfile`: this is based on `ckan/ckan-base:<version>`, a base image located in the DockerHub repository, that has CKAN installed along with all its dependencies, properly configured and running on [uWSGI](https://uwsgi-docs.readthedocs.io/en/latest/) (production setup)
* `Dockerfile.dev`: this is based on `ckan/ckan-base:<version>-dev` also located located in the DockerHub repository, and extends `ckan/ckan-base:<version>` to include:
2022-07-18 10:48:08 +02:00
2022-12-12 16:02:05 +01:00
* Any extension cloned on the `src` folder will be installed in the CKAN container when booting up Docker Compose (`docker-compose up`). This includes installing any requirements listed in a `requirements.txt` (or `pip-requirements.txt`) file and running `python setup.py develop`.
* CKAN is started running this: `/usr/bin/ckan -c /srv/app/ckan.ini run -H 0.0.0.0`.
2022-07-18 10:48:08 +02:00
* Make sure to add the local plugins to the `CKAN__PLUGINS` env var in the `.env` file.
### Extending the base images
2022-12-12 16:02:05 +01:00
You can modify the docker files to build your own customized image tailored to your project, installing any extensions and extra requirements needed. Here is where you would update to build a different CKAN base image ie: CKAN 2.9.7
2022-07-18 14:28:50 +02:00
To perform extra initialization steps you can add scripts to your custom images and copy them to the `/docker-entrypoint.d` folder (The folder should be created for you when you build the image). Any `*.sh` and `*.py` file in that folder will be executed just after the main initialization script ([`prerun.py`](https://github.com/ckan/ckan-docker-base/blob/main/ckan-2.9/base/setup/prerun.py)) is executed and just before the web server and supervisor processes are started.
2022-07-18 10:48:08 +02:00
For instance, consider the following custom image:
```
ckan
├── docker-entrypoint.d
│ └── setup_validation.sh
├── Dockerfile
└── Dockerfile.dev
```
We want to install an extension like [ckanext-validation](https://github.com/frictionlessdata/ckanext-validation) that needs to create database tables on startup time. We create a `setup_validation.sh` script in a `docker-entrypoint.d` folder with the necessary commands:
```bash
#!/bin/bash
# Create DB tables if not there
2022-07-18 14:51:17 +02:00
ckan -c /srv/app/ckan.ini validation init-db
2022-07-18 10:48:08 +02:00
```
2022-07-18 14:28:50 +02:00
And then in our `Dockerfile.dev` file we install the extension and copy the initialization scripts:
2022-07-18 10:48:08 +02:00
```Dockerfile
2022-12-12 16:02:05 +01:00
FROM ckan/ckan-base:2.9.7-dev
2022-07-18 10:48:08 +02:00
RUN pip install -e git+https://github.com/frictionlessdata/ckanext-validation.git#egg=ckanext-validation && \
pip install -r https://raw.githubusercontent.com/frictionlessdata/ckanext-validation/master/requirements.txt
COPY docker-entrypoint.d/* /docker-entrypoint.d/
```
### Applying patches
When building your project specific CKAN images (the ones defined in the `ckan/` folder), you can apply patches
to CKAN core or any of the built extensions. To do so create a folder inside `ckan/patches` with the name of the
package to patch (ie `ckan` or `ckanext-??`). Inside you can place patch files that will be applied when building
the images. The patches will be applied in alphabetical order, so you can prefix them sequentially if necessary.
For instance, check the following example image folder:
```
ckan
├── patches
│ ├── ckan
│ │ ├── 01_datasets_per_page.patch
│ │ ├── 02_groups_per_page.patch
│ │ ├── 03_or_filters.patch
│ └── ckanext-harvest
│ └── 01_resubmit_objects.patch
2022-07-18 14:51:17 +02:00
├── setup
2022-07-18 10:48:08 +02:00
├── Dockerfile
└── Dockerfile.dev
```
2022-07-19 16:09:55 +02:00
## pdb
2022-07-20 12:51:28 +02:00
Add these lines to the `ckan-dev` service in the docker-compose.dev.yml file
2022-07-20 12:49:10 +02:00
2022-07-20 12:48:34 +02:00
![pdb](https://user-images.githubusercontent.com/54408245/179964232-9e98a451-5fe9-4842-ba9b-751bcc627730.png)
2022-07-19 17:04:36 +02:00
2022-07-20 12:47:36 +02:00
Debug with pdb (example) - Interact with `docker attach $(docker container ls -qf name=ckan)`
2022-07-20 10:13:01 +02:00
2022-07-20 12:47:36 +02:00
command: `python -m pdb /usr/lib/ckan/venv/bin/ckan --config /srv/app/ckan.ini run --host 0.0.0.0 --passthrough-errors`
2022-07-18 10:48:08 +02:00
2022-08-16 14:02:49 +02:00
## NGINX
2022-12-12 10:37:08 +01:00
* The base Docker Compose configuration uses an NGINX image as the front-end (ie: reverse proxy). It includes HTTPS running on port number 8443 and an HTTP port (81). A "self-signed" SSL certificate is generated beforehand and the server certificate and key files are included. The NGINX `server_name` directive and the `CN` field in the SSL certificate have been both set to 'localhost'. This should obviously not be used for production.
2022-08-16 14:02:49 +02:00
2022-08-22 10:33:15 +02:00
Creating the SSL cert and key files as follows:
2022-12-12 10:37:08 +01:00
`openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=DE/ST=Berlin/L=Berlin/O=None/CN=localhost" -keyout ckan-local.key -out ckan-local.crt`
2022-08-22 10:33:15 +02:00
The `ckan-local.*` files will then need to be moved into the nginx/setup/ directory
2022-07-18 10:48:08 +02:00
## Known Issues
2022-07-18 13:50:11 +02:00
* Running the tests: Running the tests for CKAN or an extension inside the container will delete your current database. We need to patch CKAN core in our image to work around that.