
スポンサーリンク
Docker Desktopを用いて、Ubuntu20.04で作成したDockerコンテナにパスワード認証でSSH接続する方法を解説します。
前提
- Windows環境にDocker Desktopをインストールしていること。
> docker --versionDocker version 26.0.0, build 2ae903e
-
ubuntu 24.04のサーバを例に説明する。
-
Docker Desktop
を起動していること。
Dockerfile内容
以下内容のDockerfile
を例に説明します。
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y openssh-serverRUN mkdir /var/run/sshdRUN adduser test-user --disabled-passwordRUN echo 'test-user:passw0rd' | chpasswdRUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config
EXPOSE 22CMD ["/usr/sbin/sshd", "-D"]
上記ファイルを任意のフォルダに保存してください。
なおSSHのログインユーザはtest-user
、パスワードはpassw0rd
となります。
PowerShell起動
PowerShellを起動し、Dockerfile
の保存フォルダへ移動します。
cd [Dockerfile保存フォルダ]
Dockerイメージ作成
以下コマンドでDockerfileからイメージを作成します。
docker build -t my_ubuntu_24.04 .
-t
はイメージの名称とタグ名称を指定するオプションです。
PS XXX> docker build -t my_ubuntu_24.04 .[+] Building 0.0s (0/0) docker:default2024/10/20 21:29:10 http2: server: error reading preface from client //./pipe/docker_engine: file has already been close[+] Building 4.4s (10/10) FINISHED docker:default => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 362B 0.0s => [internal] load metadata for docker.io/library/ubuntu:24.04 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/6] FROM docker.io/library/ubuntu:24.04 0.0s => CACHED [2/6] RUN apt-get update && apt-get install -y openssh-server 0.0s => CACHED [3/6] RUN mkdir /var/run/sshd 0.0s => [4/6] RUN adduser test-user --disabled-password 1.9s => [5/6] RUN echo 'test-user:passw0rd' | chpasswd 1.0s => [6/6] RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config 0.8s => exporting to image 0.4s => => exporting layers 0.3s => => writing image sha256:aa41c0f91405847ba6d392663540b41718117183c6598cd9c9b46189a5e0ad1f 0.0s => => naming to docker.io/library/my_ubuntu_24.04 0.0s
What's Next? View a summary of image vulnerabilities and recommendations → docker scout quickview
作成後、イメージ確認
以下コマンドで作成されたイメージを確認できます。
docker images
REPOSITORY TAG IMAGE ID CREATED SIZEmy_ubuntu_24.04 latest aa41c0f91405 26 seconds ago 259MB
Dockerコンテナ作成・起動
以下コマンドでコンテナを作成・起動します。
docker run -d -p 2222:22 my_ubuntu_24.04
コンテナ作成・起動後、確認
以下コマンドで起動したコンテナを確認することができます。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc97efccb4fa4 my_ubuntu_20.04 "/usr/sbin/sshd -D" 27 seconds ago Up 24 seconds 0.0.0.0:2222->22/tcp dreamy_easley
SSHログイン確認
最後にSSHコマンドでログインできることを確認します。
ssh test-user@localhost -p 2222
「password:」にはpassw0rd
を指定してください。
The authenticity of host '[localhost]:2222 ([::1]:2222)' can't be established.ED25519 key fingerprint is SHA256:XXX.This key is not known by any other names.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '[localhost]:2222' (ED25519) to the list of known hosts.test-user@localhost's password:Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.15.146.1-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro
This system has been minimized by removing packages and content that arenot required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
The programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.
test-user@e3aac2e193be:~$
上記のようにログインできればOKです。
TeraTermといったツールを利用してもログインすることができます。
また、ログインはパスワード認証となっているため、ログイン確認後は鍵認証へ設定を変更することを推奨します。
以上で本記事の解説を終わります。
よいITライフを!