Cannot start rabbitmq-server in docker container, how to write this Dokcerfile?
celery, docker, dockerfile, python, rabbitmq
Solution
So as we discussed in comments, I'd suggest you to use official docker image for rabbitmq. Therefore you will end-up with 2 containers. In is this case: app and rabbit. Here is an example of Dockerfile and docker-compose.yml:
Dockerfile:
# use base python image with python 2.7
FROM python:2.7
# add requirements.txt to the image
ADD requirements.txt /app/requirements.txt
# set working directory to /app/
WORKDIR /app/
# install python dependencies
RUN apt-get update
RUN apt-get -y install libpq-dev python-dev
RUN pip install -r requirements.txt
And example of docker-compose.yml:
version: '2'
services:
# RabbitMQ
rabbit:
hostname: rabbit
image: rabbitmq:3.6.1-management
ports:
- "5672:5672" # we forward this port because it's useful for debugging
- "15672:15672" # here, we can access rabbitmq management plugin
# App
app:
build:
context: .
dockerfile: Dockerfile
hostname: app
volumes:
- .:/app # mount current directory inside container
ports:
- "8000:8000"
# set up links so that web knows about db and redis
links:
- rabbit
To start new containers use `docker-compose build && docker-compose up -d`
Problem
Cannot start rabbitmq-server in my docker container Here is my Dockerfile ``` # python official docker image, I have check that it is base on the debain jessie # The first line of python:2.7 Dockerfile is "FROM buildpack-deps:jessie" FROM python:2.7 # The rabbitmq-server install commands are as follow # I am sure that if I execute those commands in a Debain Jessie Server # The rabbitmq-server will be installed successfully and connectable # But in the docker, it does not works RUN echo 'deb http://www.rabbitmq.com/debian/ testing main' > /etc/apt/sources.list.d/rabbitmq.list RUN wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | apt-key add - RUN apt-get update && apt-get install -y --no-install-recommends rabbitmq-server && rm -rf /var/lib/apt/lists/* RUN service rabbitmq-server restart RUN service rabbitmq-server status # A lot of other things about deploy my project # A lot of other things about deploy my project # A lot of other things about deploy my project CMD ["xxx", "xxxx"] ``` Here are some outputs in build Dockerfile ``` Step 4/6 : RUN apt-get ...... ... ... Adding system user `rabbitmq' (UID 105) ... Adding new user `rabbitmq' (UID 105) with group `rabbitmq' ... Not creating home directory `/var/lib/rabbitmq'. invoke-rc.d: policy-rc.d denied execution of start. Processing triggers for systemd (215-17+deb8u6) ... Processing triggers for libc-bin (2.19-18+deb8u7) ... ---> 35eb870defca Removing intermediate container c2aa80f72a12 ``` ``` Step 5/6 : RUN service rabbitmq-server restart ---> Running in cff316499bf0 Restarting message broker: rabbitmq-server. ``` ``` Step 6/6 : RUN service rabbitmq-server status ---> Running in 2c03f8721cf8 Status of node rabbit@607924cbbc93 ... Error: unable to connect to node rabbit@607924cbbc93: nodedown DIAGNOSTICS =========== attempted to contact: [rabbit@607924cbbc93] rabbit@607924cbbc93: * connected to epmd (port 4369) on 607924cbbc93 * epmd reports: node 'rabbit' not running at all no other nodes on 607924cbbc93 * suggestion: start the node current node details: - node name: 'rabbitmq-cli-30@607924cbbc93' - home dir: /var/lib/rabbitmq - cookie hash: nK/d5I704NfDDEZdnP6xPg== The command '/bin/sh -c service rabbitmq-server status' returned a non-zero code: 3 ``` Where is the problem and how can fix it ? I am very confused because the install commands are correct, they work well at debain vps. However, they do not work well in the docker container. My purpose is let my python-celery connects rabbitmq-server in this container, just in this container. It does not need connectable from out of this container. Here are some guesses about this problem - Network configuration within container - Please note the Step 4/6 in docker build