はじめに
背景として、サーバPCを入手したため、サーバ化したくなった。ただ、WordPressとか作成して遊びたいので、Dockerで作りたい。(OSはクリーンのままにしたい。)
余談
本当はWindowsPC上にDocker for Windows経由でsambaサーバを作成したかった。が…どうやらポート445がWindowsのSMBポートに割り当てられており、競合してしまうため使用できかったorz
下記URLを参照。
https://aquasoftware.net/blog/?p=1259
(ちなみに、URLのServerサービスを停止すると、Docker for Windowsも止まるっぽい。。)
純正Linux最高ということで、128GBの少ない容量にUbuntu20.04をぶち込んだ。
ということで、方針を変えて、「せめてDocker学習のために、UNIXベースのOSをコンテナ化して、Dockerfileを記載し、0からsambaサーバをくみ上げてみよう」という方針を掲げたというオチになったとさ。
環境
- Ubuntu20.04にDockerをinstallして使用
調理材料
まず全体のファイル構成から。
.
├── build
│ ├── Dockerfile
│ ├── smb.conf
│ ├── start.sh
│ ├── supervisord.conf
│ ├── user.list
│ └── user_add.sh
└── docker-compose.yml
役割を順に説明すると、
- Dockerfileとdocker-compose.yml…言わずもがなDockerのビルド素材と周辺設定のファイル。
- smb.conf…sambaのコンフィグ
- start.sh…CMDまたはENTRYPOINTで指定するコマンド(と思ったが使用していないので本稿では記載しない。)
- user.list…sambaにアクセスするユーザリスト
- user_add.sh…user.listを吸い上げて、linuxのユーザ追加とsambaのユーザ追加をするシェル。
- supervisord.conf…supervisorのコンフィグ
ここからは、各ファイルのソースを張り付ける。
version : '3'
services:
samba:
build:
context: .
dockerfile: ./build/Dockerfile
image: alpine_samba
container_name: samba
ports:
- "139:139"
- "445:445"
volumes:
- "<マウントするドライブ>:/media/share/"
restart: always
tty: true
FROM alpine:latest
# ルートユーザ指定
USER root
# ポート解放
EXPOSE 139
EXPOSE 445
# パッケージ管理アップデート
RUN apk update
# samba、システム管理ソフト(openrc)のinstall
RUN apk add --no-cache samba
RUN apk add --no-cache supervisor
# RUN apk add --no-cache openrc
# openrcの初期設定
# RUN sed -i 's/#rc_sys=""/rc_sys="lxc"/g' /etc/rc.conf
# RUN echo 'rc_provide="loopback net"' >> /etc/rc.conf
# RUN sed -i 's/^#\(rc_logger="YES"\)$/\1/' /etc/rc.conf
# RUN sed -i '/tty/d' /etc/inittab
# RUN sed -i 's/hostname $opts/# hostname $opts/g' /etc/init.d/hostname
# RUN sed -i 's/mount -t tmpfs/# mount -t tmpfs/g' /lib/rc/sh/init.sh
# RUN sed -i 's/cgroup_add_service /# cgroup_add_service /g' /lib/rc/sh/openrc-run.sh
# RUN mkdir /run/openrc
# RUN touch /run/openrc/softlevel
# RUN rc-status
# sambaを起動時に自動起動するようにする
# RUN rc-update add samba
# RUN rc-service samba start
# supervisor初期設定
COPY ./build/supervisord.conf /etc/supervisord.conf
# sambaコンフィグコピー
COPY ./build/smb.conf /etc/samba/smb.conf
# ユーザ登録用シェル + ユーザリストコピー
RUN mkdir /org
COPY ./build/user.list /org/user.list
COPY ./build/user_add.sh /org/user_add.sh
# シェルの権限変更
RUN chmod 777 /org/user_add.sh
# リスト登録シェル実行
WORKDIR /org
RUN ./user_add.sh
# ワーキングディレクトリをrootへ
WORKDIR /
# シェル起動
# CMD ["/bin/sh" , "-c" , "while :; do sleep 10; done"]
ENTRYPOINT ["/usr/bin/supervisord" , "-c" , "/etc/supervisord.conf"]
[unix_http_server]
file=/dev/shm/supervisor.sock
[supervisord]
logfile=/tmp/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/tmp/supervisord.pid
nodaemon=true
minfds=1024
minprocs=200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///dev/shm/supervisor.sock
[program:smbd]
command=smbd --foreground --no-process-group --log-stdout --configfile /etc/samba/smb.conf
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nmbd]
command=nmbd --foreground --no-process-group --log-stdout --configfile /etc/samba/smb.conf
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
#======================= Global Settings =====================================
[global]
# workgroup(Setting group name)
workgroup = WORKGROUP
# servername(Free typing)
server string = Samba Server
# server role("AUTO" is stable)
server role = AUTO
# Allow IP address
; hosts allow = 192.168.1. 192.168.2. 127.
# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
; guest account = pcguest
# Save log file
log file = /log/samba_log.%m
# Put a capping on the size of the log files (in Kb).
max log size = 50
# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# via DNS nslookups. The default is NO.
dns proxy = no
# security(default:user)
security = user
#============================ Share Definitions ==============================
[homes]
comment = Home Directories
browseable = no
writable = yes
# NOTE: If you have a BSD-style print system there is no need to
# specifically define each individual printer
[printers]
comment = All Printers
path = /usr/spool/samba
browseable = no
# Set public = yes to allow user 'guest account' to print
guest ok = no
writable = no
printable = yes
# A publicly accessible directory, but read only, except for people in
# the "staff" group
[share]
comment = share directory
path = /media/share
writable = no
read only = no
<ID> <PASS>
#!/bin/sh
while read line
do
#echo $line
#リストを1行ずつ読み込んでIDとパスワードを変数に格納
usrname=`echo $line | awk '{print $1}'`
passwd=`echo $line | awk '{print $2}'`
#既存のユーザでないか確認するための1行
pdbedit -L -w | egrep "^""$usrname"":" >> /dev/null
#既存ユーザの場合は登録処理をスキップ、未登録の場合はワンライナーで登録
if [ $? == 0 ] ; then
echo "$usrname is already registered."
else
echo "Set $usrname registration and password"
adduser --disabled-password --gecos "" "$usrname"
echo "${usrname}:${passwd}" | chpasswd
echo -e "$passwd\n$passwd" | pdbedit -a -t -u $usrname
fi
done < ./user.list
# 変数初期化
usrname=0
passwd=0
# リスト消去
rm ./user.list
echo "Delete the contents of the file!!!"
※https://qiita.com/goriggg/items/513721a53de6e331eda9 を参考にして利用しています。
一応全行程終わったらlistは削除しています。セキュリティ的にも残したくない。
これらのファイル構成を行ったら、
docker-compose build
docker-compose -d up
で、立ち上がっちゃう。素敵。
詳細説明
これは後程。
終わりに
- supervisorはかゆいところに手が届く。
- WinのDockerは使用しないと決めました。
Next...
VPNサーバを立ててみよう
参考資料
https://blog.adachin.me/archives/4177
https://aquasoftware.net/blog/?p=1259
https://qiita.com/nukokoi/items/4f60124e30080d91d846