メモ：BaseXのDockerfile

自分用メモです。

ここを参考にしながら、
https://hub.docker.com/r/basex/basexhttp/dockerfile

BaseXの公式サイト（ http://basex.org/download/ ）からダウンロードしたzipを使うように Dockerfile をアレンジしました。

```Dockerfile

FROM openjdk:8-alpine

RUN apk update && \
    apk add --no-cache bash curl && \
    cd /srv && \
    curl -O http://files.basex.org/releases/9.3.3/BaseX933.zip && \
    unzip BaseX933.zip && \
    adduser -h /srv -D -u 1984 basex && \
    chown -R basex /srv
USER basex
ENV PATH $PATH:/srv/basex/bin

# 1984/tcp: API
# 8984/tcp: HTTP
# 8985/tcp: HTTP stop
EXPOSE 1984 8984 8985
VOLUME ["/srv/basex/data", "/srv/basex/repo","/srv/basex/webapp"]
WORKDIR /srv

# Run BaseX HTTP server by default
CMD ["/srv/basex/bin/basexhttp"]
```

ただし上のDockerfileでは、新規ユーザーを作成してrootユーザーから切り替えていますので、volumeをマウントしたときにファイルのowner問題が発生して「Permission denied」が出てしまいました。
対処方法はあるけど面倒だったので、しばらく新規ユーザーを作成せずにrootで動かして、問題が出ないか様子を見ることにしました。
そのDockerfileは以下の通りです。

```Dockerfile

FROM openjdk:8-alpine

RUN apk update && \
    apk add --no-cache bash curl && \
    cd /srv && \
    curl -O http://files.basex.org/releases/9.3.3/BaseX933.zip && \
    unzip BaseX933.zip
ENV PATH $PATH:/srv/basex/bin

# 1984/tcp: API
# 8984/tcp: HTTP
# 8985/tcp: HTTP stop
EXPOSE 1984 8984 8985
VOLUME ["/srv/basex/data", "/srv/basex/repo","/srv/basex/webapp"]
WORKDIR /srv

# Run BaseX HTTP server by default
CMD ["/srv/basex/bin/basexhttp"]
```

docker-compose.yml の例：

```docker-compose.yml

version: '3'
services:
  basexhttp:
    build: ./basexhttp
    # image: basex/basexhttp:latest
    restart: always
    volumes:
      - ${HOME}/docker_basex/data:/srv/basex/data:rw
    ports:
      - "11984:1984"
      - "18984:8984"
```

以下のコマンドを順次実行して、rootユーザーでDBを作成できるか試してみます。

```

docker-compose build basexhttp
docker-compose up -d
docker exec -it usrname_basexhttp_1 bash
basexclient
　　Username: admin
　　Password: admin
create db foo
quit
```

実行した時の画面イメージは以下の通り。

