EyeSeeTea / d2-docker

GNU General Public License v3.0
9 stars 3 forks source link
dhis2 dhis2-instance docker docker-compose

Requirements

On Ubuntu 18.04:

$ sudo apt install docker.io docker-compose python3 python3-setuptools

On Windows 10:

Install

On GNU/Linux:

$ sudo python3 setup.py install

If you are behind a proxy you must execute the following line to make proxy running installing pip packages:

sudo -E python3 setup.py install

For Windows, open a terminal as an Administrator and run:

$ python setup.py install

Usage

Setup core

First we need to create a core image for a specific version of DHIS2 we want to use (check available versions at releases.dhis2.org):

$ d2-docker create core docker.eyeseetea.com/eyeseetea/dhis2-core:2.37.9 --version=2.37.9

Alternatively, you may directly specify the WAR file:

$ d2-docker create core docker.eyeseetea.com/eyeseetea/dhis2-core:2.37.9 --war=dhis.war

You can add configuration files to folder DHIS2_HOME. A typical example is to add the GEE (Google Earth Engine) credentials:

$ d2-docker create core docker.eyeseetea.com/eyeseetea/dhis2-core:2.37.9 --war=dhis.war --dhis2-home=/tmp/dhis-google-auth.json

Create a base DHIS2 data image

Create a dhis2-data image from a .sql.gz SQL file and the apps and documents (or datavalue fileresources) directory to include:

$ d2-docker create data docker.eyeseetea.com/eyeseetea/dhis2-data:2.37.9-sierra --sql=sierra-db.sql.gz [--apps-dir=path/to/apps] [--documents-dir=path/to/document] [--datavalues-dir=path/to/dataValue]

Start a DHIS2 instance

Start a new container from a dhis2-data base image:

$ d2-docker start docker.eyeseetea.com/eyeseetea/dhis2-data:2.37.9-sierra

Some notes:

Custom DHIS2 dhis.conf

Copy the default dhis.conf and use it as a template to create your own configuration. Then pass it to the start command:

$ d2-docker start --dhis-conf=dhis.conf ...

Custom Tomcat server.xml

Copy the default server.xml and use it as a template to create your own configuration. Then pass it to the start command:

$ d2-docker start --tomcat-server-xml=server-xml.xml ...

Note that you should not change the catalina connector port (8080, by default). A typical configuration to use https would look like this:

<Server port="8005" shutdown="SHUTDOWN">
    ...
    <Service name="Catalina">
        <Connector
            port="8080"
            protocol="HTTP/1.1"
            proxyPort="443"
            scheme="https"
            secure="true"
            proxyName="some-host.org"
            connectionTimeout="20000"
            URIEncoding="UTF-8"
            relaxedQueryChars='\ { } | [ ]'
            redirectPort="8443"
        />
    ...
/>

Run terminal shell in DHIS2 core container

$ d2-docker shell eyeseetea/dhis2-data:2.30-sierra

Show logs for running containers

Check logs of a running container:

$ d2-docker logs -f eyeseetea/dhis2-data:2.30-sierra

If only one d2-docker container is active, you can omit the image name.

Commit & push an image

This will update the image from the current container (SQL dump, apps and documents):

$ d2-docker commit

You can also create a new dhis2-data image from the running d2-docker containers:

$ d2-docker commit eyeseetea/dhis2-data:2.30-sierra-new

Now you can upload images to hub.docker using the command push:

$ d2-docker push eyeseetea/dhis2-data:2.30-sierra

Stop a DHIS2 container instance

Stop running containers:

$ d2-docker stop eyeseetea/dhis2-data:2.30-sierra

If only one d2-docker container is active, you can omit the image name.

Export a DHIS2 container instance to a file

You can export all the images needed by d2-docker to a single file, ready to distribute.

Note that you must commit any changes first, since this will export images, not containers.

$ d2-docker export -i eyeseetea/dhis2-data:2.30-sierra dhis2-sierra

Now you can copy this file to any other machine, which may now use commands import FILE and start FILE.

If only one d2-docker container is active, you can omit the image name.

Import a DHIS2 instance from an exported file

Use the output file from command export to create all d2-docker images required:

$ d2-docker import dhis2-sierra.tgz

Start DHIS2 instance from an exported file

You can use the same start command, passing the file instead of the image name. d2-docker will then import the images of the file and automatically start the DHIS2 instance it contains.

$ d2-docker start dhis2-sierra.tgz

