5

I am build an image using Dockerfile, and I would like to add users to RabbitMQ right after installation. The problem is that during build hostname of the docker container is different from when I run the resultant image. RabbitMQ loses that user; because of changed hostname it uses another DB.

I connot change /etc/hosts and /etc/hostname files from inside a container, and looks that RabbitMQ is not picking my changes to RABBITMQ_NODENAME and HOSTNAME variables.

The only thing that I found working is running this before starting RabbitMQ broker:

echo "NODENAME=rabbit@localhost" >> /etc/rabbitmq/rabbitmq.conf.d/ewos.conf

But then I will have to run docker image with changed hostname all the time.

docker run -h="localhost" image

Any ideas on what can be done? Maybe the solution is to add users to RabbitMQ not on build but on image run?

yun_man_ger
  • 243
  • 1
  • 3
  • 10

4 Answers4

6

Just here is example how to configure from Dockerfile properly:

ENV HOSTNAME localhost

RUN /etc/init.d/rabbitmq-server start ; rabbitmqctl add_vhost /test; /etc/init.d/rabbitmq-server stop

This is remember your config.

Rubycut
  • 1,646
  • 2
  • 16
  • 24
1

Yes, I would suggest to add users when the container runs for the first time.

Instead of starting RabbitMQ directly, you can run a wrapper script that will take care of all the setup, and then start RabbitMQ. If the last step of the wrapper script is a process start, remember that you can use exec so that the new process replaces the script itself.

jpetazzo
  • 14,874
  • 3
  • 43
  • 45
  • Also there was a problem: when I add a user and right after that stop RabbitMQ, added user is not persisted. Adding a bit of ``sleep 3`` before RabbitMQ stop command resolved the issue. – yun_man_ger Sep 25 '13 at 12:02
  • This is not solution, because images should be built from Dockerfile, not that you have to run container to set things up every time you do deployment from docker image. – Rubycut Jan 17 '14 at 10:23
  • @Rubycut you are right. Have you tested, your answer below? I haven't seen command execution separated by semicolons till this time. Did you encounter the problem with persisting configs like I did above? – yun_man_ger Jan 18 '14 at 02:29
  • Yes, I've use it last week on latest version of Rabbitmq and latest docker version. Semicolons work fine, you can even use \ to go to next line if line is too long. You can also use && instead of semicolons. Yes, I had persisting problem until I changed HOSTNAME, now everything is persisted properly. – Rubycut Jan 19 '14 at 13:57
1

This is how I did it.

Dockerfile

FROM debian:jessie

MAINTAINER Francesco Casula <fra.casula@gmail.com>

VOLUME ["/var/www"]
WORKDIR /var/www

ENV HOSTNAME my-docker
ENV RABBITMQ_NODENAME rabbit@my-docker

COPY scripts /root/scripts

RUN /bin/bash /root/scripts/os-setup.bash && \
    /bin/bash /root/scripts/install-rabbitmq.bash

CMD /etc/init.d/rabbitmq-server start && \
    /bin/bash

os-setup.bash

#!/bin/bash

echo "127.0.0.1 localhost" > /etc/hosts
echo "127.0.1.1 my-docker" >> /etc/hosts

echo "my-docker" > /etc/hostname

install-rabbitmq.bash

#!/bin/bash

echo "NODENAME=rabbit@my-docker" > /etc/rabbitmq/rabbitmq-env.conf

echo 'deb http://www.rabbitmq.com/debian/ testing main' | tee /etc/apt/sources.list.d/rabbitmq.list
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | apt-key add -
apt-get update

cd ~
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server_3.6.5-1_all.deb
dpkg -i rabbitmq-server_3.6.5-1_all.deb
apt-get install -f -y

/etc/init.d/rabbitmq-server start

sleep 3

rabbitmq-plugins enable amqp_client mochiweb rabbitmq_management rabbitmq_management_agent \
                        rabbitmq_management_visualiser rabbitmq_web_dispatch webmachine

rabbitmqctl delete_user guest
rabbitmqctl add_user bunny password
rabbitmqctl set_user_tags bunny administrator
rabbitmqctl delete_vhost /
rabbitmqctl add_vhost symfony_prod
rabbitmqctl set_permissions -p symfony_prod bunny ".*" ".*" ".*"
rabbitmqctl add_vhost symfony_dev
rabbitmqctl set_permissions -p symfony_dev bunny ".*" ".*" ".*"
rabbitmqctl add_vhost symfony_test
rabbitmqctl set_permissions -p symfony_test bunny ".*" ".*" ".*"

/etc/init.d/rabbitmq-server restart

IS_RABBIT_INSTALLED=`rabbitmqctl status | grep RabbitMQ | grep "3\.6\.5" | wc -l`

if [ "$IS_RABBIT_INSTALLED" = "0" ]; then
    exit 1
fi

IS_RABBIT_CONFIGURED=`rabbitmqctl list_users | grep bunny | grep "administrator" | wc -l`

if [ "$IS_RABBIT_CONFIGURED" = "0" ]; then
    exit 1
fi

Don't forget to run the container by specifying the right host with the -h flag:

docker run -h my-docker -it --name=my-docker -v $(pwd)/htdocs:/var/www my-docker
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
0

The only thing that helped me was to change default value in rabbitmq-env.conf of MNESIA_BASE property to MNESIA_BASE=/data and I added this command RUN mkdir /data in Dockerfile before starting server and add users.

Armando
  • 137
  • 10