LoginSignup
57
53

More than 5 years have passed since last update.

Dockerコンテナ内にsudoユーザを追加する

Last updated at Posted at 2017-01-27

事の起こり

re:dashセットアップ手順
上記を見て、re:dashをdockerで動かしてみようかと手を動かした時に、
sudoユーザでインストールするようなプログラムだけど、
今まではdockerはrootでやっていたので、
いい機会なのでsudoユーザ作成についてまとめました。

ゴール

  • 追加するsudoユーザ情報
ユーザ名 ユーザが所属するグループ ユーザのpassword
iganari developer hogehoge

Dockerfile

  • ubuntu:16.04で構築しますが、他のOSでも基本的にいけると思います。
    • 中身
Dockerfile
FROM ubuntu:16.04
MAINTAINER iganari <iganari@qiita.com>

# apt update
RUN apt-get update
RUN apt-get install -y sudo

# add sudo user
RUN groupadd -g 1000 developer && \
    useradd  -g      developer -G sudo -m -s /bin/bash iganari && \
    echo 'iganari:hogehoge' | chpasswd

RUN echo 'Defaults visiblepw'             >> /etc/sudoers
RUN echo 'iganari ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER iganari
  • 実行コマンド
cmd
# docker build --no-cache .
# docker tag $(docker images -q | head -1) sudo-test
# docker run --rm -it sudo-test /bin/bash

================
コンテナ内でsudo有りと無しで確認してみる

### sudo無し
iganari@4b6360fbafce:/$ apt-get update
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?


### sudo有り
iganari@4b6360fbafce:/$ sudo apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu xenial-security InRelease
Reading package lists... Done

以上で、sudoユーザが出来ました。

ソース

Gistにも載せておきます。

補足

ハマったポイントとしては
echo 'Defaults visiblepw' >> /etc/sudoers
これが見慣れなくてすこし調べました。

ちなみに、当初の目的であるredashを入れるところは別途構築中です…

参考

dockerでsudoできるユーザを追加するdockerfile
ssh で sudo 実行
Jenkinsでsudo実行時のエラー

57
53
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
57
53