```

$ docker-compose build basexhttp
Building basexhttp
Step 1/7 : FROM openjdk:8-alpine
8-alpine: Pulling from library/openjdk
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
c2274a1a0e27: Pull complete
Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
Status: Downloaded newer image for openjdk:8-alpine
 ---> a3562aa0b991
Step 2/7 : RUN apk update &&     apk add --no-cache bash curl &&     cd /srv &&     curl -O http://files.basex.org/releases/9.3.3/BaseX933.zip &&     unzip BaseX933.zip
 ---> Running in ce02aa1917bb
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
v3.9.6-36-g8bb1a68c68 [http://dl-cdn.alpinelinux.org/alpine/v3.9/main]
v3.9.6-13-gfbfc29c6a0 [http://dl-cdn.alpinelinux.org/alpine/v3.9/community]
OK: 9789 distinct packages available
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
(1/9) Installing ncurses-terminfo-base (6.1_p20190105-r0)
(2/9) Installing ncurses-terminfo (6.1_p20190105-r0)
(3/9) Installing ncurses-libs (6.1_p20190105-r0)
(4/9) Installing readline (7.0.003-r1)
(5/9) Installing bash (4.4.19-r1)
Executing bash-4.4.19-r1.post-install
(6/9) Installing nghttp2-libs (1.35.1-r2)
(7/9) Installing libssh2 (1.9.0-r1)
(8/9) Installing libcurl (7.64.0-r3)
(9/9) Installing curl (7.64.0-r3)
Executing busybox-1.29.3-r10.trigger
OK: 113 MiB in 63 packages
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9.9M  100  9.9M    0     0   938k      0  0:00:10  0:00:10 --:--:-- 1081k
Archive:  BaseX933.zip
  inflating: basex/.basexhome
  inflating: basex/BaseX.jar
   creating: basex/bin/
  inflating: basex/bin/basex
  inflating: basex/bin/basex.bat
  inflating: basex/bin/basexclient
  inflating: basex/bin/basexclient.bat
  inflating: basex/bin/basexgui
  inflating: basex/bin/basexgui.bat
  inflating: basex/bin/basexhttp
  inflating: basex/bin/basexhttp.bat
  inflating: basex/bin/basexhttpstop
  inflating: basex/bin/basexhttpstop.bat
  inflating: basex/bin/basexserver
  inflating: basex/bin/basexserver.bat
  inflating: basex/bin/basexserverstop
  inflating: basex/bin/basexserverstop.bat
  inflating: basex/CHANGELOG
   creating: basex/data/
   creating: basex/etc/
  inflating: basex/etc/factbook.xml
  inflating: basex/etc/stopwords.txt
  inflating: basex/etc/w3-catalog.xml
  inflating: basex/etc/xhtml-lat1.ent
  inflating: basex/etc/xhtml-special.ent
  inflating: basex/etc/xhtml-symbol.ent
  inflating: basex/etc/xhtml1-frameset.dtd
  inflating: basex/etc/xhtml1-strict.dtd
  inflating: basex/etc/xhtml1-transitional.dtd
  inflating: basex/etc/xqdoc.zip
   creating: basex/lib/
  inflating: basex/lib/basex-api-9.3.3.jar
  inflating: basex/lib/basex-xqj-9.0.jar
  inflating: basex/lib/commons-codec-1.4.jar
  inflating: basex/lib/commons-fileupload-1.4.jar
  inflating: basex/lib/commons-io-1.4.jar
   creating: basex/lib/custom/
  inflating: basex/lib/igo-0.4.3.jar
  inflating: basex/lib/javax.servlet-api-3.1.0.jar
  inflating: basex/lib/jdom-1.1.jar
  inflating: basex/lib/jetty-client-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-continuation-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-http-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-io-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-security-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-server-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-servlet-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-servlets-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-util-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-webapp-9.4.28.v20200408.jar
  inflating: basex/lib/jetty-xml-9.4.28.v20200408.jar
  inflating: basex/lib/jing-20181222.jar
  inflating: basex/lib/jline-2.14.6.jar
  inflating: basex/lib/jts-1.13.jar
  inflating: basex/lib/LICENSES
  inflating: basex/lib/lucene-stemmers-3.4.0.jar
  inflating: basex/lib/milton-api-1.8.1.4.jar
  inflating: basex/lib/mime-util-2.1.3.jar
  inflating: basex/lib/slf4j-api-1.7.28.jar
  inflating: basex/lib/slf4j-simple-1.7.28.jar
  inflating: basex/lib/tagsoup-1.2.1.jar
  inflating: basex/lib/websocket-api-9.4.28.v20200408.jar
  inflating: basex/lib/websocket-client-9.4.28.v20200408.jar
  inflating: basex/lib/websocket-common-9.4.28.v20200408.jar
  inflating: basex/lib/websocket-server-9.4.28.v20200408.jar
  inflating: basex/lib/websocket-servlet-9.4.28.v20200408.jar
  inflating: basex/lib/xml-resolver-1.2.jar
  inflating: basex/lib/xmldb-api-1.0.jar
  inflating: basex/lib/xqj-api-1.0.jar
  inflating: basex/lib/xqj2-0.2.0.jar
  inflating: basex/LICENSE
  inflating: basex/readme.txt
   creating: basex/repo/
   creating: basex/repo/http-www.functx.com-1.0/
  inflating: basex/repo/http-www.functx.com-1.0/cxan.xml
  inflating: basex/repo/http-www.functx.com-1.0/expath-pkg.xml
   creating: basex/repo/http-www.functx.com-1.0/functx/
  inflating: basex/repo/http-www.functx.com-1.0/functx/functx.xq
  inflating: basex/repo/http-www.functx.com-1.0/functx/functx.xsl
   creating: basex/src/
  inflating: basex/src/hello-world.xq
   creating: basex/webapp/
   creating: basex/webapp/chat/
  inflating: basex/webapp/chat/chat-util.xqm
  inflating: basex/webapp/chat/chat-ws.xqm
  inflating: basex/webapp/chat/chat.xqm
   creating: basex/webapp/dba/
  inflating: basex/webapp/dba/common.xqm
   creating: basex/webapp/dba/databases/
  inflating: basex/webapp/dba/databases/backups.xqm
  inflating: basex/webapp/dba/databases/database.xqm
  inflating: basex/webapp/dba/databases/databases.xqm
  inflating: basex/webapp/dba/databases/db-alter.xqm
  inflating: basex/webapp/dba/databases/db-copy.xqm
  inflating: basex/webapp/dba/databases/db-create.xqm
  inflating: basex/webapp/dba/databases/db-drop.xqm
  inflating: basex/webapp/dba/databases/db-optimize.xqm
   creating: basex/webapp/dba/databases/resources/
  inflating: basex/webapp/dba/databases/resources/backup-download.xqm
  inflating: basex/webapp/dba/databases/resources/db-add.xqm
  inflating: basex/webapp/dba/databases/resources/db-delete.xqm
  inflating: basex/webapp/dba/databases/resources/db-download.xqm
  inflating: basex/webapp/dba/databases/resources/db-query.xqm
  inflating: basex/webapp/dba/databases/resources/db-rename.xqm
  inflating: basex/webapp/dba/databases/resources/db-replace.xqm
  inflating: basex/webapp/dba/databases/resources/resource.xqm
   creating: basex/webapp/dba/files/
  inflating: basex/webapp/dba/files/dir-change.xqm
  inflating: basex/webapp/dba/files/dir-create.xqm
  inflating: basex/webapp/dba/files/file-delete.xqm
  inflating: basex/webapp/dba/files/file-download.xqm
  inflating: basex/webapp/dba/files/file-start.xqm
  inflating: basex/webapp/dba/files/file-stop.xqm
  inflating: basex/webapp/dba/files/file-upload.xqm
  inflating: basex/webapp/dba/files/files.xqm
   creating: basex/webapp/dba/jobs/
  inflating: basex/webapp/dba/jobs/job-result.xqm
  inflating: basex/webapp/dba/jobs/job-stop.xqm
  inflating: basex/webapp/dba/jobs/jobs.xqm
   creating: basex/webapp/dba/lib/
  inflating: basex/webapp/dba/lib/config.xqm
  inflating: basex/webapp/dba/lib/html.xqm
  inflating: basex/webapp/dba/lib/options.xqm
  inflating: basex/webapp/dba/lib/util.xqm
  inflating: basex/webapp/dba/login.xqm
   creating: basex/webapp/dba/logs/
  inflating: basex/webapp/dba/logs/log-delete.xqm
  inflating: basex/webapp/dba/logs/log-download.xqm
  inflating: basex/webapp/dba/logs/logs.xqm
   creating: basex/webapp/dba/queries/
  inflating: basex/webapp/dba/queries/queries.xqm
  inflating: basex/webapp/dba/queries/query-close.xqm
  inflating: basex/webapp/dba/queries/query-eval.xqm
  inflating: basex/webapp/dba/queries/query-open.xqm
  inflating: basex/webapp/dba/queries/query-save.xqm
   creating: basex/webapp/dba/sessions/
  inflating: basex/webapp/dba/sessions/session-kill.xqm
  inflating: basex/webapp/dba/sessions/sessions-list.xqm
   creating: basex/webapp/dba/settings/
  inflating: basex/webapp/dba/settings/settings-gc.xqm
  inflating: basex/webapp/dba/settings/settings.xqm
   creating: basex/webapp/dba/static/
  inflating: basex/webapp/dba/static/basex.svg
   creating: basex/webapp/dba/static/codemirror/
   creating: basex/webapp/dba/static/codemirror/lib/
  inflating: basex/webapp/dba/static/codemirror/lib/codemirror.css
  inflating: basex/webapp/dba/static/codemirror/lib/codemirror.js
   creating: basex/webapp/dba/static/codemirror/mode/
   creating: basex/webapp/dba/static/codemirror/mode/xml/
  inflating: basex/webapp/dba/static/codemirror/mode/xml/xml.js
   creating: basex/webapp/dba/static/codemirror/mode/xquery/
  inflating: basex/webapp/dba/static/codemirror/mode/xquery/xquery.js
  inflating: basex/webapp/dba/static/editor.js
  inflating: basex/webapp/dba/static/js.js
  inflating: basex/webapp/dba/static/style.css
   creating: basex/webapp/dba/users/
  inflating: basex/webapp/dba/users/pattern-add.xqm
  inflating: basex/webapp/dba/users/pattern-drop.xqm
  inflating: basex/webapp/dba/users/user-create.xqm
  inflating: basex/webapp/dba/users/user-drop.xqm
  inflating: basex/webapp/dba/users/user-edit.xqm
  inflating: basex/webapp/dba/users/user.xqm
  inflating: basex/webapp/dba/users/users-info.xqm
  inflating: basex/webapp/dba/users/users.xqm
  inflating: basex/webapp/restxq.xqm
   creating: basex/webapp/static/
  inflating: basex/webapp/static/.ignore
  inflating: basex/webapp/static/basex.svg
  inflating: basex/webapp/static/chat.js
  inflating: basex/webapp/static/style.css
   creating: basex/webapp/WEB-INF/
  inflating: basex/webapp/WEB-INF/jetty.xml
  inflating: basex/webapp/WEB-INF/web.xml
Removing intermediate container ce02aa1917bb
 ---> ba7499924a6c
Step 3/7 : ENV PATH $PATH:/srv/basex/bin
 ---> Running in beea25740ddd
Removing intermediate container beea25740ddd
 ---> c2c458d63638
Step 4/7 : EXPOSE 1984 8984 8985
 ---> Running in 5ceb4f9368dd
Removing intermediate container 5ceb4f9368dd
 ---> e0fa1cb5de64
Step 5/7 : VOLUME ["/srv/basex/data", "/srv/basex/repo","/srv/basex/webapp"]
 ---> Running in 6dc131612d9d
Removing intermediate container 6dc131612d9d
 ---> a70fbeeba964
Step 6/7 : WORKDIR /srv
 ---> Running in b2cf7c566959
Removing intermediate container b2cf7c566959
 ---> ac14a694d542
Step 7/7 : CMD ["/srv/basex/bin/basexhttp"]
 ---> Running in 87749ec6cb60
Removing intermediate container 87749ec6cb60
 ---> 8fe59c31000d
Successfully built 8fe59c31000d
Successfully tagged usrname_basexhttp:latest
```
```

$ docker-compose up -d
Creating network "usrname_default" with the default driver
Creating usrname_basexhttp_1 ... done
```
```

$ docker exec -it usrname_basexhttp_1 bash
bash-4.4# basexclient
Username: admin
Password: 
BaseX 9.3.3 [Client]
Try 'help' to get more information.
> create db foo
Database 'foo' created in 154.08 ms.
> quit
Have a nice day.
bash-4.4# exit
$
```

上記のように、rootユーザーでデータベースを作成してもエラーは出ませんでした。
Dockerでvolumeをマウントしましたので、ホスト側のデータベースファイルも見てみます。

```

$ ls -l ~/docker_basex/data/
合計 4
drwxr-xr-x 2 root root 4096  6月 10 11:37 foo
```
```

$ ls -l ~/docker_basex/data/foo/
合計 20
-rw-r--r-- 1 root root    0  6月 10 11:37 atv.basex
-rw-r--r-- 1 root root    4  6月 10 11:37 atvl.basex
-rw-r--r-- 1 root root    0  6月 10 11:37 atvr.basex
-rw-r--r-- 1 root root  464  6月 10 11:37 inf.basex
-rw-r--r-- 1 root root 4096  6月 10 11:37 tbl.basex
-rw-r--r-- 1 root root    2  6月 10 11:37 tbli.basex
-rw-r--r-- 1 root root    0  6月 10 11:37 txt.basex
-rw-r--r-- 1 root root    4  6月 10 11:37 txtl.basex
-rw-r--r-- 1 root root    0  6月 10 11:37 txtr.basex
```

rootユーザーでfooデータベースが作成されていました。
このまましばらく、問題が出ないか様子を見ることにします。