Docker+PostgreSQL　Error: Database is uninitialized

# 背景

https://github.com/docker-library/postgres
ここからgit cloneし、postgresql ver14の雛形をとってきた。
docker buildし、docker run <imageID>コマンド打った時に以下のような分が出てrunするが、すぐに落ちてしまってた。

```:Error文
2024-03-17 11:52:35 Error: Database is uninitialized and superuser password is not specified.
2024-03-17 11:52:35        You must specify POSTGRES_PASSWORD to a non-empty value for the
2024-03-17 11:52:35        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
2024-03-17 11:52:35 
2024-03-17 11:52:35        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
2024-03-17 11:52:35        connections without a password. This is *not* recommended.
2024-03-17 11:52:35 
2024-03-17 11:52:35        See PostgreSQL documentation about "trust":
2024-03-17 11:52:35        https://www.postgresql.org/docs/current/auth-trust.html
```

# 対処法
よくwsl上のbuildしたあとのlogを見返してみた。
```
dk@daisei:~/postgres/14/bookworm$ docker build .
[+] Building 1.5s (19/19) FINISHED                          docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 10.20kB                               0.0s
 => [internal] load metadata for docker.io/library/debian:bookworm-s  1.4s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [ 1/14] FROM docker.io/library/debian:bookworm-slim@sha256:ccb33  0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 85B                                      0.0s
 => CACHED [ 2/14] RUN set -eux;  groupadd -r postgres --gid=999;  u  0.0s
 => CACHED [ 3/14] RUN set -ex;  apt-get update;  apt-get install -y  0.0s
 => CACHED [ 4/14] RUN set -eux;  savedAptMark="$(apt-mark showmanua  0.0s
 => CACHED [ 5/14] RUN set -eux;  if [ -f /etc/dpkg/dpkg.cfg.d/docke  0.0s
 => CACHED [ 6/14] RUN set -eux;  apt-get update;  apt-get install -  0.0s
 => CACHED [ 7/14] RUN mkdir /docker-entrypoint-initdb.d              0.0s
 => CACHED [ 8/14] RUN set -ex;  key='B97B0AFCAA1A47F044F244A07FCC7D  0.0s
 => CACHED [ 9/14] RUN set -ex;   export PYTHONDONTWRITEBYTECODE=1;   0.0s
 => CACHED [10/14] RUN set -eux;  dpkg-divert --add --rename --diver  0.0s
 => CACHED [11/14] RUN mkdir -p /var/run/postgresql && chown -R post  0.0s
 => CACHED [12/14] RUN mkdir -p "/var/lib/postgresql/data" && chown   0.0s
 => CACHED [13/14] COPY docker-entrypoint.sh docker-ensure-initdb.sh  0.0s
 => CACHED [14/14] RUN ln -sT docker-ensure-initdb.sh /usr/local/bin  0.0s
 => exporting to image                                                0.0s
 => => exporting layers                                               0.0s
 => => writing image sha256:acd3ea24bee42afb6be1783115e67063f96326e8  0.0s

What's Next?
  1. Sign in to your Docker account → docker login
  2. View a summary of image vulnerabilities and recommendations → docker scout quickview
```

最後に1,2をやったほうがよさそうな文があった。もしかしてと思い、これに従ってみた。

# 手順1
```
dk@d:~/postgres/14/bookworm$ docker login
Log in with your Docker ID or email address to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com/ to create one.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
```
上手くいかなかった。最後にURLがある。みてみよう。
https://docs.docker.com/security/for-developers/access-tokens/
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3661566/bf033526-9676-58a6-4f50-ed86761f6c5a.png)

どうやら、loginの際にDocker AccountのusernameとTokenを打ち込まなければいけなかったようだ。それを打ち込んでみよう。

```
docker login -u <username>
Password: 
Login Succeeded
```
ログインできたようだ、runしているcontainerを確認してみよう。
```
docker container ls
CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS        PORTS                    NAMES
```
うーん、まだか。そういえばあともう一個やることあったな。

