Making Video Calls Using Asterisk on Docker

02 May 2016

In this article I will explain how Asterisk can be setup inside a Docker container and also how to configure two softphones for it and interconnect them; finally you should be able to make video calls between them.

Downloading the Image and Initial Startup

Like I have explained in a previous article, you will first need to use the pull command to download the image; for our example we will use the Asterisk version 13 provided by cleardevice.

root@mirinda:~# docker pull cleardevice/docker-cert-asterisk13-ubuntu
Using default tag: latest
latest: Pulling from cleardevice/docker-cert-asterisk13-ubuntu
118aadd1f859: Already exists 
41402770caf2: Already exists 
a5051dd98acd: Already exists 
a3ed95caeb02: Already exists 
ea3d50686f68: Already exists 
8c169808b38c: Already exists 
dfc038b728ed: Already exists 
d17be1700849: Already exists 
01fb0b07054f: Already exists 
976cc1b6670a: Already exists 
Digest: sha256:cbf672d003a8a4dac8dbfec6c91c673bc8eed1f3691df129e2f0b779bbc6716f
Status: Image is up to date for cleardevice/docker-cert-asterisk13-ubuntu:latest

Next you will have to decide on a location for the asterisk configuration files, I decided to place them in /opt/asterisk but you can place them anywhere as long as you specify the exact path in the arguments of the Docker command.

Finally, we will start the Asterisk container:

root@mirinda:~# docker run --restart=always --privileged --name asterisk01 -d -p 5060:5060 -p 5060:5060/udp -p 8000-8100:8000-8100/udp -v /opt/asterisk/:/etc/asterisk cleardevice/docker-cert-asterisk13-ubuntu

Even if the command looks very complex, the arguments can be easily explained: with -v we are mounting our local directory /opt/asterisk in the container as /etc/asterisk; with -p we are binding the ports needed by Asterisk to communicate with the outside world; with –restart we are changing the restart policy, so that the container will always restart in case it encounters an error or just exits; finally with the –privileged switch we are giving the container some extra privileges that Asterisk needs in order to run.

Initializing Asterisk and Configuring SIP

There are so many configuration files but we only need to edit three of them: sip.conf, rtp.conf and extensions.conf; so normally we would have to install Asterisk on our local machine or get the files from somewhere. We can use a trick to generate standard the standard files: we will connect to the container and install Asterisk there, and then remove and recreate the Docker container. This is very helpful because once we remove the container, all changes on it will be lost, still we will have all of Asterisks default configuration files generated in /opt/asterisk as this directory was mounted.

root@mirinda:~# docker exec -ti asterisk01 bash
root@8c3fde4eadb7:~# apt-get update
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
Get:3 http://archive.ubuntu.com trusty Release.gpg [933 B]
Reading package lists... Done
root@8c3fde4eadb7:~# apt-get install asterisk -y
Reading package lists... Done
Building dependency tree 
Running hooks in /etc/ca-certificates/update.d....done.
root@8c3fde4eadb7:~# exit
root@mirinda:~# docker kill asterisk01; docker rm asterisk01
root@mirinda:~# docker run --restart=always --privileged --name asterisk01 -d -p 5060:5060 -p 5060:5060/udp -p 8000-8100:8000-8100/udp -v /opt/asterisk/:/etc/asterisk cleardevice/docker-cert-asterisk13-ubuntu

In order to setup the sip accounts we will need to edit the files mentioned above so we should first move the default files and create new ones.

root@docker:~# cd /root/asterisk
root@docker:~/asterisk# mv sip.conf sip.sample
root@docker:~/asterisk# mv extensions.conf extensions.sample
root@docker:~/asterisk# mv rtp.conf rtp.sample

Now using your favourite text editor create sip.conf, extensions.conf, rtp.conf and insert the content below:

videosupport=yes ;enable video
allow=h264 ;video codec, set the same on your softphone


exten = 6001,1,Dial(SIP/6001)
exten = 6002,1,Dial(SIP/6002)
same = n,Hangup()

Make sure you modify YOUR_EXTERNAL_IP with the external IP of your Docker container; this can be local or public. The last step is to connect to the Docker container, reload sip and dialplan and test if the configuration is correct:

root@mirinda:~/asterisk# docker exec -ti asterisk01 asterisk -rvvvvv
Privilege escalation protection disabled!
See https://wiki.asterisk.org/wiki/x/1gKfAQ for more details.
Asterisk 11.7.0~dfsg-1ubuntu1, Copyright (C) 1999 - 2013 Digium, Inc. and others.
Created by Mark Spencer <markster@digium.com>
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 11.7.0~dfsg-1ubuntu1 currently running on 8c3fde4eadb7 (pid = 31)
8c3fde4eadb7*CLI> sip reload
Reloading SIP
8c3fde4eadb7*CLI> dialplan reload
Dialplan reloaded.
8c3fde4eadb7*CLI> sip show peers
Name/username Host Dyn Forcerport ACL Port Status Description 
6001/6001 D N 5060 OK (2 ms) 
6002/6002 D N 5060 OK (2 ms) 
2 sip peers [Monitored: 2 online, 0 offline Unmonitored: 0 online, 0 offline]



by George Lucian Tabacar

Want to learn more?