RabbitMQ in Docker - user creation not persisted

前端 未结 4 1112
别那么骄傲
别那么骄傲 2021-02-07 21:44

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

相关标签:
4条回答
  • 2021-02-07 22:22

    For what it's worth, something similar is done in this dockerfile, but I can't get it to persist either:

    RUN /usr/sbin/rabbitmq-server -detached && \
        sleep 5 && \
        rabbitmqctl add_user bunnyuser my_pass1 && \
        rabbitmqctl add_user bunny-admin my_pass2 && \
        rabbitmqctl set_user_tags bunny-admin administrator && \
        rabbitmqctl set_permissions -p / bunnyuser ".*" ".*" ".*"
    
    0 讨论(0)
  • 2021-02-07 22:32

    Because many people are still having this problem (including me), what I did was:

    At building, copy the RabbitMQ database_dir at /var/lib/rabbitmq/mnesia/rabbit\@$(hostname) to /root (everything in /root stays persisted) after configuring all users.

    At runtime, copy the database dir back from /root to /var/lib/rabbitmq/mnesia.

    Only disadvantages: changes made to the database in RabbitMQ will be reset at runtime. I found no other way to do this with docker-compose however.

    Configure.sh (as RUN command in Dockerfile):

    echo "NODENAME=rabbit@message-bus" > /etc/rabbitmq/rabbitmq-env.conf
    echo "127.0.0.1 message-bus" >> /etc/hosts #prevents error that 'message-bus' node doesnt exist (this doesnt persist in /etc/hosts)
    rabbitmqctl add user ... #etc
    rabbitmqctl stop
    mkdir /root/rabbitmq_database
    cp -R /var/lib/rabbitmq/mnesia/rabbit\@message-bus/* /root/rabbitmq_database
    

    Runtime.sh (as entrypoint in Dockerfile):

    #copy database back from /root
    mkdir -p /var/lib/rabbitmq/mnesia/rabbit\@message-bus
    cp -R /root/rabbitmq_database/* /var/lib/rabbitmq/mnesia/rabbit\@message-bus
    rabbitmq-server
    
    0 讨论(0)
  • 2021-02-07 22:33

    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
    
    0 讨论(0)
  • 2021-02-07 22:42

    I do something similar and it persists:

    RUN service rabbitmq-server start ; \
        rabbitmqctl add_vhost /sensu ; \
        rabbitmqctl add_user sensu sensu ; \
        rabbitmqctl set_permissions -p /sensu sensu ".*" ".*" ".*" ; \
        service rabbitmq-server stop
    

    Are you sure the creation process occurs in the first place? The sleeps and subshells don't make it obvious.

    0 讨论(0)
提交回复
热议问题