2025/05/04 Updated by

Docker Image を自作する

ubuntu 24.04LTS, sshd


[Up] Japanese English

sshd が自動起動する ubuntu24.04LTS の docker Image を生成する

方針

作成手順

  1. 作業用フォルダを作成する
  2.   $ mkdir -p ~/doc/docker/ubuntu24_sshd
      $ cd ~/doc/docker/ubuntu24_sshd
    
  3. 作業用フォルダの中に Dockerfile を作成する。Dockerfile中の パスワード の部分は、推測されにくい文字列に必ず変更すること。
  4. Dockerfile
    # ゲストOS: Ubuntu 24.04 LTS
    
    FROM ubuntu:24.04
    
    
    # Change Your Own UNAME, UID, GID, PASS
    
    ENV UNAME=guest
    ENV UID=3000
    ENV GID=3000
    ENV PASS=パスワード
    
    ENV SSHD_PORT=22
    
    # 必要なパッケージのインストール
    
    RUN apt-get update && \
        DEBIAN_FRONTEND=noninteractive apt-get install -y \
        sudo \
        openssh-server \
        supervisor \
        && rm -rf /var/lib/apt/lists/*
    
    
    # SSH 設定: パスワード認証を有効化
    
    RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
        sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config && \
        sed -i "s/^#Port.*/Port ${SSHD_PORT}/" /etc/ssh/sshd_config && \
        mkdir /var/run/sshd
    
    
    # supervisord の設定ファイルを設置する (Daemon 起動用)
    
    RUN mkdir -p /var/log/supervisor
    COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    
    
    # ポート開放
    
    EXPOSE 22
    
    
    # Copy Shell Script "entrypoint.sh"
    
    COPY entrypoint.sh /entrypoint.sh
    RUN chmod +x /entrypoint.sh
    
    ENTRYPOINT ["/entrypoint.sh"]
    
    CMD []
    
  5. 作業用フォルダの中に supervisord.conf を作成する
  6. supervisord.conf
    # supervisord の設定ファイル
    
    [supervisord]
    nodaemon=true
    logfile=/var/log/supervisor/supervisord.log
    
    # sshd を起動する
    
    [program:sshd]
    command=/usr/sbin/sshd -D
    autostart=true
    autorestart=true
    
  7. 作業用フォルダの中に entrypoint.sh を作成する
  8. entrypoint.sh
    #!/bin/bash
    set -e
    
    # ユーザーが存在しない場合のみ作成する
    if id "${UNAME}" &>/dev/null; then
        echo "User ${UNAME} already exists. Skipping creation."
    else
        # 同名グループが無ければ作成
        if ! getent group "${UNAME}" &>/dev/null; then
            echo "Creating group ${UNAME} with GID=${GID}"
            groupadd -g ${GID} ${UNAME}
        else
            echo "Group ${UNAME} already exists. Skipping group creation."
        fi
    
        echo "Creating user ${UNAME} with UID=${UID}, GID=${GID}"
        useradd -m -u ${UID} -g ${GID} -s /bin/bash ${UNAME}
        echo "${UNAME}:${PASS}" | chpasswd
        adduser ${UNAME} sudo
    fi
    
    
    # SSHD のポート番号を変更する
    sed -i "s/^Port.*/Port ${SSHD_PORT}/" /etc/ssh/sshd_config
    
    
    # ホームディレクトリの Owner が root:root になるので変更する。
    chown -v ${UNAME}:${UNAME}  /home/${UNAME}
    
    
    # supervisord start (background)
    /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf &
    
    
    # Execute Commands in CMD
    if [ "$#" -gt 0 ]; then
        exec "$@"
    else
        wait
    fi
    
  9. Image を build する。
  10.   $ docker build -t ubuntu24-sshd .
      ...
    成功
    
  11. 生成した Image を確認する
  12. $ docker image ls
    REPOSITORY      TAG       IMAGE ID       CREATED              SIZE
    ubuntu24-sshd   latest    1f06d9a9455f   About a minute ago   334MB
    ...
    

