【LIVA Z】Dockerを入れてDockerの上でAsteriskを動かす【Ubuntu18.04】

UbuntuサーバにDocker入れます。で、Docker上でAsteriskを使いたい。

※2019/6/23追記
本記事に沿ってインストールした後、SIP.conf等設定ファイルの中身を変更しても、SIPフォンからRegistration(Asteriskとの接続)が出来ず。
仕方ないので、以下のサイトを参考にdockerイメージでAsterisk13を導入しました。
github.com

該当のイメージはすでにAsterisk13がインストール済みなので、
asterisk -r
asterisk CLIに入ることができます。

あと、本記事の手順だとAsterisk15をインストールしてますが、どうやらLTSじゃなかったらしいので、
Asterisk13か16を入れたほうがいいようです。15は2019年10月にサポートが切れます。
Asterisk Versions - Asterisk Project - Asterisk Project Wiki
追記終わり

1.Dockerの導入

どうやらapt installで入れるバージョンは古いとのこと。実のところ、公式のドキュメントにUbuntu18.04の導入方法が記載されていたので、そのとおりに実行するだけ。

docs.docker.com

必要なパッケージを入れる

$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

GPG 公開鍵を入れる。あとでaptで入れるときに必要になります。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

追加ができれば、「OK」が表示されます。

以下のコマンドでフィンガープリントが確認できるらしい。

$ sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ 不明 ] Docker Release (CE deb)
sub rsa4096 2017-02-22 [S]

リポジトリーの追加。LIVA ZはCeleronなので、「x86_64 / amd64」のタブで記載されている内容を実行します。

$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

※追記 Linux mint 19の場合は以下のコマンドらしい

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

qiita.com


インストール準備ができたので、Dockerをインストールします。

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

インストールに成功すると、バージョン情報表示でバージョンが見れます。

$ sudo docker version
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 02:35:57 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 01:59:36 2019
OS/Arch: linux/amd64
Experimental: false

2.DockerにUbuntu18.04イメージをダウンロード

Ubuntu18.04イメージは用意されているので、コマンドで簡単に入れられます。

$ sudo docker pull ubuntu:18.04
6abc03819f3e: Pull complete
05731e63f211: Pull complete
0bd67c50d6be: Pull complete
Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
Status: Downloaded newer image for ubuntu:18.04

$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 7698f282e524 2 weeks ago 69.9MB


落としたイメージを基にコンテナに名前を付けて起動。名前は「ubuntu18.04-Asterisk」にしました。
※この段階でpオプションでポートをしておく。後から追加もできるらしいけど、iptablesを使うので面倒。

$ sudo docker run -it -d -p 5060:5060 --name ubuntu18.04-Asterisk ubuntu:18.04
ubuntu:18.04
df8293948e68cc896dda26b36d5dd1934cc034a69e6336a8cedb2e3894b19d3f

psオプションで動いているかを確認できます。

$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS   NAMES
df8293948e68 ubuntu:18.04 "/bin/bash" 13 minutes ago Up 13 minutes 0.0.0.0:5060->5060/tcp ubuntu18.04-Asterisk

コンテナに入るのには以下のコマンドを実行します。

$ sudo docker exec -it ubuntu18.04-Asterisk /bin/bash
root@df8293948e68:/#

root@df8293948e68:/# ls -la
total 72
drwxr-xr-x 1 root root 4096 Jun 1 16:15 .
drwxr-xr-x 1 root root 4096 Jun 1 16:15 ..

  • rwxr-xr-x 1 root root 0 Jun 1 16:15 .dockerenv

...

コンテナから出るにはexitで可能。

root@df8293948e68:/# exit
exit

コンテナを停止するには以下のコマンドを実行

$ sudo docker stop ubuntu18.04-Asterisk
ubuntu18.04-Asterisk

$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

停止したコンテナを再度起動

$ sudo docker start ubuntu18.04-Asterisk
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df8293948e68 ubuntu:18.04 "/bin/bash" 21 minutes ago Up 6 seconds 0.0.0.0:5060->5060/tcp ubuntu18.04-Asterisk

なお、コンテナの削除する場合は以下のコマンドで。

$ docker rm ubuntu18.04-Asterisk

コンテナの状況とコマンドの関係性は以下のサイトでチャーハンの例えが分かりやすかったです。
qiita.com


あと、コンテナはaptでアップグレードしておいたほうがいい。

root@df8293948e68:/# apt update
root@df8293948e68:/# apt upgrade

3.コンテナにAsteriskを導入する

以下のサイトを参考にします。
https://linuxize.com/post/how-to-install-asterisk-on-ubuntu-18-04/


