LoginSignup
0
1

More than 3 years have passed since last update.

ホストOS上のEncFSで暗号化したディレクトリを、Dockerコンテナにマウント

Last updated at Posted at 2019-06-23

はじめに

まず掲題の内容を実現することで私が期待することを、以下に箇条書きで記載します。

  1. ホストOS上のプロジェクト毎のディレクトリは、EncFSを使って暗号化
  2. Dockerコンテナで、RailsサーバをDevelopmentモードで起動
    • データボリュームとして、1のディレクトリをマウント
    • このディレクトリ内でRailsサーバを起動
  3. 1のディレクトリ内のファイルをホストOS上で編集
  4. 2で起動しているRailsサーバに変更が反映される

要約すると、ホストOS上のEncFSで暗号化したディレクトリ配下のファイルをエディタで編集した内容が、
Dockerコンテナで起動しているRailsアプリに即時反映されることを、目的としています。
上記の実現は簡単だと思ったのですが、EncFS関連で若干詰まったので、再度詰まらないための備忘録として、本記事を記載します。

環境

# OS
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"
# EncFS
$ encfs --version
encfs バージョン 1.9.2
# Docker
$ docker version
Client:
 Version:           18.09.5
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        e8ff056
 Built:             Thu May  9 23:11:19 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.09.5
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       e8ff056
  Built:            Thu May  9 22:59:19 2019
  OS/Arch:          linux/amd64
  Experimental:     false

何もせず、EncFSで暗号化したディレクトリを指定した場合

まずテスト用にEncFSで暗号化したディレクトリを作成します。

$ cd ~/tmp
$ mdkir .test
$ mkdir {,.}test
$ encfs ~/tmp/.test ~/tmp/test
新しい暗号化ボリュームを作成します。
Please choose from one of the following options:
 enter "x" for expert configuration mode,
 enter "p" for pre-configured paranoia mode,
 anything else, or an empty line will select standard mode.
?>

標準の設定が選択されました。

設定が完了しました。以下のプロパティのファイルシステムが
作成されます:
ファイルシステム暗号アルゴリズム: "ssl/aes", バージョン 3:0:2
Filename encoding: "nameio/block", version 4:0:2
鍵サイズ: 192 ビット
ブロックサイズ: 1024 バイト
Each file contains 8 byte header with unique IV data.
Filenames encoded using IV chaining mode.
File holes passed through to ciphertext.

Now you will need to enter a password for your filesystem.
You will need to remember this password, as there is absolutely
no recovery mechanism.  However, the password can be changed
later using encfsctl.

新しい Encfs パスワード:
Encfs パスワードの確認:
# 確認用に、適当なファイル名でファイルを作成
$ touch test/a.txt

次に上記で作成したディレクトリをボリュームとして、Dockerコンテナを起動します。

$ docker run -v ${PWD}/test:/test -it --rm busybox ls /test
docker: Error response from daemon: error while creating mount source path '/home/takuya/tmp/test': mkdir /home/takuya/tmp/test: file exists.

上記のようなエラーが出て、コンテナの起動に失敗してしまいます。

解決方法

エラーメッセージからは読み取れませんが、EncFSの実行時に allow_root オプションを追加することで解決しました。
まず /etc/fuse.conf を以下の差分のように編集します。

@@ -5,4 +5,4 @@
 #mount_max = 1000

 # Allow non-root users to specify the allow_other or allow_root mount options.
-#user_allow_other
+user_allow_other

次に allow_root オプションを追加して、encfs コマンドを実行します。

# マウントした状態で、再度マウントしようとするとエラーになるため、アンマウント
$ fusermount -u ./test
# allow_root オプションを追加して実行
$ encfs -o allow_root ~/tmp/.test ~/tmp/test

先程失敗した、Dockerコンテナの起動を試してみます。

$ docker run -v ${PWD}/test:/test -it --rm busybox ls /test
a.txt

Dockerコンテナが無事起動し、ホストOS上に作成した a.txtls で確認することが出来ました。

おわりに

指定するディレクトリを適宜変更することで、「はじめに」に書いたことが実現可能になりました。
同様の問題に悩んでいる人が居るかどうか分かりませんが、本記事の情報が問題解決の一助になれば幸いです。

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