3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

docker-composeでSSHコンテナを作ってみた

Last updated at Posted at 2021-04-07

概要

Webアプリケーションから別のサーバーへSSH接続またはSFTP接続することを想定した、開発環境用のdocker構成をご紹介します。

環境

  • macOS Big Sur バージョン11.2.3
  • Docker version 20.10.5, build 55c4c88
  • docker-compose version 1.28.5, build c4eb3a1f

構成

├── docker
│   ├── storage
│   │   └── Dockerfile
│   └── web
│       └── Dockerfile
├── docker-compose.yml
├── storage
└── web
    └── index.html

手順

1. キーペアの作成

キーペアを保存するためのディレクトリを作成して、その中にキーペアを作成します。

mkdir -p docker/.ssh
cd docker/.ssh
ssh-keygen -t rsa -b 4096 -N "" -f test_rsa

docker/.sshの中に、test_rsatest_rsa.pubができていればOKです。

2. docker-compose.ymlの作成

今回は検証用に2つのコンテナを用意します。

docker-compose.yml
version: '3.8'
services:
  web:
    build: ./docker/web
    volumes:
      - ./web:/var/www/html
      - ./docker/.ssh/test_rsa:/root/.ssh/test_rsa
    ports:
      - '80:80'
  storage:
    build: ./docker/storage
    volumes:
      - ./storage:/storage
      - ./docker/.ssh/test_rsa.pub:/root/.ssh/authorized_keys
    ports:
      - '10022:22'

webコンテナ: SSH元のホスト想定です。秘密鍵test_rsaをマウントしています。
storageコンテナ: SSH先のホスト想定です。公開鍵test_rsa.pubをマウントしています。

3. Dockerfileの作成

dockerディレクトリにstoragewebディレクトリをそれぞれ作成します。

webコンテナのDockerfile

docker/webにDockerfileを作成します。

FROM php:7.4-apache

RUN apt-get update && apt-get install -y openssh-server

storageコンテナのDockerfile

docker/storageにDockerfileを作成します。

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd

RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22

CMD chmod 0600 ~/.ssh/authorized_keys &&  \
    /usr/sbin/sshd -D

4. webアプリケーションを作成

webアプリケーションの処理の中でSSH接続したり、SFTPでファイルをアップロードやダウンロードする想定ですが、めんど...お時間の都合上(笑)、とりあえずはwebページが表示できるだけのものにしています。
webの中に、index.htmlを作成します。(作らなくてもいいです。)

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>テスト</title>
</head>
<body>
<p>Webアプリケーションコンテナ</p>
</body>
</html>

5. dockerの起動

いつも通りのコマンドです。

docker-compose up -d

6. 確認

SSHユーザーはrootになります。

ローカルホストからのSSH接続

localhost10022storageコンテナの22と繋がっているので、以下のコマンドで接続できます。

# 秘密鍵を置いているディレクトリに移動
cd docker/.ssh

# SSH接続
ssh -i test_rsa root@localhost -p 10022

webコンテナからのSSH接続

コンテナ間の通信の場合は、ホスト名はコンテナ名を利用するようになりますので、以下のコマンドで接続できます。
ちなみに、portは22なので、オプションの指定はなくても問題ありません。

# webコンテナに入ります
docker-compose exec web bash

# 秘密鍵を置いているディレクトリに移動
cd /root/.ssh

# SSH接続
ssh -i test_rsa root@storage -p 22

参考

Docker | 公開鍵でコンテナにssh接続する出来るようにするDockerfileの例

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?