Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ in Docker - user creation not persisted

I've got a problem where the user user1 is not persisted in the container that I have created using the following Dockerfile. What is the reason for this? Is this a RabbitMQ specific issue? e.g. I have to explicitly specify that a user must be persisted

FROM dockerfile/rabbitmq

# Define mount points.
VOLUME ["/data/log", "/data/mnesia"]

# Define working directory.
WORKDIR /data

RUN (rabbitmq-start &) && \
  sleep 10 && \
  rabbitmqctl add_user user1 password1 && \
  rabbitmqctl set_user_tags user1 administrator && \
  rabbitmqctl set_permissions -p / user1 ".*" ".*" ".*"  && \
  sleep 10 && \
  rabbitmqctl stop && \
  sleep 10
# Define default command.
CMD ["rabbitmq-start"]

# Expose ports.
EXPOSE 5672
EXPOSE 15672
like image 384
lolski Avatar asked Nov 06 '14 04:11

lolski


People also ask

How do I change my RabbitMQ docker username and password?

If you wish to change the default username and password of guest / guest , you can do so with the RABBITMQ_DEFAULT_USER and RABBITMQ_DEFAULT_PASS environmental variables. These variables were available previously in the docker-specific entrypoint shell script but are now available in RabbitMQ directly.

How do I use RabbitMQ with docker?

Open a terminal, navigate to your rabbitmq-go folder and run docker-compose up . This command will pull the rabbitmq:3-management-alpine image, create the container rabbitmq and start the service and webUI. You should see something like this: Once you see this, open your browser and head over to http://localhost:15672.


1 Answers

I know it's an old question, but struggled for hours with this problem today and finally solved it for me: The issue seems to be due to the default hostname changing at every new container with Docker, and RabbitMQ actually binds the configuration to the host name.

I set the NODENAME variable in /etc/rabbitmq/rabbitmq-env.conf before setting up the user:

# make the node name static
RUN echo 'NODENAME=rabbit@localhost' > /etc/rabbitmq/rabbitmq-env.conf

and now it works.

Hope it can help.

EDIT:

Here is a working Dockerfile (copying a rabbitmq-env.conf file to the container):

FROM ubuntu:latest

RUN groupadd -r rabbitmq && useradd -r -d /var/lib/rabbitmq -m -g rabbitmq rabbitmq

# add rabbitmq repo
RUN apt-get update && \
apt-get install wget --assume-yes && \
wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc && \
sudo apt-key add rabbitmq-signing-key-public.asc && \
sed -i -e '1ideb http://www.rabbitmq.com/debian/ testing main\' /etc/apt/sources.list && \
apt-get update && \
apt-get install rabbitmq-server --assume-yes

# Enable plugins
RUN rabbitmq-plugins enable rabbitmq_management && \
rabbitmq-plugins enable rabbitmq_web_stomp && \
rabbitmq-plugins enable rabbitmq_mqtt

# expose ports
# Management
EXPOSE  15672
# Web-STOMP plugin
EXPOSE  15674
# MQTT:
EXPOSE  1883


# configure RabbitMQ
COPY ["rabbitmq-env.conf", "/etc/rabbitmq/rabbitmq-env.conf"]
RUN chmod 755 /etc/rabbitmq/rabbitmq-env.conf

# Create users for the apps
COPY ["rabbitmq-setup.sh", "/tmp/rabbitmq/rabbitmq-setup.sh"]
RUN mkdir /var/run/rabbitmq && \
chmod -R 755 /var/run/rabbitmq && \
chown -R rabbitmq:rabbitmq /var/run/rabbitmq && \
service rabbitmq-server start && \
sh /tmp/rabbitmq/rabbitmq-setup.sh && \
rm /tmp/rabbitmq/rabbitmq-setup.sh && \
service rabbitmq-server stop

# start rabbitmq
USER rabbitmq
CMD ["rabbitmq-server", "start"]

My rabbitmq-env.conf file:

NODENAME=rabbimq@localhost

My rabbitmq-setup.sh:

rabbitmqctl add_vhost myvhost && rabbitmqctl add_user myuser mypasswd && rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*" && rabbitmqctl set_user_tags myuser administrator
like image 138
Ben Avatar answered Oct 14 '22 01:10

Ben