Container 用の永続的なファイルシステムを作成する

コンテナに永続的なファイルシステムを提供するために、1777 のパーミッションでフォルダを作っておく。 skicky bit が on (1777) のフォルダには、 「誰でもファイルを作成できるが、作成した本人だけがファイルを変更したり消したりできる」 という特徴がある。

$ sudo mkdir -p /home/docker         ← ディレクトリを作成する
$ sudo chmod 1777 /home/docker       ← 誰でもファイルを作成できるが、作成した本人にしか消去できないモードに設定する
$ ls -ld /home/docker                ← ディレクトリのsticky bit が on になっていることを確認する。
drwxrwxrwt 3 root root 4096  4月 26 15:47 /home/docker

Docker Contaner を生成する (1)

Image ubuntu24-sshd のデフォルトのユーザ情報を用いて、新しい Container ubuntu24-guest を生成する。

  1. Image から Container を生成して起動する。ユーザ情報はデフォルト値 (guest) を利用する。 デフォルトユーザーのホームディレクトリに、ホストOSのディレクトリをマウントしている
  2. $ docker run --name ubuntu24-guest --restart always -p 12022:22  \
        -v /home/docker/guest:/home/guest/doc -d ubuntu24-sshd
    
    起動オプション
  3. Container からマウントされるホスト OS の /home/docker/guest が存在しない場合は、 "docker run" を実行したホストOSのユーザ (nitta) の権限で作成される。
  4. $ ls -ld /home/docker/guest
    drwxr-xr-x 2 nitta nitta 4096  5月  2 16:28 /home/docker/guest
    
  5. 起動した Container の様子を調べる。
  6. $ docker container ls
    CONTAINER ID   IMAGE           COMMAND            CREATED         STATUS         PORTS                   NAMES
    705bbad9c331   ubuntu24-sshd   "/entrypoint.sh"   2 minutes ago   Up 2 minutes   0.0.0.0:12022->22/tcp   ubuntu24-guest
    
  7. Container のログを調べる。
  8. $ docker logs ubuntu24-guest
    Creating group guest with GID=3000
    Creating user guest with UID=3000, GID=3000
    useradd: warning: the home directory /home/guest already exists.
    useradd: Not copying any file from skel directory into it.
    info: Adding user `guest' to group `sudo' ...
    changed ownership of '/home/guest' from root:root to guest:guest
    2025-05-04 14:04:06,031 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
    2025-05-04 14:04:06,033 INFO supervisord started with pid 37
    2025-05-04 14:04:07,037 INFO spawned: 'sshd' with pid 38
    2025-05-04 14:04:08,041 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
    
  9. ホストOS から、ssh を用いてゲストOS にアクセスする。
  10. $ ssh -p 12022 guest@localhost
    guest@localhost's password: パスワード ← エコーバックされない
    (ゲスト OS のシェルが起動する)
    Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.10.14-linuxkit x86_64)
    ...
    Last login: Fri May  2 07:29:20 2025 from 172.17.0.1
    $
    
  11. (ゲストOS上で)ホームディレクトリを調べる。
  12. (ゲストOS上で)
    $ pwd            ← ホームディレクトリのパスを表示する。
    /home/guest
    $ ls -l          ← ホームディレクトリの情報を表示する。
    total 4
    drwxr-xr-x 2 guest guest 4096 May  2 07:28 doc     ← ホストOSの /home/docker/guest がマウントされている
    
  13. (ゲストOS上で) パスワードを変更する
  14. $ passwd
    Changing password for guest.
    Current password: パスワード ← エコーバックされない
    New password:  新しいパスワード ← エコーバックされない
    Retype new password:  新しパスワード ← エコーバックされない
    passwd: password updated successfully
    
  15. (ゲストOS上で) シェルを終了する。
  16. $ exit
    Connection to localhost closed.
    nitta@um580:~/doc/docker$ 
    

