Sunday, November 17, 2024
Google search engine
HomeGuest BlogsHow To Run Asterisk in Docker Container

How To Run Asterisk in Docker Container

Asterisk is a free and open-source tool to build/develop communications applications. It was developed by Mark Spencer of Digium, in 1999. Since 2018, Asterisk has been a division of Sangoma Technologies Corporation. It was initially created for Linux systems but currently runs on a variety of devices such as NetBSD, OpenBSD, FreeBSD, macOS, and Solaris, and it can also be embedded systems based on OpenWrt.

The Asterisk software includes many features and benefits available for enterprise and proprietary PBX systems. It provides voice mail, conference calling, interactive voice response (phone menus), and automatic call distribution.

Since Asterisk is open source, users are able to access the code and create new functionality by writing dial plan scripts in several of Asterisk’s own extensions languages, adding custom modules in C and PHP or implementing the Asterisk Gateway Interface (AGI) programs using any programming language with the ability to communicate using the stdin and stdout streams.

In this guide, we will run Asterisk in a Docker Container. This comes with a lot of features and benefits such as:

  • PrivateDial, customizable Asterisk configuration
  • AutoBan, a built-in intrusion detection and prevention system
  • Asterisk powering IP PBX systems and VoIP gateways
  • Small image size based on Alpine Linux
  • Additionally, provide the G.729 and G.723.1 audio codecs
  • Automatic integration of Let’s Encrypt LTS certificates using the reverse proxy Traefik
  • Container audio using the pulse socket of the host
  • Log directed to docker daemon with a configurable level
  • Small image size based on Alpine Linux
  • WebSMS, send and receive messages, SMS, over HTTP
  • Persistent storage facilitated by configuration and run data being consolidated under /srv

Setup Pre-requisites

In this guide, there are a number of applications required. Install them with the commands:

## On RHEL/CentOS/RockyLinux 8
sudo yum update
sudo yum install curl git vim make

## On Debian/Ubuntu
sudo apt update && sudo apt upgrade -y
sudo apt install curl git vim make

## On Fedora
sudo dnf update
sudo dnf -y install curl git vim make

You also need to have Docker installed on your system. To do that, you can use the aid provided in the guide below:

Commands are shared to install Docker Compose:

curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
chmod +x docker-compose-linux-x86_64
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

Confirm installation:

$ docker-compose version
Docker Compose version v2.14.2

Add your user to the Docker group:

sudo usermod -aG docker $USER
newgrp docker

Start and enable the service:

sudo systemctl start docker && sudo systemctl enable docker

You also need to install Docker-compose:

#1. Configure the Asterisk Container

Before we run the container, we need to download the required files and make desired adjustments. We will use git to clone the repository that contains these files:

git clone https://github.com/mlan/docker-asterisk.git

Navigate into the directory:

cd docker-asterisk

Now we have all the required files for this deployment. For this guide, we will use the sample configs in the demo directory.

mv demo deploy
cd deploy

Here, we have a docker-compose.yml that can be used to spin a sample VoIP SIP server. In the file, there are various configs and environment variables. You can open this file for editing:

vim docker-compose.yml

In the file, you can have the following environmental variables:

Variable Default value Description
SYSLOG_LEVEL 4 Logging level, from 0 to 8. 0 is the least, whereas, 8 is the most log output.
WEBSMSD_PORT 80 This is a PHP web server port, used by WebSMS. Undefined or non-numeric, will disable the PHP web server.
ACME_FILE /acme/acme.json The file that contains TLS certificates, is provided by Let’s encrypt using Traefik.
HOSTNAME $(hostname) It is used to identify the relevant TLS certificates in ACME_FILE.
TLS_CERTDAYS 30 Self-signed TLS certificate validity duration in days.
TLS_KEYBITS 2048 Self-signed TLS key length in bits.
SYSLOG_OPTIONS -SDt S: smaller output, D: drop duplicates, t: Strip client-generated timestamps.

#2. Create Persistent Storage for Asterisk

Persistent storage is used to store data for the container. For this guide, we will create storage with the name asterisk-conf to persist data under the /srv directory.

We will create the directory with the command:

sudo mkdir -p /data/asterisk-conf

Assign the correct permissions:

sudo chmod 775 -R /data/asterisk-conf
sudo chown -R $USER:docker /data/asterisk-conf

For Rhel-based systems, configure SELinux:

sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config

Now e will create the docker volume using the above path:

docker volume create --driver local \
     --opt type=none \
     --opt device=/data/asterisk-conf \
     --opt o=bind asterisk-conf 

Once created, you can check it with the command:

$ docker volume list
DRIVER    VOLUME NAME
local     asterisk-conf

Now we will modify the docker-compose YAML and add external: true

$ vim docker-compose.yml
volumes:
  asterisk-conf:                                # Persistent storage
    external: true                                                       

