メンチカツには醤油でしょ!!

AWS/Java/Node.js/Spreadsheets/Docker/Jenkins/コミュニティ・勉強会レポを主とした技術系ブログ

DockerでFTPサーバーを構築したら簡単すぎた

pullしてrunするだけがいい

色々検討しても良いのかも知れませんが先日のOracle Databaseの件のように動かないコンテナに苦労したり、不可能な環境構築にトライしたくない領域だったので、今回は最初からDocker Hubを検索。Most downloads, most starsを選択しました。
https://hub.docker.com/r/stilliard/pure-ftpd/

なお、SFTPならコチラがMost downloads, most starsでした。
https://hub.docker.com/r/atmoz/sftp/

さて、通常はドキュメント通りに進めますがpure-ftpd:hardened だとアップロード後のリネームができなかったりしたので、pur-ftpd:latestを使います。

> docker pull stilliard/pure-ftpd:latest

latest: Pulling from stilliard/pure-ftpd
386a066cd84a: Pull complete
:::::略:::::: Pull complete
26eb4ca837a0: Pull complete
Digest: sha256:d21f3d12be52d8669b5561c7325f60013b61c9920a78db9d85e2e1545cbdae4f
Status: Downloaded newer image for stilliard/pure-ftpd:latest

> docker run -d --name ftpd_server -p 21:21 -p 30000-30009:30000-30009 -e "PUBLICHOST=localhost" stilliard/pure-ftpd:latest

0206a6e60ff6f738a1e23beecdb46d4631da2e654962dc18e1c7526e325cc840

> docker ps -a

CONTAINER ID  IMAGE                       COMMAND                 CREATED                 STATUS        PORTS                                                     NAMES
0206a6e60ff6  stilliard/pure-ftpd:latest  "/bin/sh -c '/run...."  Less than a second ago  Up 3 seconds  0.0.0.0:21->21/tcp, 0.0.0.0:30000-30009->30000-30009/tcp  ftpd_server

> docker exec -it 0206a6e60ff6 bash

root@0206a6e60ff6:/# cd /home/ftpusers/

root@0206a6e60ff6:/home/ftpusers# ls -la

total 8
drwxr-xr-x 2 root root 4096 Feb 7 20:39 .
drwxr-xr-x 3 root root 4096 Nov 30 08:28 ..

root@0206a6e60ff6:/home/ftpusers# pure-pw useradd my-account-name -f /etc/pure-ftpd/passwd/pureftpd.passwd -m -u ftpuser -d /home/ftpusers/my-account-name

Password: ********
Enter it again: ********

root@0206a6e60ff6:/home/ftpusers# mkdir my-account-name

root@0206a6e60ff6:/home/ftpusers# chown ftpuser:ftpgroup my-account-name

root@0206a6e60ff6:/home/ftpusers# ls -la

total 12
drwxr-xr-x 3 root root 4096 Feb 7 20:41 .
drwxr-xr-x 3 root root 4096 Nov 30 08:28 ..
drwxr-xr-x 3 ftpuser ftpgroup 4096 Feb 7 20:41 my-account-name

/home/ftpuser/【設定したアカウント名】
ディレクトリが作成されていることが確認できます。
ここでFFFTPなどでファイルアップロードなどします。
ユーザー名とパスワードはpure-pwコマンドの時に設定したものです。
(下記の例はtestというディレクトリを作成した例)

root@0206a6e60ff6:/home/ftpusers# cd my-account-name

root@0206a6e60ff6:/home/ftpusers/bob# ls -la

total 12
drwxr-xr-x 3 ftpuser ftpgroup 4096 Feb 7 20:41 .
drwxr-xr-x 3 root root 4096 Feb 7 20:41 ..
drwxr-xr-x 2 ftpuser ftpgroup 4096 Feb 7 20:41 test

簡単すぎて鼻血出そう...

Gitでssh/cloneした時にPermission denied (publickey).

sshは通るけどcloneはできない

あると思います。そんな時のためのメモです。

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

これですよね。

ssh-keygenでid_rsa以外を指定

ssh-keygenした時にkeyのファイル名を変更していると上記のエラーが出ると思います。(たとえばgithub_id_rsa, github_id_rsa.pub のように)

GitHubのSettings⇒SSHで公開鍵登録しても、
sshは通るけど、cloneはできないという状態になると思います。謎。

この場合は、ssh-addをする必要があります。
デフォルトの名前(id_rsa)であれば生成時にssh-addされるようなのですが
自分でsshキー名を(変更)指定した場合はssh-addされないようです。

一発で決めたい

事前に色々やってた方はまず下記を。

~/.ssh $ vi known_hosts
(known_hostsに登録されていたら面倒なので一旦消しましょう。viの行削除はdd。保存して終了は:wq!です)
~/.ssh $ rm github*
(再度キーを生成しますので一旦うまくいかなかった生成キー消しちゃいましょう)

綺麗になった所で、下記のように一通でいきましょう!

~/.ssh $ ssh-keygen -t rsa -C "my-mailaddress-for-git@mail.com"

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/my-computer-name/.ssh/id_rsa): github_id_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in github_id_rsa.
Your public key has been saved in github_id_rsa.pub.
The key fingerprint is:
SHA256:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq my-mailaddress-for-git@mail.com
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|   o             |
|  . *            |
| . * X   .       |
|o * @ @ S        |
|o+.O X +         |
|Eo++* +          |
|.=+=++ +         |
|..oo o+ .        |
+----[SHA256]-----+

(ここでGitHubにてSettings->SSHを開き、生成した鍵の公開鍵の方(github_id_rsa.pubの方)を登録しましょう)


