0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dokcerコンテナ間でSSHできるようにする

Last updated at Posted at 2025-02-15

はじめに

Dockerコンテナ間でパスワード認証でSSHできるようにする設定がわからず、
調査に手間取ったので共有
環境について一通り書いたあとに詰まった個所と対策について記述している

環境

Docker

todotodo@vm:~/Endlessh$ docker --version
Docker version 27.5.1, build 9f9e405

ディレクトリ構成

.
├── attacker_context
│   └── Dockerfile
├── docker-compose.yml
└── pot_context
    ├── Dockerfile
    └── start.sh

docker-compose.yml

docker-compose.yml
version: '3'

networks:                           #コンテナが所属するネットワークを定義
  Endless:
    ipam:
      driver: default
      config:
        - subnet: 11.11.11.0/24     #コンテナが所属するサブネット

services:
  attacker:
    build: attacker_context
    tty: true
    networks:
      Endless:                      #コンテナが所属するネットワークを指定
        ipv4_address: 11.11.11.2    #コンテナのIPアドレスを指定

  pot:
    build: pot_context
    tty: true
    expose:                         #他コンテナからアクセスできるポート。
      - "2222"
      - "22"          
    networks:
      Endless:                   #コンテナが所属するネットワークを指定
        ipv4_address: 11.11.11.3    #コンテナのIPアドレスを指定

pot_context/Dockerfile

pot_context/Dockerfile
RUN apt-get update && \
    apt-get install -y \
    build-essential \
    cmake \
    git \
    sudo \
    wget \
    vim \
    openssh-server \
    openssh-client \
    iputils-ping

COPY ./start.sh /start.sh
RUN chmod +x /start.sh
CMD /start.sh

attacker_context/Dockerfile

attacker_context/Dockerfile
FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y \
    build-essential \
    cmake \
    git \
    sudo \
    wget \
    vim \
    openssh-server \
    openssh-client \
    iputils-ping

start.sh

start.sh
#!/bin/bash
echo "PORT 22" >> /etc/ssh/sshd_config
echo "PORT 2222" >> /etc/ssh/sshd_config
adduser --disabled-password --gecos "" "test"
echo "test:test" | chpasswd
service ssh restart
tail -f /dev/null

コンテナの起動

$ docker compose build
$ docker compose up -d

SSH接続

attackerコンテナに入ってpotコンテナにsshする

$ docker compose exec attacker bash
root@75db6a563d01:/# ssh test@11.11.11.3
test@11.11.11.3's password: 
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-36-generic x86_64)
<snip>
Last login: Sat Feb 15 09:20:22 2025 from 11.11.11.2
test@6b9757d2f085:~$ 

つまった個所

コンテナ間で通信可能なポートが開いていない

docker-compose.ymlexposeでコンテナ間通信に使うポートを設定する

openssh-server, clientがinstallされていない

ubuntu:latestは最低限の構成となっており、openssh-serveropenssh-clientは含まれていないので
RUN apt-get install openssh-server openssh-clientでinstallしている

port 22: Connection refused エラーがでてSSHできない

pingを打ってみてポート22での疎通に問題がないことを確認する

root@cbd78eb629f7:/# ping 11.11.11.3 -p 22
PATTERN: 0x22
PING 11.11.11.3 (11.11.11.3) 56(84) bytes of data.
64 bytes from 11.11.11.3: icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from 11.11.11.3: icmp_seq=2 ttl=64 time=0.077 ms

SSHで使用可能なポートを指定するために、/etc/ssh/sshd_configPORT 22を追記する必要がある
その後service ssh restartでsshを再起動する

start.sh
echo "PORT 22" >> /etc/ssh/sshd_config
service ssh restart

SSH接続できるようになる

root@75db6a563d01:/# ssh test@11.11.11.3
test@11.11.11.3's password: 
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-36-generic x86_64)
<snip>
Last login: Sat Feb 15 09:20:22 2025 from 11.11.11.2
test@6b9757d2f085:~$ 

docker-compose up でコンテナは立ち上がるが即座に終了してしまう

DockerfileCMDでコンテナ起動時に実行したいスクリプトstart.shを指定している
その場合、スクリプト実行後にコンテナが終了してしまう
終了させたくない場合はスクリプト末尾に下記を追加する
tail -f /dev/null
tail -fは指定したファイルに変更があった際に表示する機能だが、監視対象が/dev/nullという虚無なので何も起こらない
つまり、tail/dev/nullの変更を監視し続けてコンテナが終了しない

参考資料
https://qiita.com/ken992/items/872a90736e6af26ef4ab

まとめ

コンテナ間でパスワードを使用したSSHができるようになった
自分でやろうとした際につまった個所が複数あったのでまとめてみた
誰かの助けになったらうれしいです

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?