#3. Run the Asterisk Container

Now you are set to start the container using the docker-compose YAML file. In the demo directory, we also have a Makefile which is so important in this deployment.

Install build tools:

sudo apt install build-essential

This makes it so easy to manage the container and execute other commands. To start the container, we will use the command:

make up

Sample Output:

docker-compose up -d
[+] Running 13/13
 ⠿ tele Pulled                                                                          13.2s
   ⠿ c158987b0551 Pull complete                                                          1.5s
   ⠿ 390edeb6309b Pull complete                                                          2.1s
   ⠿ f9c34e7c3c9b Pull complete                                                          3.8s
   ⠿ 84dbb9a70c8b Pull complete                                                          4.4s
   ⠿ d507ed88ccf8 Pull complete                                                          4.7s
   ⠿ 947c68adf1a6 Pull complete                                                          5.0s
   ⠿ ebad4628becc Pull complete                                                          5.3s
   ⠿ d2548a783261 Pull complete                                                          5.6s
   ⠿ b7d2cd86d6d3 Pull complete                                                          7.9s
   ⠿ 74e1e4e2b334 Pull complete                                                          8.9s
   ⠿ 608c86894759 Pull complete                                                          9.3s
   ⠿ aeb618e023a5 Pull complete                                                         11.3s
[+] Running 1/1
 ⠿ Container demo-tele-1  Started                                                        8.8s

To verify if the container is running, use the command:

$ make ps
docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED              STATUS                        PORTS
demo-tele-1         mlan/asterisk       "docker-entrypoint.s…"   tele                About a minute ago   Up About a minute (healthy)   0.0.0.0:5060->5060/udp, :::5060->5060/udp, 0.0.0.0:5060-5061->5060-5061/tcp, 0.0.0.0:10000-10099->10000-10099/udp, :::5060-5061->5060-5061/tcp, :::10000-10099->10000-10099/udp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp

You can also use the normal command:

docker ps

You now have all the Asterisk configuration files stored in your path:

$ ls /data/asterisk-conf/etc/
asterisk  nftables.d  ssl

We can now allow these ports through the firewall:

##For UFW
sudo ufw allow proto tcp from any to any port 5060,5061

##For Firewalld
sudo firewall-cmd --add-port=5060/tcp --permanent
sudo firewall-cmd --add-port=5061/tcp --permanent
sudo firewall-cmd --reload

Now we are set to use Asterisk as desired. To connect to the asterisk command line interface (CLI) running in the container, we will execute:

make cli
##OR
docker-compose exec tele asterisk -rvvvddd

Sample Output:

docker-compose exec tele asterisk -rvvvddd
Seeding global EID '02:42:ac:11:00:02' from 'eth0' using 'siocgifhwaddr'
Parsing /etc/asterisk/asterisk.conf
Asterisk 18.15.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <[email protected]>
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 18.15.0 currently running on ec74230892b0 (pid = 252)
Parsing /etc/asterisk/logger.conf
Core debug was OFF and is now 3.
ec74230892b0*CLI> 

Now we can run the normal Asterisk commands. For example:

ec74230892b0*CLI> pjsip show auths    
    
  I/OAuth:  <AuthId/UserName.............................................................>
==========================================================================================

     Auth:  itsp:mydoe-oauth/username
     Auth:  jane.doe-iauth/jane.doe
     Auth:  john.doe-iauth/john.doe

Objects found: 3

ec74230892b0*CLI> 

There are several other commands, to view them, use the command:

ec74230892b0*CLI> help
!                              -- Execute a shell command
acl show                       -- Show a named ACL or list all named ACLs
aeap show client               -- Show AEAP client configuration by id
aeap show clients              -- Show all AEAP client configurations
agi dump html                  -- Dumps a list of AGI commands in HTML format
agi exec                       -- Add AGI command to a channel in Async AGI
agi set debug [on|off]         -- Enable/Disable AGI debugging
agi show commands [topic]      -- List AGI commands or specific help
aoc set debug                  -- enable cli debugging of AOC messages
bridge kick                    -- Kick a channel from a bridge
......

To exit type:

c99cd82ef92f*CLI> exit
Asterisk cleanly ending (0).
Executing last minute cleanups
Asterisk ending (0).

#4. Manage the Asterisk Container

We can manage the container using a bunch of commands here.

To start the container, use:

make start
##OR
docker-compose start

To stop the container:

make stop
##OR
docker-compose stop

To see the configuration:

make config
##OR
docker-compose config

To enable and disable sound:

make sound_enable
make sound_disable

To remove the container use the command:

make down 
##OR
docker-compose down

Closing Thoughts

Now we have successfully deployed the Asterisk Container using Docker. You are free to make the desired configurations to it to fit your needs. I hope this was informative.

Related posts:

RELATED ARTICLES

Most Popular

Recent Comments