Docker rabbitmq host name

I am creating an image using Dockerfile, and I would like to add users to RabbitMQ right after installation. The problem is that during the build, the hostname of the docker container is different from when I run the resulting image. RabbitMQ is losing this user; due to the changed host name, it is using a different database.

I can change the /etc/hosts and /etc/hostname files from inside the container and it looks like RabbitMQ does not select my changes in the variables RABBITMQ_NODENAME and HOSTNAME .

The only thing I found was to run this before starting the RabbitMQ broker:

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

But then I have to run the docker image with the changed host name 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 to the assembly, but when the image starts?

+4
source share
3 answers

Yes, I would suggest adding users when the container is launched for the first time.

Instead of running RabbitMQ directly, you can run a shell script that takes care of all the settings, and then run RabbitMQ. If the last step of the shell script is to start the process, remember that you can use exec so that the new process replaces the script itself.

+1
source

Here is an example of how to properly configure from Dockerfile:

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

This is your configuration.

+6
source

Here 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 

Remember to start the container by specifying the correct host with the -h flag:

 docker run -h my-docker -it --name=my-docker -v $(pwd)/htdocs:/var/www my-docker 
0
source

All Articles