Open6

DockerでPostgreSQLのソースコードをビルドして実行する

tommy34tommy34

Dockerfile

FROM ubuntu:24.04

ENV TZ=Asia/Tokyo
ENV PATH=/usr/local/pgsql/bin:$PATH
ENV VERSION=16.2

RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone && \
  sed -i 's@archive.ubuntu.com@ftp.jaist.ac.jp/pub/Linux@g' /etc/apt/sources.list && \
  echo "bind 'set bell-style none'" >> .bashrc && \
  apt-get update && apt-get install --no-install-recommends -y wget build-essential libssl-dev libncurses5-dev \
    libicu-dev libreadline-dev zlib1g-dev pkg-config cmake cloc && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* && \
  wget -q --no-check-certificate https://ftp.postgresql.org/pub/source/v${VERSION}/postgresql-${VERSION}.tar.gz && \
  tar xf postgresql-${VERSION}.tar.gz

WORKDIR /postgresql-${VERSION}

RUN ./configure && make -j$(nproc) install
tommy34tommy34

コンテナのビルド

$ docker build -t postgresql .
[+] Building 557.8s (8/8) FINISHED                                                                                                                                                    docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                                  0.0s
 => => transferring dockerfile: 822B                                                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                                                     0.0s
 => => transferring context: 2B                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ubuntu:24.04                                                                                                                                       1.1s
 => CACHED [1/4] FROM docker.io/library/ubuntu:24.04@sha256:36fa0c7153804946e17ee951fdeffa6a1c67e5088438e5b90de077de5c600d4c                                                                          0.0s
 => [2/4] RUN ln -snf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo Asia/Tokyo > /etc/timezone &&   sed -i 's@archive.ubuntu.com@ftp.jaist.ac.jp/pub/Linux@g' /etc/apt/sources.list &&   e  440.6s
 => [3/4] WORKDIR /postgresql-16.2                                                                                                                                                                    0.0s
 => [4/4] RUN ./configure && make -j$(nproc) install                                                                                                                                                113.0s
 => exporting to image                                                                                                                                                                                2.9s
 => => exporting layers                                                                                                                                                                               2.9s
 => => writing image sha256:816dd5d9f100198f52d32729f3877235e1b9c7b4b34b78524d1a5f6f32a8ca07                                                                                                          0.0s
 => => naming to docker.io/library/postgresql                                                                                                                                                         0.0s

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview
tommy34tommy34

コンテナでPostgreSQLを初期化して、psqlクライアントでデータベースに接続できたことを確認した

$ docker run -it --rm --name postgresql_container postgresql bash
root@edabfbef714e:/postgresql-16.2# ls
COPYRIGHT  GNUmakefile  GNUmakefile.in  HISTORY  INSTALL  Makefile  README  aclocal.m4  config  config.log  config.status  configure  configure.ac  contrib  doc  meson.build  meson_options.txt  src
root@edabfbef714e:/postgresql-16.2# ls /usr/local/
bin  etc  games  include  lib  man  pgsql  sbin  share  src
root@edabfbef714e:/postgresql-16.2# ls /usr/local/pgsql
bin  include  lib  share
root@edabfbef714e:/postgresql-16.2# ls /usr/local/pgsql/bin
clusterdb   dropdb    initdb             pg_basebackup  pg_controldata  pg_dumpall     pg_recvlogical  pg_rewind       pg_upgrade       pgbench   reindexdb
createdb    dropuser  pg_amcheck         pg_checksums   pg_ctl          pg_isready     pg_resetwal     pg_test_fsync   pg_verifybackup  postgres  vacuumdb
createuser  ecpg      pg_archivecleanup  pg_config      pg_dump         pg_receivewal  pg_restore      pg_test_timing  pg_waldump       psql
root@edabfbef714e:/postgresql-16.2# PATH=/usr/local/pgsql/bin:$PATH
root@edabfbef714e:/postgresql-16.2# initdb --version
initdb (PostgreSQL) 16.2
root@edabfbef714e:/postgresql-16.2# initdb -D ${PWD}/tmp -E UNICODE --locale=C
initdb: error: cannot be run as root
initdb: hint: Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
root@edabfbef714e:/postgresql-16.2# useradd postgres
root@edabfbef714e:/postgresql-16.2# su - postgres
su: warning: cannot change directory to /home/postgres: No such file or directory
$ ls
COPYRIGHT  GNUmakefile  GNUmakefile.in  HISTORY  INSTALL  Makefile  README  aclocal.m4  config  config.log  config.status  configure  configure.ac  contrib  doc  meson.build  meson_options.txt  src
$ PATH=/usr/local/pgsql/bin:$PATH
$ initdb -D ${PWD}/tmp -E UNICODE --locale=C
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /postgresql-16.2/tmp ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /postgresql-16.2/tmp -l logfile start

$ ps
  PID TTY          TIME CMD
   46 pts/0    00:00:00 sh
   63 pts/0    00:00:00 ps
