LoginSignup
15
15

More than 5 years have passed since last update.

sudoできるユーザの追加をDockerfileに書いてみた

Last updated at Posted at 2018-02-03

環境
Windows 10 Pro 1709(16299.192)
Docker version 17.12.0-ce, build c97c6d6
CentOS Linux release 7.4.1708 (Core)

前提

・「Docker for Windows」のインストール済み
・「CentOS7」の「Dockerイメージ」をダウンロード済み または 作成済み
Refer to Window10にCentOS7のdockerコンテナを作ってみた

1.「Dockerfile」を作成

Dockerfile
FROM centos
LABEL maintainer "michiru <0ashina0@gmail.com>"

# ROOTにパスワードをセット
RUN echo 'root:newpassword' |chpasswd

# ユーザを作成
RUN useradd newuser
RUN echo 'newuser:newuserpass' |chpasswd
RUN echo "newuser    ALL=(ALL)       ALL" >> /etc/sudoers

# インストール済みのパッケージをアップデート&キャッシュのクリア
RUN yum -y update && yum clean all

# sudoをインストール
RUN yum install -y sudo

2.「Dockerイメージ」の作成

PS C:\Users\UserName> docker build ./ -t useradd

3.確認

「Dockerコンテナ」の作成&起動

PS C:\Users\UserName> docker run -it useradd

追加ユーザの確認

CentOSで実行
[root@7d831becc17d /]# su - newuser
[newuser@7d831becc17d ~]$ sudo ls

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for newuser:
[newuser@7d831becc17d ~]$ su - root
Password:
Last failed login: Sat Feb  3 14:14:57 UTC 2018 on pts/0
There was 1 failed login attempt since the last successful login.
[root@7d831becc17d ~]# logout
[newuser@7d831becc17d ~]$ logout
[root@7d831becc17d /]# exit
PS C:\Users\UserName>

「Dockerコンテナ」の再起動

PS C:\Users\UserName> docker start 7d831becc17d
PS C:\Users\UserName> docker attach 7d831becc17d

追加ユーザで2回目のsudoでコマンド実行。(パスワードなし)

[root@7d831becc17d /]# su - newuser
Last login: Sat Feb  3 14:14:20 UTC 2018 on pts/0
[newuser@7d831becc17d ~]$ sudo touch aaa
[newuser@7d831becc17d ~]$ sudo ls
aaa
[newuser@7d831becc17d ~]$ su - root
Password:
Last login: Sat Feb  3 14:15:09 UTC 2018 on pts/0
Last failed login: Sat Feb  3 14:19:39 UTC 2018 on pts/0
There was 1 failed login attempt since the last successful login.
[root@7d831becc17d ~]#
15
15
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
15
15