LoginSignup
36
40

More than 5 years have passed since last update.

Docker上 で samba を動かしてファイル共有する

Last updated at Posted at 2015-11-15

続き

Docker上で samba を動かす

ZFSのファイルシステムは NFS または SMB(CIFS)で共有することができます。ただし、on Linux では別途 nfs-kernel-serverやsambaをインストールする必要があるため1 今回はZFSを通じて共有するのではなく、Docker上でsambaを動かしてSMB共有してみます。

Docker をインストールする

最初に公式ドキュメントに従ってDockerをインストールします。14.04なので前準備(Prerequisites)は不要だそうです。

$ curl -sSL https://get.docker.com/ | sh
apparmor is enabled in the kernel and apparmor utils were already installed
+ sudo -E sh -c apt-key adv --keyserver ...
(略)
+ sudo -E sh -c docker version
Client:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   76d6bc9
 Built:        Tue Nov  3 17:43:42 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   76d6bc9
 Built:        Tue Nov  3 17:43:42 UTC 2015
 OS/Arch:      linux/amd64

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker hoge

Remember that you will have to log out and back in for this to take effect!

てっきり1.8がインストールされると思っていたら1.9でした。
テスト実行してみます。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b901d36b6f2f: Pull complete
0a6ba66e537a: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.
...
(略)

インストールできているようです。簡単で助かる!
次に 自身のユーザアカウントを dockerグループに加えることで、sudoなしでdockerを実行できるようにします。

$ sudo usermod -aG docker hoge

hogeがユーザ名です。このとき -aG でなく -G としてしまうと大変なことになるそうなので注意します。

ログアウト、再ログイン後に 今度はsudoなしで動作確認します

$ docker run hello-world

Hello from Docker.
This message shows that your installation appears to be working correctly.
(略)

問題ないようです。

Dockerイメージを探す

Dockerイメージを自分で構成するのは sambaでない他のサービスを動かすときに試すこととして、今回は公式のDocker Hubで既存イメージを探して、それを利用してみます。

  1. 右上の検索ボックスでsambaを検索
    クリップボード.png

  2. Starsを選択:Starが多い順に検索結果を表示

  3. 上から順にいくつか見ていき、Full Description の記述がわかりやすかったdperson/samba に決定!

共有用のファイルシステムを作成する

前準備として、共有用のファイルシステムtank/pubを作成し、/mnt/pub にマウントします。また、dperson/samba から書き込み可能にするため /mnt/pubの所有者をdockerを実行するユーザに変えます。

$ sudo zfs create tank/pub
$ sudo zfs set atime=off tank/pub
$ sudo zfs set mountpoint=/mnt/pub tank/pub
$ sudo chown hoge /mnt/pub
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        94G  1.5G   92G   2% /
(略)
tank/home       1.3T  128K  1.3T   1% /home
tank/pub        1.3T  128K  1.3T   1% /mnt/pub
$ ll /mnt
total 16
drwxr-xr-x  3 root    root   16 Nov 12 01:14 ./
drwxr-xr-x 21 root    root 4096 Nov 13 19:33 ../
drwxr-xr-x  2 hoge    root    6 Nov 13 20:56 pub/

dperson/samba を起動する

いよいよDockerで dperson/samba を起動します。docker run のオプションはこちらを参考にさせて頂きました。
パラメータ中の dperson/samba より後のオプション -s と -u については dperson/samba のConfigurationに説明があります。普段ならsmb.confに記述する内容を、-s と -u で指定する感じです。

$ docker run --name samba -p 139:139 -p 445:445 -v /mnt/pub:/mnt/pub -d dperson/samba -s "pub;/mnt/pub;no;no;no;foo" -u "foo;bar"
Unable to find image 'dperson/samba:latest' locally
latest: Pulling from dperson/samba
a719479f5894: Pull complete
91bac885982d: Pull complete
69865d53ad81: Pull complete
52390241cd86: Pull complete
c7005920b097: Pull complete
26ee48a2628d: Pull complete
ffc195f7d642: Pull complete
bf2b48e20c44: Pull complete
Digest: sha256:19aeb4df3ee0a81095c5f2006b916af08abcc143540f9cd595f3839b3d77343a
Status: Downloaded newer image for dperson/samba:latest
0cf58b5f4d1ba117b94a0533c43cca55c9f40b90f8f5523633bb90d23b15a07c

dperson/sambaはローカルにないのでpull(ダウンロード)が始まります。10秒程度待ったら起動した(ように見える)のですが 本当にうまくいったのか…?

別の環境から共有してみます。192.168.0.10は、Docker/sambaを動かしている環境のIPアドレスです。

$ sudo mount -t cifs -o username=foo,password=bar,iocharset=utf8 //192.168.0.10/pub /media/nas

あっさりマウントできた!
今度はファイルを置いてみます。

$ echo aaa > /media/nas/test.txt
$ cat /media/nas/test.txt
aaa

置けました!pull含めて10秒程度で本当に立ち上がるとはびっくりです。

dperson/samba を停止・削除する

起動がうまくいったので コンテナを停止・削除してみます。

$ docker stop samba && docker rm samba
samba
samba

コンテナは消えましたが、先ほど置いたファイル(test.txt)は /mnt/pub/ に残っているはずなので、確認します。

$ ll /mnt/pub
(略)
-rw-rw-r-- 1 hoge     users  4 Nov 13 22:24 test.txt

夢じゃなかったようです。

続く


  1. 本家Solaris版ではZFS単体で共有ができるようです 

36
40
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
36
40