2016-02-24 84 views
2

我在docker中使用芹菜時遇到了問題。芹菜在Docker上不起作用

我配置了兩個docker容器,web_server和celery_worker。 celery_worker包含rabbitmq-server。 web_server從芹菜工作者調用任務。

我在虛擬機中配置了同樣的東西。它的工作原理。但是碼頭工會說出如下的錯誤信息。

Traceback (most recent call last): 
    File "/web_server/test/test_v1_data_description.py", line 58, in test_create_description 
    headers=self.get_basic_header() 

    ......... 
    ......... 

    File "../task_runner/__init__.py", line 31, in run_describe_task 
    kwargs={}) 
    File "/usr/local/lib/python3.4/dist-packages/celery/app/base.py", line 349, in send_task 
    self.backend.on_task_call(P, task_id) 
    File "/usr/local/lib/python3.4/dist-packages/celery/backends/rpc.py", line 32, in on_task_call 
    maybe_declare(self.binding(producer.channel), retry=True) 
    File "/usr/local/lib/python3.4/dist-packages/kombu/messaging.py", line 194, in _get_channel 
    channel = self._channel = channel() 
    File "/usr/local/lib/python3.4/dist-packages/kombu/utils/__init__.py", line 425, in __call__ 
    value = self.__value__ = self.__contract__() 
    File "/usr/local/lib/python3.4/dist-packages/kombu/messaging.py", line 209, in <lambda> 
    channel = ChannelPromise(lambda: connection.default_channel) 
    File "/usr/local/lib/python3.4/dist-packages/kombu/connection.py", line 756, in default_channel 
    self.connection 
    File "/usr/local/lib/python3.4/dist-packages/kombu/connection.py", line 741, in connection 
    self._connection = self._establish_connection() 
    File "/usr/local/lib/python3.4/dist-packages/kombu/connection.py", line 696, in _establish_connection 
    conn = self.transport.establish_connection() 
    File "/usr/local/lib/python3.4/dist-packages/kombu/transport/pyamqp.py", line 116, in establish_connection 
    conn = self.Connection(**opts) 
    File "/usr/local/lib/python3.4/dist-packages/amqp/connection.py", line 165, in __init__ 
    self.transport = self.Transport(host, connect_timeout, ssl) 
    File "/usr/local/lib/python3.4/dist-packages/amqp/connection.py", line 186, in Transport 
    return create_transport(host, connect_timeout, ssl) 
    File "/usr/local/lib/python3.4/dist-packages/amqp/transport.py", line 299, in create_transport 
    return TCPTransport(host, connect_timeout) 
    File "/usr/local/lib/python3.4/dist-packages/amqp/transport.py", line 95, in __init__ 
    raise socket.error(last_err) 
nose.proxy.OSError: [Errno 111] Connection refused 

這些是兩個容器的Dockerfiles。

用於web_server的Dockerfile。

FROM ubuntu:14.04 
MAINTAINER Jinho Yoo 

# Update packages. 
RUN apt-get clean 
RUN apt-get update 

# Create work folder. 
RUN mkdir /web_server 
WORKDIR /web_server 

# Setup web server and celery. 
ADD ./install_web_server_conf.sh ./install_web_server_conf.sh 
RUN chmod +x ./install_web_server_conf.sh 
RUN ./install_web_server_conf.sh 

#Reduce docker size. 
RUN rm -rf /var/lib/apt/lists/* 

# Run web server. 
CMD ["python3","web_server.py"] 

# Expose port. 
EXPOSE 5000 

celery_worker的Dockerfile。

FROM ubuntu:14.04 
MAINTAINER Jinho Yoo 

# Update packages. 
RUN apt-get clean 
RUN apt-get update 
RUN apt-get install -y wget build-essential ca-certificates-java 

# Setup python environment. 
ADD ./bootstrap/install_python_env.sh ./install_python_env.sh 
RUN chmod +x ./install_python_env.sh 
RUN ./install_python_env.sh 

# Install Python libraries including celery. 
RUN pip3 install -r ./core/requirements.txt 

# Add mlcore user for Celery worker 
RUN useradd --uid 1234 -M mlcore 
RUN usermod -L mlcore 

# Celery configuration for supervisor 
ADD celeryd.conf /etc/supervisor/conf.d/celeryd.conf 
RUN mkdir -p /var/log/celery 

# Reduce docker size. 
RUN rm -rf /var/lib/apt/lists/* 

# Run celery server by supervisor. 
CMD ["supervisord", "-c", "/ml_core/supervisord.conf"] 

# Expose port. 
EXPOSE 8080 
EXPOSE 8081 
EXPOSE 4040 
EXPOSE 7070 
EXPOSE 5672 
EXPOSE 5671 
EXPOSE 15672 

回答

0

我找到了原因。 celery_worker中的碼頭集裝箱不運行rabbitmq-server。所以我在下面的celery_worker的Dockerfile中添加了兩行。

# Run rabbitmq server and celery. 
ENTRYPOINT service rabbitmq-server start && supervisord -c /ml_core/supervisord.conf 
0

碼頭集裝箱無法正常通話。我的猜測是你的連接字符串是localhost:<port>

有幾種方法可以讓您的容器能夠進行通信。

1:連接 http://rominirani.com/2015/07/31/docker-tutorial-series-part-8-linking-containers/

基本上,在運行時,碼頭工人增加了一個進入你的hosts文件指向同一個私人碼頭工人的網絡堆棧中的泊塢窗容器的內部IP地址。

2:docker run --net=host: 此結合您的容器到主機網絡堆棧,從而,所有的容器將表現爲從localhost運行,並且可以作爲這樣的訪問。如果您運行綁定到相同外部端口的多個容器,則可能會遇到端口衝突問題,請注意這一點。

3:外部HAProxy: 您可以將DNS條目綁定到HAProxy,並配置代理以使用與DNS條目匹配的主機頭將流量重定向到主機:您的容器正在運行的端口以及來自其他容器將「離開」私人碼頭網絡堆棧,打到DNS服務器,然後回到HAProxy,HAProxy將指向適當的容器。