DockerコンテナにSSHでログインする

DockerコンテナにSSHでログインする

Amazonのアソシエイトとして、ITナレッジライフは適格販売により収入を得ています。

記事の文字数:666 / 総アクセス数:233 views

Docker Desktopを用いて、Ubuntu20.04で作成したDockerコンテナにパスワード認証でSSH接続する方法を解説します。

Docker Desktopを用いて、Ubuntu20.04で作成したDockerコンテナにパスワード認証でSSH接続する方法を解説します。

前提

  • Windows環境にDocker Desktopをインストールしていること。
確認バージョン
> docker --version
Docker version 26.0.0, build 2ae903e
  • ubuntu 24.04のサーバを例に説明する。

  • Docker Desktopを起動していること。

alt text

Dockerfile内容

以下内容のDockerfileを例に説明します。

Dockerfile
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN adduser test-user --disabled-password
RUN echo 'test-user:passw0rd' | chpasswd
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config
EXPOSE 22
CMD ["/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:default
2024/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 SIZE
my_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 NAMES
c97efccb4fa4 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])? yes
Warning: 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 are
not 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 the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
test-user@e3aac2e193be:~$

上記のようにログインできればOKです。
TeraTermといったツールを利用してもログインすることができます。
また、ログインはパスワード認証となっているため、ログイン確認後は鍵認証へ設定を変更することを推奨します。

Dockerユーザにお勧めの本

おすすめ

開発系エンジニアのためのDocker絵とき入門

難易度
実用性
読みやすさ

コンテナの概念を絵で理解できる。開発環境の構築に苦手意識がある人にこそ読んでほしい一冊。

仕組みと使い方がわかる Docker&Kubernetesのきほんのきほん

難易度
実用性
読みやすさ

DockerだけでなくK8sにも触れている。モダンなインフラ構築の第一歩に最適です。

Docker&仮想サーバー完全入門 Webクリエイター&エンジニアの作業がはかどる開発環境構築ガイド

難易度
実用性
読みやすさ

実践的な開発環境構築に特化。設定ファイルの書き方など、すぐに使える知識が豊富です。

Ubuntuユーザにお勧めの本

Ubuntuサーバー徹底入門

難易度
実用性
網羅性

設定の勘所が詳しく解説されている。Ubuntu運用の強い味方になってくれる一冊です。

ゼロからわかるLinuxサーバー超入門 Ubuntu対応版 かんたんIT基礎講座

難易度
実用性
読みやすさ

非常に丁寧に手順が示されている。独学で初めてサーバーを立てるならこの本が最適です。


以上で本記事の解説を終わります。
よいITライフを!

Dockerユーザにお勧めの本

おすすめ

開発系エンジニアのためのDocker絵とき入門

難易度
実用性
読みやすさ

コンテナの概念を絵で理解できる。開発環境の構築に苦手意識がある人にこそ読んでほしい一冊。

人気記事


記事を評価

Thanks!
目次
Scroll to Top