はじめに
Ubuntu側でファイル共有サーバを作りたいな。。。
そうだ、Samba しよう
環境汚したくないし、Dockerでやろ
前提条件
- 環境
$ uname -a
Linux smb_host 5.15.0-75-generic #82-Ubuntu SMP Tue Jun 6 23:10:23 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.2 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
-
共有するフォルダ
/export/smb_share
-
UbuntuのサーバのIPアドレスは、
192.168.100.100
-
Dockerはインストール済み
$ docker -v
Docker version 24.0.2, build cb74dfc
- Docker未インストールなら、【2023年06月版】Ubuntu22.04 に docker / docker compose をインストール を参照
設定
docker-compose.yaml の入手
$ curl -O https://raw.githubusercontent.com/ServerContainers/samba/master/docker-compose.yml
docker-compose.yaml の編集
- ユーザに関する記述を削除、共有の記述を削除して、今回共有する
shared
について記述
$ cp docker-compose.yaml docker-compose.yaml.back
$ vim docker-compose.yaml
$ diff docker-compose.yaml.back docker-compose.yaml
diff --git a/docker-compose.yml.back b/docker-compose.yml
index 0688958..1a59100 100644
--- a/docker-compose.yml.back
+++ b/docker-compose.yml
@@ -23,40 +23,42 @@ services:
#WSDD2_DISABLE: 1
#AVAHI_DISABLE: 1
- GROUP_family: 1500
-
- ACCOUNT_alice: alipass
- UID_alice: 1000
- GROUPS_alice: family
-
- ACCOUNT_bob: bobpass
- UID_bob: 1001
- GROUPS_bob: family
-
- # example for hashed password (user: foo | password: bar) - generated using create-hash.sh script.
- ACCOUNT_foo: "foo:1000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:86C156FC198B358CCCF6278D8BD49B6A:[U ]:LCT-61B0859A:"
- UID_foo: 1002
- GROUPS_foo: family
-
- SAMBA_VOLUME_CONFIG_shared_home: "[Home]; path=/shares/homes/%U; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes"
-
- SAMBA_VOLUME_CONFIG_aliceonly: "[Alice Share]; path=/shares/alice; valid users = alice; guest ok = no; read only = no; browseable = yes"
- SAMBA_VOLUME_CONFIG_alicehidden: "[Alice Hidden Share]; path=/shares/alice-hidden; valid users = alice; guest ok = no; read only = no; browseable = no"
-
- SAMBA_VOLUME_CONFIG_bobonly: "[Bob Share]; path=/shares/bob; valid users = bob; guest ok = no; read only = no; browseable = yes"
-
- SAMBA_VOLUME_CONFIG_public: "[Public]; path=/shares/public; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes; force group = family"
- SAMBA_VOLUME_CONFIG_public_ro: "[Public ReadOnly]; path=/shares/public; guest ok = yes; read only = yes; browseable = yes; force group = family"
-
- SAMBA_VOLUME_CONFIG_timemachine: "[TimeMachine]; path=/shares/timemachine/%U; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes; fruit:time machine = yes; fruit:time machine max size = 500G"
+# GROUP_family: 1500
+#
+# ACCOUNT_alice: alipass
+# UID_alice: 1000
+# GROUPS_alice: family
+#
+# ACCOUNT_bob: bobpass
+# UID_bob: 1001
+# GROUPS_bob: family
+#
+# # example for hashed password (user: foo | password: bar) - generated using create-hash.sh script.
+# ACCOUNT_foo: "foo:1000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:86C156FC198B358CCCF6278D8BD49B6A:[U ]:LCT-61B0859A:"
+# UID_foo: 1002
+# GROUPS_foo: family
+#
+# SAMBA_VOLUME_CONFIG_shared_home: "[Home]; path=/shares/homes/%U; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes"
+#
+# SAMBA_VOLUME_CONFIG_aliceonly: "[Alice Share]; path=/shares/alice; valid users = alice; guest ok = no; read only = no; browseable = yes"
+# SAMBA_VOLUME_CONFIG_alicehidden: "[Alice Hidden Share]; path=/shares/alice-hidden; valid users = alice; guest ok = no; read only = no; browseable = no"
+#
+# SAMBA_VOLUME_CONFIG_bobonly: "[Bob Share]; path=/shares/bob; valid users = bob; guest ok = no; read only = no; browseable = yes"
+#
+# SAMBA_VOLUME_CONFIG_public: "[Public]; path=/shares/public; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes; force group = family"
+# SAMBA_VOLUME_CONFIG_public_ro: "[Public ReadOnly]; path=/shares/public; guest ok = yes; read only = yes; browseable = yes; force group = family"
+#
+# SAMBA_VOLUME_CONFIG_timemachine: "[TimeMachine]; path=/shares/timemachine/%U; valid users = alice, bob, foo; guest ok = no; read only = no; browseable = yes; fruit:time machine = yes; fruit:time machine max size = 500G"
+ SAMBA_VOLUME_CONFIG_share: "[shared]; comment = ubuntu22.04 share; path=/shared; available = yes; read only = no; browsable = yes; public = yes; writeable = yes; guest ok = yes"
volumes:
- /etc/avahi/services/:/external/avahi
# avoid loops when mounting folders to /shares (I'd recommend explicit mapping for each share)
- - ./shares/alice:/shares/alice
- - ./shares/alice-hidden:/shares/alice-hidden
- - ./shares/bob:/shares/bob
- - ./shares/public:/shares/public
- - ./shares/homes:/shares/homes
- - ./shares/timemachine:/shares/timemachine
+# - ./shares/alice:/shares/alice
+# - ./shares/alice-hidden:/shares/alice-hidden
+# - ./shares/bob:/shares/bob
+# - ./shares/public:/shares/public
+# - ./shares/homes:/shares/homes
+# - ./shares/timemachine:/shares/timemachine
+ - /export/smb_share:/shared
起動
$ docker compose up -d
[+] Building 0.0s (0/0)
[+] Running 1/1
✔ Container docker_smb-samba-1 Started
- ログを確認
$ docker compose logs -f
動作確認
Windows端末から、//192.168.100.100/shared
で共有を開く
サービス化
稼働中の docker compose を停止
$ docker compose down
docker-compose.yaml の配置
-
/usr/local/bin/docker_smb
に配置する -
docker_smb
は適当に替えても良い、サービス名になる
$ sudo mkdir /usr/local/bin/docker_smb
$ sudo cp docker-compose.yaml /usr/local/bin/docker_smb/
systemd のユニットファイルを作成
- ユニットファイル
/etc/systemd/system/docker-compose-service@.service
を作成する
$ cat /etc/systemd/system/docker-compose-service@.service
[Unit]
Description=%i managed by docker-compose
Requires=docker.service
[Service]
Type=simple
Environment=COMPOSE_FILE=/usr/local/bin/%i/docker-compose.yaml
ExecStartPre=-/usr/bin/docker compose -f ${COMPOSE_FILE} down --volumes
ExecStart=/usr/bin/docker compose -f ${COMPOSE_FILE} up
ExecStop=/usr/bin/docker compose -f ${COMPOSE_FILE} down --volumes
[Install]
WantedBy=multi-user.target
ユニットファイルの読み込み
- ユニットファイルを編集したときも再読み込みが必要
$ sudo systemctl daemon-reload
systemd のサービス登録と起動
$ sudo systemctl enable --now docker-compose-service@docker_smb
systemd でサービス化した samba の動作確認
$ sudo systemctl status docker-compose-service@docker_smb
または
$ docker-compose logs -f
さいごに
かんたんでしたね