ZNC in Docker container

ZNC is a popular IRC bouncer, which stays connected to IRC channels and log conversations while one isn't connected. This note desrcibes how to launch ZNC in a Docker container, so that it be launched on any Docker enabled platform. To quick start with Docker, one can follow the steps proposed to install Docker on an Ubuntu Linux (for instance in a VM).

Using ZNC setup wizard

Once Docker is installed on the system, we can prepare an image that will be used to run ZNC with the following Dockerfile (Docker configuration file):

FROM ubuntu:precise
MAINTAINER Florent Flament

# Using fr ubuntu mirrors and adding universe repository, to install znc
RUN echo "deb http://fr.archive.ubuntu.com/ubuntu precise main restricted" \
> /etc/apt/sources.list
RUN echo "deb http://fr.archive.ubuntu.com/ubuntu precise universe" \
>> /etc/apt/sources.list

# Installing znc package
RUN apt-get -y update
RUN apt-get -y install znc

# Creating directory to store znc configuration
RUN mkdir -m 775 /var/znc
RUN chgrp daemon /var/znc
USER daemon

Let's assume that the Dockerfile is stored in the znc-noconf directory. We can build the ZNC ready image with the following command:

$ docker build -t znc:noconf znc-noconf/

The next step is to configure ZNC. The following command will run the configuration wizard in a new container based on the previously generated image:

$ docker run -i -t znc:noconf -d /var/znc -c

After having answered to all the questions, ZNC will generate its configuration files. It will then be ready to run in daemon mode. Now we can save a Docker image including ZNC's configuration files.

$ CONT=$(docker ps -a | grep "minutes ago" | head -1 | awk '{print $1}')
$ docker commit \
> -run='{"Cmd": ["/usr/bin/znc", "-f", "-d", "/var/znc/"], "User": "daemon"}' \
> $CONT znc:ready

The ZNC image is now ready, and can be launched. In addition to launch ZNC in a new container, the following command will map the host's 6697 TCP port on the container's 6697 port (assuming that ZNC has been configured to listen to port 6697).

$ docker run -d -p 6697:6697 znc:ready

Using a previously made ZNC configuration file

One can setup a ZNC Docker container even quicker if he already has a znc.conf configuration file. One has to create a directory (for instance myznc/) containing both: znc.conf and a Dockerfile, with the following content:

FROM ubuntu:precise
MAINTAINER Florent Flament

# Using fr ubuntu mirrors and adding universe repository, to install znc
RUN echo "deb http://fr.archive.ubuntu.com/ubuntu precise main restricted" \
> /etc/apt/sources.list
RUN echo "deb http://fr.archive.ubuntu.com/ubuntu precise universe" \
>> /etc/apt/sources.list

# Installing znc package
RUN apt-get -y update
RUN apt-get -y install znc

# Creating directory to store znc configuration
RUN mkdir -m 775 /var/znc
# Generates key for SSL exchanges
RUN /usr/bin/znc -d /var/znc -p
# Copies ZNC configuration file
ADD znc.conf /var/znc/configs/
RUN chown -R daemon:daemon /var/znc
USER daemon

# Setting default container's command
CMD ["/usr/bin/znc", "-f", "-d", "/var/znc/"]

Then the ZNC container can be launched right after having built the image:

$ docker build -t znc:myznc myznc/
$ docker run -d -p 6697:6697 znc:myznc

Update 2014/02/25

Here's an example of ZNC configuration file listening for SSL incoming connexions:

Listener4  = +6697

<User USER1>
        Pass       = PASSWORDHASHHERE
        Admin      = true
        Nick       = NICK1
        AltNick    = NICK2
        Ident      = NICK1
        RealName   = REALNAME
        Buffer     = 500
        KeepBuffer = true
        ChanModes  = +stn
        LoadModule = admin
        Server     = irc.freenode.net 6667 
        BindHost   = 0.0.0.0
</User>

<User USER2>
        Pass       = PASSWORDHASHHERE
        Admin      = true
        Nick       = NICK1
        AltNick    = NICK2
        Ident      = NICK1
        RealName   = REALNAME
        Buffer     = 500
        KeepBuffer = true
        ChanModes  = +stn
        LoadModule = admin
        Server     = CUSTOMIRCSERVER +6697 IRCSERVERPASSWORD
        BindHost   = 0.0.0.0
</User>

PASSWORDHASHHERE can be generated by using znc -s. IRC clients (like xchat) have to be set up by specifying in the "Server Password" field a user's credentials <user>:<password> (i.e USER1:PASSORDHASHHERE).