On the first run, the images will been created, but you can either run this command again or the standard start DHIS2_DATA_IMAGE_NAME.

Delete image/containers linked to an instance

$ d2-docker rm eyeseetea/dhis2-data:2.30-sierra

Copy Docker images to/from local directories

You can use a Docker image or a data directory (db + apps + documents) as source, that will create a new Docker image eyeseetea/dhis2-data:2.30-sierra2 and a sierra-data/ directory:

$ d2-docker copy eyeseetea/dhis2-data:2.30-sierra eyeseetea/dhis2-data:2.30-sierra2 sierra-data

$ docker image ls | grep 2.30-sierra2
eyeseetea/dhis2-data      2.30-sierra2         930aced0d915        1 minutes ago      106MB

$ ls sierra-data/
apps document db.sql.gz

Alternatively, you can use a data directory (db + apps + documents) as source and create Docker images from it:

$ d2-docker copy sierra-data eyeseetea/dhis2-data:2.30-sierra3 eyeseetea/dhis2-data:2.30-sierra4
[...]

$ docker image ls | grep "2.30-sierra\(3\|4\)"
eyeseetea/dhis2-data 2.30-sierra3 930aced0d915 1 minutes ago 106MB
eyeseetea/dhis2-data 2.30-sierra4 d3a374301234 1 minutes ago 106MB

List all local d2-docker data images

Lists dhis2-data images present in the local repository and the container status:

$ d2-docker list
eyeseetea/dhis2-data:2.30-sierra RUNNING[port=8080]
eyeseetea/dhis2-data:2.30-vietnam STOPPED
eyeseetea/dhis2-data:2.30-cambodia STOPPED

Run SQL file in container

Run a SQL file or open an interactive postgres session in a running Dhis2 instance:

$ d2-docker run-sql [-i eyeseetea/dhis2-data:2.30-sierra] some-query.sql

Dump current database to SQL file in container

$ d2-docker run-sql [-i eyeseetea/dhis2-data:2.30-sierra] --dump

Upgrade DHIS2 version

$ d2-docker upgrade \
    --from=eyeseetea/dhis2-data:2.30-sierra \
    --to=eyeseetea/dhis2-data:2.32-sierra \
    --migrations=upgrade-sierra/

Migration folder upgrade-sierra should then contain data to be used in each intermediate upgrade version. Supported migration data:

A full example might look:

upgrade-sierra/2.31/dhis.war
upgrade-sierra/2.31/fix-users.sql
upgrade-sierra/2.31/dhis2-home/dhis-google-auth.json
upgrade-sierra/2.31/post-metadata.sh
upgrade-sierra/2.31/some-metadata-used-by-the-script.json

upgrade-sierra/2.32/fix-org-units-geometry.sql

Clean-up

Docker infrastructure (images, networks, containers, volumes) takes up a lot of hard-disk space.

Remove all local volumes not used by at least one container:

$ docker volume prune

Remove all stopped containers:

$ docker container prune

Remove all dangling images (the temporal images that have <none> on its name/tag):

$ docker image prune

WARNING: Dangerous operation Delete all stopped containers, networks, volumes, images and cache. Note, that any dhis2-data image still not pushed to the repository, will be also deleted whether the instance is running or nor (as it's not kept as an active container):

$ docker system prune -a --volumes

Development

Run d2-docker from sources

$ ./d2-docker-dev.sh

Dockerized d2-docker (d2-container)

Create a dockerized d2-docker:

$ bash build-docker-container.sh

API Server

Start Flask server in development mode:

$ FLASK_ENV=development flask run

Usage examples:

$ curl http://localhost:5000/version

$ curl  -H "Content-Type: application/json" -sS http://localhost:5000/instances/start -X POST \
    -d '{"image": "docker.eyeseetea.com/samaritans/dhis2-data:2.36.8-sp-ip-training", "port": 8080, "detach": true}'

Currently, there are no API docs nor params validations. For each command src/d2_docker/commands/COMMAND.py, check function setup to see the supported parameters.

The API server provides a proxy to Harbor to bypass CORS issues. Configure first the harbor authentication file:

$ cp flaskenv.secret.template flaskenv.secret
$ # Edit flaskenv.secret
$ mkdir -p ~/.config/d2-docker/
$ cp flaskenv.secret ~/.config/d2-docker/

$ curl -sS 'http://localhost:5000/harbor/https://docker.eyeseetea.com/api/v2.0/quotas/1' | jq