LoginSignup
13
9

More than 3 years have passed since last update.

dockerfileでパスワードを求めないsudoer作り(Alpine、Debian、Ubuntu、Centos)

Last updated at Posted at 2020-06-21

紹介する内容

  • Alpine、Debian、Ubuntu、Centosの例です
    • uidとデフォルトshell設定を含みます

結論

  • Alpine linux他はsudoerの作り方が似てます
  • dockerのvolumeでpermission denied問題解決や、作りたいときに使えます

紹介始めます

ディレクトリ構成

全体ソースコードは https://github.com/cheekykorkind/qiita-example/tree/master/dockerfiles/no-password-sudoer で確認できます

  • 全体図 Dockerfileの名はどのLinuxかをわかりやすく表現しました。
    AlpineDockerfileはAlpine 3.11
    CentosDockerfileはCentos 7
    DebianDockerfileはDebian buster
    UbuntuDockerfileはubuntu 18.04allD.png

Dockerfileの中身

  • AlpineDockerfile
    • Alpine linuxだけがsudoerの作り方が違う気がします
FROM alpine:3.11

RUN apk add sudo

RUN adduser -D -u 1001 -s /bin/sh -G wheel alpine-sudoer
RUN echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
  • CentosDockerfile

FROM centos:7

RUN yum -y install sudo

RUN useradd --uid 1001 --create-home --shell /bin/bash -G wheel,root centos-sudoer
RUN echo '%wheel ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
  • DebianDockerfile
FROM debian:buster-slim

RUN apt update && apt install -y sudo

RUN useradd --uid 1001 --create-home --shell /bin/sh -G sudo,root debian-sudoer
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
  • UbuntuDockerfile
FROM ubuntu:18.04

RUN apt update && apt install -y sudo

RUN useradd --uid 1001 --create-home --shell /bin/bash -G sudo,root ubuntu-sudoer
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

試し順番です

  1. デレクトリー移動

    • cd qitta-example/dockerfiles/no-password-sudoer
  2. dockerコンテナをバックグラウンドで起動

    • docker-compose up -d
  3. 各々のdockerコンテナにsudoerが作られたか確認します

    • docker exec -it alpine-sudoer /bin/sh -e -c "cat /etc/passwd | grep alpine-sudoer"
    • docker exec -it debian-sudoer /bin/sh -e -c "cat /etc/passwd | grep debian-sudoer"
    • docker exec -it ubuntu-sudoer /bin/bash -e -c "cat /etc/passwd | grep ubuntu-sudoer"
    • docker exec -it centos-sudoer /bin/bash -e -c "cat /etc/passwd | grep centos-sudoer" checkSudoers.png
  4. (選択)自由に確認したいなら、入りたいdockerコンテナに入ります

    • docker exec -it alpine-sudoer /bin/sh
    • docker exec -it debian-sudoer /bin/sh
    • docker exec -it ubuntu-sudoer /bin/bash
    • docker exec -it centos-sudoer /bin/bash
13
9
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
13
9