3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dockerコンテナを一般ユーザーで操作する

Posted at

はじめに

コンテナを作成した時、デフォルトのユーザーはrootユーザーになっています。
そのため、コンテナ内で作成したファイルは権限がroot権限になってしまい、コンテナの外での編集に支障をきたしてしまいます。そのため、下記の流れでユーザーの設定を行います。

1..envに環境変数を記載
2.docker-compose.ymlのargsにて、Dockerfileに渡す環境変数を記載
3.Dockerfileにてグループとユーザーを追加&使用者を一般ユーザーに変更

docker-compose.yml

.env
UID=1000
GID=1000
USERNAME=user_name
docker-compose.yml
version: "3"
services:
  app:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
      args:
        # Dockerfileに環境変数を渡す
        - UID=${UID}
        - GID=${GID}
        - USERNAME=${USERNAME}

Dockerfile

Dockerfile
# イメージを指定
FROM php

# 中略

# docker-compose.ymlで設定した環境変数を導入
ARG UID
ARG GID
ARG USERNAME

# グループ・ユーザーを追加
RUN groupadd -g ${GID} ${USERNAME} \
  && useradd -u ${UID} -g ${GID} -s /bin/bash -m ${USERNAME}

# コンテナを操作するユーザーをルートから一般ユーザーに変更
USER ${UID}
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?