昔記事は書いたけど、流石にバージョンが違いすぎるので・・・
engetu21.hatenablog.com
というか、「Ubuntu Asterisk」でググると最初に出てくるのが自分のブログのような・・・?
とりあえず、現時点のインストールバージョンは「Asterisk 15.7.2」となります。


とりあえず、wgetコマンドが入っていないので、まずはwgetをインストールして、ソースコードをダウンロード。

# apt install wget
# cd /usr/src/
# wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-15-current.tar.gz

落としてきたファイルを解凍

# tar zxvf asterisk-15-current.tar.gz

Asteriskを動かす上で、mp3モジュールの構築が必要らしいので、解凍したディレクトリからシェルスクリプトを実行します(ソースをダウンロードしてくれるらしい)。
ただ、シェルスクリプト内でSVNを使ってるらしいので、SVNのパッケージも入れます。

# apt install subversion
# ./asterisk-15.7.2/contrib/scripts/get_mp3_source.sh
A addons/mp3
A addons/mp3/MPGLIB_README
A addons/mp3/common.c
A addons/mp3/huffman.h
A addons/mp3/tabinit.c
A addons/mp3/Makefile
A addons/mp3/README
A addons/mp3/decode_i386.c
A addons/mp3/dct64_i386.c
A addons/mp3/MPGLIB_TODO
A addons/mp3/mpg123.h
A addons/mp3/layer3.c
A addons/mp3/mpglib.h
A addons/mp3/decode_ntom.c
A addons/mp3/interface.c
Exported revision 202.


その他Asterisk依存性解消のため、以下のコマンドを実行。

# ./asterisk-15.7.2/contrib/scripts/install_prereq install

(略)

※途中で項目入力がある(地域とタイムゾーンの設定)
Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing the
time zones in which they are located.

1. Africa 4. Australia 7. Atlantic 10. Pacific 13. Etc
2. America 5. Arctic 8. Europe 11. SystemV
3. Antarctica 6. Asia 9. Indian 12. US

Geographic area: 6


Please select the city or region corresponding to your time zone.

1. Aden 24. Dubai 47. Kuching 70. Shanghai
2. Almaty 25. Dushanbe 48. Kuwait 71. Singapore
3. Amman 26. Famagusta 49. Macau 72. Srednekolymsk
4. Anadyr 27. Gaza 50. Magadan 73. Taipei
5. Aqtau 28. Harbin 51. Makassar 74. Tashkent
6. Aqtobe 29. Hebron 52. Manila 75. Tbilisi
7. Ashgabat 30. Ho_Chi_Minh 53. Muscat 76. Tehran
8. Atyrau 31. Hong_Kong 54. Nicosia 77. Tel_Aviv
9. Baghdad 32. Hovd 55. Novokuznetsk 78. Thimphu
10. Bahrain 33. Irkutsk 56. Novosibirsk 79. Tokyo
11. Baku 34. Istanbul 57. Omsk 80. Tomsk
12. Bangkok 35. Jakarta 58. Oral 81. Ujung_Pandang
13. Barnaul 36. Jayapura 59. Phnom_Penh 82. Ulaanbaatar
14. Beirut 37. Jerusalem 60. Pontianak 83. Urumqi
15. Bishkek 38. Kabul 61. Pyongyang 84. Ust-Nera
16. Brunei 39. Kamchatka 62. Qatar 85. Vientiane
17. Chita 40. Karachi 63. Qostanay 86. Vladivostok
18. Choibalsan 41. Kashgar 64. Qyzylorda 87. Yakutsk
19. Chongqing 42. Kathmandu 65. Rangoon 88. Yangon
20. Colombo 43. Khandyga 66. Riyadh 89. Yekaterinburg
21. Damascus 44. Kolkata 67. Sakhalin 90. Yerevan
22. Dhaka 45. Krasnoyarsk 68. Samarkand
23. Dili 46. Kuala_Lumpur 69. Seoul

Time zone: 79

(略)

※国番号の入力。日本は81です。
This is the numeric code for the region your phone system will be operating in
(eg. 61 for Australia or 33 for France). It is used to configure the default
regional standards that Voicetronix telephony hardware should comply with.

ITU-T telephone code: 81

(略)

#############################################
## install completed successfully
#############################################


Asteriskコンパイルします。開発ツールが必要になるので、build-essentialをインストールしてからコンパイルを実行します。

# apt install build-essential
# cd asterisk-15.7.2/

# ./configure


※完了すると以下のように表示される。

configure: Menuselect build configuration successfully completed

               .$$$$$$$$$$$$$$$=..      
            .$7$7..          .7$$7:.    
          .$$:.                 ,$7.7   
        .$7.     7$$$$           .$$77  
     ..$$.       $$$$$            .$$$7 
    ..7$   .?.   $$$$$   .?.       7$$$.
   $.$.   .$$$7. $$$$7 .7$$$.      .$$$.
 .777.   .$$$$$$77$$$77$$$$$7.      $$$,
 $$$~      .7$$$$$$$$$$$$$7.       .$$$.