~/.ssh $ ssh -i github_id_rsa git@github.com The authenticity of host 'github.com (192.30.253.113)' can't be established. RSA key fingerprint is SHA256:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts. PTY allocation request failed on channel 0 Hi ryoichi-obara! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed. ~/.ssh $ ssh-add ~/.ssh/github_id_rsa Identity added: /Users/my-computer-name/.ssh/github_id_rsa (/Users/my-computer-name/.ssh/github_id_rsa)

これでも
Could not open a connection to your authentication agent.
などとエラーが出る場合はssh-addする前にこのようにしましょう。

~/.ssh $ eval "$(ssh-agent)"

Agent pid 59566

~/.ssh $ ssh-add ~/.ssh/github_id_rsa

Enter passphrase for ~/.ssh/github_id_rsa: 

$ git clone git@github.com:ryoichi-obara/test.git

(成功)

ssh-addが消えて立ち上げるたびに毎回やらないといけない問題

Windows環境(git bash)のみですかね。
.bashrcに書いてgit bash起動時に実行されるようにしておけば良さそうです、、と以前まで書いていたのですがssh config (~/.ssh/config) に書けば良さそうです。
ssh configは、sshの接続設定をまとめて定義できるファイルです。

一般的にHost名はただの設定名ですが、ここでgithub.comと設定しないと繋がらないです。これにずっと悩まされてましたが解決!

Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/github_id_rsa

この設定は

$ ssh git@github.com -p 22 -i ~/.ssh/github_id_rsa

と等価です。設定名に github と指定しているので、

$ ssh github

で繋がるようになります。

--- ↓ 旧(一応) .bashrcに書くやり方 ---
$ vi ~/.bashrc
eval `ssh-agent`
ssh-add ~/.ssh/github_id_rsa

なお、立ち上げたときに
WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile.
が出て気になる場合は

$ touch ~/bash_profile

などとして空の.bash_profileを作っておけば警告は出なくなります。

参考

Mac ssh-add 自動 - @//メモ
https://hondou.homedns.org/pukiwiki/index.php?Mac%20ssh-add%20%BC%AB%C6%B0

GitHubSSHに関してのドキュメント
Error: Permission denied (publickey) - User Documentation

d.hatena.ne.jp

fa11enprince.hatenablog.com

過去にこんな記事も書いてました

ryoichi0102.hatenablog.com

ryoichi0102.hatenablog.com

Docker上のOracleで初期化処理したい

Docker上のOracleで初期化処理したい

ryoichi0102.hatenablog.com

先日立てたOracle on Dockerですが、初期化処理として
・ユーザーやスキーマが作りたい
・初期データを投入したい
ということがもちろんあり、その記録です。

※追記

なお、現在は公式のOracle Dockerイメージからdocker run~初期化sqlやデータのimportができます。

ryoichi0102.hatenablog.com

Dockerfileを書いてビルド?

諦めました。
下記にソース引用しましたが、bashではなくshでの実行になるので、このコンテナだとやれることが少ないです。

結局、ボリュームマウントで初期化

~/docker/oracle/init/start-up.sh
~/docker/oracle/init/01_create_USER1.ddl
~/docker/oracle/init/02_DATA_USER1.dmp
~/docker/oracle/init/03_create_USER2.ddl
~/docker/oracle/init/04_DATA_USER2.dmp

こんな感じに初期処理が記載されたシェル (start-up.sh)と、user作成用のDDLとdmpファイルを用意して ~/docker/oracle/init 配下などに置きます。

シェルの内容は、

#!/bin/bash

export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=XE

/u01/app/oracle/product/11.2.0/xe/bin/sqlplus SYS/oracle@xe as sysdba @/docker-entrypoint-initdb.d/01_create_USER1.ddl
/u01/app/oracle/product/11.2.0/xe/bin/imp     USER1/user1Pass@xe  file=/docker-entrypoint-initdb.d/02_DATA_USER1.dmp full=y
/u01/app/oracle/product/11.2.0/xe/bin/sqlplus SYS/oracle@xe as sysdba @/docker-entrypoint-initdb.d/03_create_USER2.ddl
/u01/app/oracle/product/11.2.0/xe/bin/imp     USER2/user2Pass@xe  file=/docker-entrypoint-initdb.d/04_DATA_USER2.dmp full=y

のような感じです。

そして、実行時にボリュームマウントします。

$ docker run -d -p 10022:22 -p 11521:1521 -v ~/docker/oracle/init:/docker-entrypoint-initdb.d -e ORACLE_ALLOW_REMOTE=true --name my-oracle-container wnameless/oracle-xe-11g

docker-entrypoint-initdb.dディレクトリに置いたシェルファイルは、run時に自動実行されるように元コンテナ(wnameless/oracle-xe-11g)で設定されています。

初期化処理がsqlファイルだけで良い場合はそれを置くだけでSYSTEMユーザーで実行してくれるようになっています。(下記ソース参照)

原作者ソース

github.com

for f in /docker-entrypoint-initdb.d/*; do
  case "$f" in
    *.sh) echo "$0: running $f"; . "$f" ;;
    *.sql) echo "$0: running $f"; echo "exit" | /u01/app/oracle/product/11.2.0/xe/bin/sqlplus "SYSTEM/oracle" @"$f"; echo ;;
    *) echo "$0: ignoring $f" ;;
  esac
  echo
done

ミソは↓ここ↓
*.sh) echo "$0: running $f"; . "$f" ;;
となっているのでbashではなくshで実行されるのです。

また、sqlSYSTEMユーザーで実行されます。