# 手順２
```
dk@d:~/postgres/14/bookworm$ docker build .
[+] Building 1.7s (20/20) FINISHED                          docker:default
 => [internal] load build definition from Dockerfile                  0.0s
 => => transferring dockerfile: 10.20kB                               0.0s
 => [internal] load metadata for docker.io/library/debian:bookworm-s  1.6s
 => [auth] library/debian:pull token for registry-1.docker.io         0.0s
 => [internal] load .dockerignore                                     0.0s
 => => transferring context: 2B                                       0.0s
 => [ 1/14] FROM docker.io/library/debian:bookworm-slim@sha256:ccb33  0.0s
 => [internal] load build context                                     0.0s
 => => transferring context: 85B                                      0.0s
 => CACHED [ 2/14] RUN set -eux;  groupadd -r postgres --gid=999;  u  0.0s
 => CACHED [ 3/14] RUN set -ex;  apt-get update;  apt-get install -y  0.0s
 => CACHED [ 4/14] RUN set -eux;  savedAptMark="$(apt-mark showmanua  0.0s
 => CACHED [ 5/14] RUN set -eux;  if [ -f /etc/dpkg/dpkg.cfg.d/docke  0.0s
 => CACHED [ 6/14] RUN set -eux;  apt-get update;  apt-get install -  0.0s
 => CACHED [ 7/14] RUN mkdir /docker-entrypoint-initdb.d              0.0s
 => CACHED [ 8/14] RUN set -ex;  key='B97B0AFCAA1A47F044F244A07FCC7D  0.0s
 => CACHED [ 9/14] RUN set -ex;   export PYTHONDONTWRITEBYTECODE=1;   0.0s
 => CACHED [10/14] RUN set -eux;  dpkg-divert --add --rename --diver  0.0s
 => CACHED [11/14] RUN mkdir -p /var/run/postgresql && chown -R post  0.0s
 => CACHED [12/14] RUN mkdir -p "/var/lib/postgresql/data" && chown   0.0s
 => CACHED [13/14] COPY docker-entrypoint.sh docker-ensure-initdb.sh  0.0s
 => CACHED [14/14] RUN ln -sT docker-ensure-initdb.sh /usr/local/bin  0.0s
 => exporting to image                                                0.0s
 => => exporting layers                                               0.0s
 => => writing image sha256:acd3ea24bee42afb6be1783115e67063f96326e8  0.0s

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

なるほど、loginをしたから一個減ってるな。次の手順「docker scout quickview」をしてみよう。
ちなみにdocker scout quickviewとは
「イメージのセキュリティに関する情報と、対象方法を表示できる」ものらしい。参照先：https://qiita.com/gtrekter/items/e15a862bd5b0c33069a9

```
docker scout quickview
    i New version 1.5.2 available (installed version is 1.4.1) at https://github.com/docker/scout-cli
    ✓ Image stored for indexing
    ✓ Indexed 724 packages

  Target               │  local://vsc-ya
  uid:latest  │    0C     8H    20M    78L     3?   
    digest             │  f980adbc19d9                                                                                           │                                     
  Base image           │  python:3.11-bookworm                                                                                   │    0C     8H    20M    78L     2?   
  Refreshed base image │  python:3.11-bookworm                                                                                   │    0C     1H     5M    96L     1?   
                       │                                                                                                         │           -7    -15    +18     -1
```
何やら、スキャンしてくれたみたい。

# 手順３
これでいけるだろうと再度docker run&docker container ls
```
$ docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                          PORTS                    NAMES
```
うーん。なぜだろうと思い、Docker DesktopのcontainerでError文を読んでみるとまたまた、
```
2024-03-17 11:55:09 Error: Database is uninitialized and superuser password is not specified.
2024-03-17 11:55:09        You must specify POSTGRES_PASSWORD to a non-empty value for the
2024-03-17 11:55:09        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
2024-03-17 11:55:09 
2024-03-17 11:55:09        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
2024-03-17 11:55:09        connections without a password. This is *not* recommended.
2024-03-17 11:55:09 
2024-03-17 11:55:09        See PostgreSQL documentation about "trust":
2024-03-17 11:55:09        https://www.postgresql.org/docs/current/auth-trust.html
```
となっていた。状況が違うので今度こそこのエラー文に従い、run時に「"-e POSTGRES_PASSWORD=password"」といれてみたら、

```
docker container ls -a
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                          PORTS                    NAMES
29d24eed9bbd   acd3ea24bee4   "docker-entrypoint.s…"   About a minute ago   Exited (1) About a minute ago                            magical_babbage
```
やっとできました。

# まとめ
ログインとセキュリティスキャン、Password指定がないと、GithubのものはDockerの利用ができないようになっているんですね。本番に近い環境なのでこのようなつくりが必要なのかと思いました。この情報がお役に立てれば幸いです。