$ tail /etc/passwd
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/run/ircd:/usr/sbin/nologin
_apt:x:42:65534::/nonexistent:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
postgres:x:1001:1001::/home/postgres:/bin/sh
$ pg_ctl -D /postgresql-16.2/tmp -l logfile start
waiting for server to start.... done
server started
$ ps aufx
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4204  3200 pts/0    Ss   07:12   0:00 bash
root        20  0.0  0.0   5180  2304 pts/0    S    07:14   0:00 su
root        21  0.0  0.0   4628  3584 pts/0    S    07:14   0:00  \_ bash
root        45  0.0  0.0   5736  2432 pts/0    S    07:16   0:00      \_ su - postgres
postgres    46  0.0  0.0   2308  1408 pts/0    S    07:16   0:00          \_ -sh
postgres    75  0.0  0.0   7876  3712 pts/0    R+   07:18   0:00              \_ ps aufx
postgres    68  0.9  0.2 197528 20224 ?        Ss   07:18   0:00 /usr/local/pgsql/bin/postgres -D /postgresql-16.2/tmp
postgres    69  0.0  0.0 197528  3520 ?        Ss   07:18   0:00  \_ postgres: checkpointer
postgres    70  0.0  0.0 197528  3392 ?        Ss   07:18   0:00  \_ postgres: background writer
postgres    72  0.0  0.0 197528  3392 ?        Ss   07:18   0:00  \_ postgres: walwriter
postgres    73  0.0  0.0 199124  6336 ?        Ss   07:18   0:00  \_ postgres: autovacuum launcher
postgres    74  0.0  0.0 199100  5824 ?        Ss   07:18   0:00  \_ postgres: logical replication launcher
$ createdb testdb
$ psql testdb
psql (16.2)
Type "help" for help.

testdb=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
testdb=# \list
                                                  List of databases
   Name    |  Owner   | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+---------+-------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | C       | C     |            |           |
 template0 | postgres | UTF8     | libc            | C       | C     |            |           | =c/postgres          +
           |          |          |                 |         |       |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | C       | C     |            |           | =c/postgres          +
           |          |          |                 |         |       |            |           | postgres=CTc/postgres
 testdb    | postgres | UTF8     | libc            | C       | C     |            |           |
(4 rows)

testdb=# select * from pg_language;
  oid  | lanname  | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
-------+----------+----------+---------+--------------+---------------+-----------+--------------+--------
    12 | internal |       10 | f       | f            |             0 |         0 |         2246 |
    13 | c        |       10 | f       | f            |             0 |         0 |         2247 |
    14 | sql      |       10 | f       | t            |             0 |         0 |         2248 |
 13612 | plpgsql  |       10 | t       | t            |         13609 |     13610 |        13611 |
(4 rows)

testdb=# \q
could not save history to file "/home/postgres/.psql_history": No such file or directory
$ exit
root@edabfbef714e:/postgresql-16.2#
tommy34tommy34

ソースコードの行数は約216万行

root@edabfbef714e:/postgresql-16.2# cloc --exclude-dir=tmp .
    8505 text files.
    5926 unique files.
    3920 files ignored.

github.com/AlDanial/cloc v 1.98  T=7.58 s (782.0 files/s, 434754.4 lines/s)
---------------------------------------------------------------------------------------
Language                             files          blank        comment           code
---------------------------------------------------------------------------------------
C                                     1495         195649         370016        1071191
PO File                                452         170368         209726         509243
HTML                                  1156           4190              0         175257
SQL                                    722          27490          20932         109282
C/C++ Header                          1014          18930          61399         102555
Perl                                   276          10695           9335          49570
yacc                                    11           4681           3236          37591
XML                                      5             29             29          31117
Text                                   104            175              0          27841
Bourne Shell                            36           3175           2856          21127
Meson                                  264           1847           1117          10195
make                                   301           2188           2316           7447
lex                                     13           1118           2193           5400
m4                                      13            607            154           4433
XSLT                                    11            247            198           1516
Bourne Again Shell                       2            120            135           1156
SVG                                      6              0            148            832
C++                                      3            168            282            746
YAML                                     2            139            167            545
Python                                   8            156            135            410
DOS Batch                                6             21             26            141
CSS                                      1             37             14            131
D                                        1             14             14             66
Assembly                                 2             14             38             44
Windows Resource File                    2              3              1             33
sed                                      2              3             17             19
Lisp                                     1              1              1             17
Prolog                                   1              2              0             17
CSV                                      5              0              0             16
INI                                      1              3              0             11
IDL                                      8              0              0              8
Windows Message File                     1              0              0              5
Windows Module Definition                1              0              1              4
---------------------------------------------------------------------------------------
SUM:                                  5926         442070         684486        2167966
---------------------------------------------------------------------------------------
root@edabfbef714e:/postgresql-16.2#