docker-compose のpermission deniedが解消されない
解決したいこと
docker初心者です。
dockerでpostgleSQLを使おうとしているのですが、permission deniedが解消されず、
困っています。
発生している問題・エラー
// 起動中のコンテナ
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8f7545236df dpage/pgadmin4 "/entrypoint.sh" 44 seconds ago Up 45 seconds 443/tcp, 0.0.0.0:81->80/tcp, :::81->80/tcp nestjs-practice-pgadmin-1
313eb325cb02 postgres:14.2-alpine "docker-entrypoint.s…" 44 seconds ago Restarting (1) 15 seconds ago postgres
// エラー
docker logs 313eb325cb02
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
chown: /var/lib/postgresql/data/pgdata: Permission denied
エラーの内容的に権限周りのことなのかと思いましたが、解消方法を調べて実行しても解決しません。
使用環境
・mac
・lima: version 0.17.2
・docker(CLI): version: 24.0.6
・docker compose: version v2.21.0
該当するソースコード
・docker-compose.yaml
version: '3.8'
services:
postgres:
image: postgres:14.2-alpine
container_name: postgres
ports:
- 5432:5432
volumes:
- ./docker/postgres/init.d:/docker-entrypoint-initdb.d
- ./docker/postgres/pgdata:/var/lib/postgresql/data:z
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_INITDB_ARGS: '--encoding=UTF-8'
POSTGRES_DB: postgres
PGDATA: /var/lib/postgresql/data/pgdata
hostname: postgres
restart: always
user: root
pgadmin:
image: dpage/pgadmin4
restart: always
ports:
- 81:80
environment:
PGADMIN_DEFAULT_EMAIL: nestjs@example.com
PGADMIN_DEFAULT_PASSWORD: password
volumes:
- ./docker/pgadmin:/var/lib/pgadmin:z
depends_on:
- postgres
user: root
・docker.yaml
# A template to use Docker instead of containerd & nerdctl
# $ limactl start ./docker.yaml
# $ limactl shell docker docker run -it -v $HOME:$HOME --rm alpine
# To run `docker` on the host (assumes docker-cli is installed):
# $ export DOCKER_HOST=$(limactl list docker --format 'unix://{{.Dir}}/sock/docker.sock')
# $ docker ...
# This template requires Lima v0.8.0 or later
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: 'https://cloud-images.ubuntu.com/releases/22.04/release-20230729/ubuntu-22.04-server-cloudimg-amd64.img'
arch: 'x86_64'
digest: 'sha256:d5b419272e01cd69bfc15cbbbc5700d2196242478a54b9f19746da3a1269b7c8'
- location: 'https://cloud-images.ubuntu.com/releases/22.04/release-20230729/ubuntu-22.04-server-cloudimg-arm64.img'
arch: 'aarch64'
digest: 'sha256:5ecab49ff44f8e44954752bc9ef4157584b7bdc9e24f06031e777f60860a9d17'
# Fallback to the latest release image.
# Hint: run `limactl prune` to invalidate the cache
- location: 'https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img'
arch: 'x86_64'
- location: 'https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img'
arch: 'aarch64'
cpus: 4
memory: '8GiB'
disk: '100GiB'
mounts:
- location: '~'
writable: true
- location: '/tmp/lima'
writable: true
- location: '~/work'
writable: true
- location: '~/private'
writable: true
- location: '~/NestJS/nestjs-practice' # 作業中のディレクトリです。
writable: true
# containerd is managed by Docker, not by Lima, so the values are set to false here.
containerd:
system: false
user: false
provision:
- mode: system
# This script defines the host.docker.internal hostname when hostResolver is disabled.
# It is also needed for lima 0.8.2 and earlier, which does not support hostResolver.hosts.
# Names defined in /etc/hosts inside the VM are not resolved inside containers when
# using the hostResolver; use hostResolver.hosts instead (requires lima 0.8.3 or later).
script: |
#!/bin/sh
sed -i 's/host.lima.internal.*/host.lima.internal host.docker.internal/' /etc/hosts
- mode: system
script: |
#!/bin/bash
set -eux -o pipefail
command -v docker >/dev/null 2>&1 && exit 0
export DEBIAN_FRONTEND=noninteractive
curl -fsSL https://get.docker.com | sh
# NOTE: you may remove the lines below, if you prefer to use rootful docker, not rootless
systemctl disable --now docker
apt-get install -y uidmap dbus-user-session
- mode: user
script: |
#!/bin/bash
set -eux -o pipefail
systemctl --user start dbus
dockerd-rootless-setuptool.sh install
docker context use rootless
probes:
- script: |
#!/bin/bash
set -eux -o pipefail
if ! timeout 30s bash -c "until command -v docker >/dev/null 2>&1; do sleep 3; done"; then
echo >&2 "docker is not installed yet"
exit 1
fi
if ! timeout 30s bash -c "until pgrep rootlesskit; do sleep 3; done"; then
echo >&2 "rootlesskit (used by rootless docker) is not running"
exit 1
fi
hint: See "/var/log/cloud-init-output.log". in the guest
hostResolver:
# hostResolver.hosts requires lima 0.8.3 or later. Names defined here will also
# resolve inside containers, and not just inside the VM itself.
hosts:
host.docker.internal: host.lima.internal
portForwards:
- guestSocket: '/run/user/{{.UID}}/docker.sock'
hostSocket: '{{.Dir}}/sock/docker.sock'
message: |
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
------
docker context create lima-{{.Name}} --docker "host=unix://{{.Dir}}/sock/docker.sock"
docker context use lima-{{.Name}}
docker run hello-world
------
end
自分で試したこと
・:z
をつけてみた。
・コンテナ実行時のuserを指定してみた。
// ホスト
uid=501(myName) gid=20(staff) groups=20(staff),12(everyone)
// docker内
uid=501(myName) gid=1000(myName) groups=1000(myName)
①.envの作成
UID=501
GID=1000
USERNAME=myName
②docker-compose.yamlに追記
version: '3.8'
services:
node-dev:
user: $UID:$GID
build:
context: .
args:
UID: $UID
GID: $GID
USERNAME: $USERNAME
postgres:
image: postgres:14.2-alpine
container_name: postgres
ports:
- 5432:5432
volumes:
- ./docker/postgres/init.d:/docker-entrypoint-initdb.d
- ./docker/postgres/pgdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_INITDB_ARGS: '--encoding=UTF-8'
POSTGRES_DB: postgres
PGDATA: /var/lib/postgresql/data/pgdata
hostname: postgres
restart: always
user: root
pgadmin:
image: dpage/pgadmin4
restart: always
ports:
- 81:80
environment:
PGADMIN_DEFAULT_EMAIL: nestjs@example.com
PGADMIN_DEFAULT_PASSWORD: password
volumes:
- ./docker/pgadmin:/var/lib/pgadmin:z
depends_on:
- postgres
user: root
③Dokcerfileを作成
FROM ubuntu
WORKDIR /app
ARG UID
ARG GID
ARG USERNAME
ENV UID ${UID}
ENV GID ${GID}
ENV USERNAME ${USERNAME}
RUN groupadd -g ${GID} ${USERNAME}
RUN useradd -u ${UID} -g ${USERNAME} -m ${USERNAME}
※上記手順は下記の資料を参考にやりました。
何卒ご教示の程、よろしくお願い致します。