.$$7          .7$$$$$$$7:          ?$$$.
$$$          ?7$$$$$$$$$$I        .$$$7 
$$$       .7$$$$$$$$$$$$$$$$      :$$$. 
$$$       $$$$$$7$$$$$$$$$$$$    .$$$.  
$$$        $$$   7$$$7  .$$$    .$$$.   
$$$$             $$$$7         .$$$.    
7$$$7            7$$$$        7$$$      
 $$$$$                        $$$       
  $$$$7.                       $$  (TM)     
   $$$$$$$.           .7$$$$$$  $$      
     $$$$$$$$$$$$7$$$$$$$$$.$$$$$$      
       $$$$$$$$$$$$$$$$.                

configure: Package configured for: 
configure: OS type  : linux-gnu
configure: Host CPU : x86_64
configure: build-cpu:vendor:os: x86_64 : pc : linux-gnu :
configure: host-cpu:vendor:os: x86_64 : pc : linux-gnu :

makeのメニュー設定をします。

# make menuselect

途中でアドオンについて聞かれる。ここではmp3モジュールを構築するように指定。
[Add-ons]で→を押す。右側のリストに映るので、[format_mp3]のところでEnterを押すことで、*(選択済み)になります。F12を押して保存して終了。
f:id:engetu21:20190602030256p:plain

menuselect changes saved!
コマンドライン上に表示されます。


makeを実行します。

# make

(略)

Building Documentation For: third-party channels pbx apps codecs formats cdr cel bridges funcs tests main res addons
+--------- Asterisk Build Complete ---------+
+ Asterisk has successfully been built, and +
+ can be installed by running: +
+ +
+ make install +
+-------------------------------------------+

インストール実行。

# make install

(略)

done
+---- Asterisk Installation Complete -------+
+ +
+ YOU MUST READ THE SECURITY DOCUMENT +
+ +
+ Asterisk has successfully been installed. +
+ If you would like to install the sample +
+ configuration files (overwriting any +
+ existing config files), run: +
+ +
+ For generic reference documentation: +
+ make samples +
+ +
+ For a sample basic PBX: +
+ make basic-pbx +
+ +
+ +
+----------------- or ---------------------+
+ +
+ You can go ahead and install the asterisk +
+ program documentation now or later run: +
+ +
+ make progdocs +
+ +
+ **Note** This requires that you have +
+ doxygen installed on your local system +
+-------------------------------------------+



※ここでエラーが発生することがある。その場合はcontrib/scripts/get_mp3_source.shをもう一度実行すると解消される。
Installing modules from addons...
/usr/bin/install: cannot stat 'format_mp3.so': No such file or directory
/usr/src/asterisk-15.7.2/Makefile.moddir_rules:112: recipe for target 'install' failed
make[1]: *** [install] Error 1
Makefile:613: recipe for target 'addons-install' failed
make: *** [addons-install] Error 2


無事完了。次はサンプル構成ファイルを構築するようです。

# make samples

続いて、PBX設定ファイルを構築。

# make basic-pbx

最後にAsterisk initスクリプト構築と共有ライブラリのキャッシュを更新をします。

# make config
# ldconfig

4.Asteriskのユーザを作成とセッティング

デフォルトではAsteriskのユーザはrootになっているようなので、asteriskという名前で新規作成。

# adduser --system --group --home /var/lib/asterisk --no-create-home --gecos "Asterisk PBX" asterisk

新しく作ったユーザを有効にするため、Asteriskの設定を変更。
ついでにviコマンドが使えるようにvim入れます(コンテナに入ってなかった)

# apt install vim
# vi /etc/default/asterisk

※最後に以下を追加
AST_USER="asterisk"
AST_GROUP="asterisk"

asteriskユーザーをdialoutand、audioグループに追加します。

# usermod -a -G dialout,audio asterisk

いくつかのディレクトリにアクセスできるよう、所属とパーミッションを変更

# chown -R asterisk: /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk
# chmod -R 750 /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk

5.Asterisk実行

Asteriskを実行します。参考元はsystemctlを使ってますが、serviceコマンドで実行します。

# service asterisk status
* asterisk is not running

# service asterisk start
* Starting Asterisk PBX: asterisk [ OK ]

# service asterisk status
* asterisk is running

動いていれば、Asterisk CLIプロンプトが表示できる。

# asterisk -vvvr
Asterisk 15.7.2, Copyright (C) 1999 - 2016, Digium, Inc. and others.
Created by Mark Spencer
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 15.7.2 currently running on df8293948e68 (pid = 3040)

※終了はexitを入力