Docker Contaner を生成する (2)

Image ubuntu24-sshd を用いて、 ユーザ情報を指定して、SSHDの待ち受けポート番号を変更して、 新しい Container ubuntu24-ssh12023 を生成する。

  1. Image から Container を生成して起動する。下に示すのはユーザ名を nitta とした例であり、 自分の環境に合わせて適切に変更すること。 Container 側では sshd が 12023 番ポートで待ち受けていて、ホストOSは 12023番ポートへのアクセスをそのまま Container に port forwarding する。
  2. $ docker run --name ubuntu24-ssh12023 --restart always \
        -e UNAME=nitta -e UID=2000 -e GID=2000 -e PASS=新しいパスワード \
        -e SSHD_PORT=12023 \
        -p 12023:12023  -v /home/docker/nitta:/home/nitta/doc -d ubuntu24-sshd
    
    起動オプション
  3. 上の指定方法で Container を生成したときは、一旦停止した Container を start する度に 変数の値を指定しないと予想外の挙動をするので注意すること。
  4. Docker Desktop だと、起動時のオプションを覚えているのでそのまま起動して問題がないようだ。
  5. Container からマウントされるホスト OS の /home/docker/geust が存在しない場合は、 "docker run" を実行したホストOSのユーザの権限 (nitta) で作成される。
  6. $ ls -ld /home/docker/nitta
    drwxr-xr-x 2 nitta nitta 4096  5月  2 16:08 /home/docker/nitta
    
  7. 起動した Container の様子を調べる。
  8. $ docker container ls
    CONTAINER ID   IMAGE           COMMAND            CREATED              STATUS              PORTS                              NAMES
    c8096999e40c   ubuntu24-sshd   "/entrypoint.sh"   About a minute ago   Up About a minute   22/tcp, 0.0.0.0:12023->12023/tcp   ubuntu24-ssh12023
    ...
    
  9. Container のログを調べる。
  10. $ docker logs ubuntu24-ssh12023
    Creating group nitta with GID=2000
    Creating user nitta with UID=2000, GID=2000
    useradd: warning: the home directory /home/nitta already exists.
    useradd: Not copying any file from skel directory into it.
    info: Adding user `nitta' to group `sudo' ...
    changed ownership of '/home/nitta' from root:root to nitta:nitta
    2025-05-04 14:45:27,712 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
    2025-05-04 14:45:27,713 INFO supervisord started with pid 37
    2025-05-04 14:45:28,716 INFO spawned: 'sshd' with pid 38
    2025-05-04 14:45:29,719 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
    
  11. ホストOS から、ssh を用いてゲストOS にアクセスする。
  12. $ ssh -p 12023 nitta@localhost
    nitta@localhost's password: パスワード ← エコーバックされない
    (ゲスト OS のシェルが起動する)
    Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.10.14-linuxkit x86_64)
    ...
    Last login: Fri May  2 07:29:20 2025 from 172.17.0.1
    $
    
  13. (ゲストOS上で)ホームディレクトリを調べる。
  14. (ゲストOS上で)
    $ pwd            ← ホームディレクトリのパスを表示する。
    /home/nitta
    $ ls -l          ← ホームディレクトリの情報を表示する。
    total 4
    drwxr-xr-x 2 nitta nitta 4096 May  2 07:28 doc      ← ホストOSの /home/docker/nitta がマウントされている
    
  15. (ゲストOS上で) パスワードを変更する
  16. $ passwd
    Changing password for nitta.
    Current password: パスワード ← エコーバックされない
    New password:  新しいパスワード(長いバージョン) ← エコーバックされない
    Retype new password:  新しパスワード ← エコーバックされない
    passwd: password updated successfully
    
  17. (ゲストOS上で) シェルを終了する。
  18. $ exit
    Connection to localhost closed.
    nitta@um580:~/doc/docker$