ConnectionRefusedError:[Errno 111] python中的连接被拒绝

时间:2019-05-10 06:53:06

标签: python docker amazon-ec2 python-sockets

我在EC2的Docker容器中运行了python应用程序。当我在EC2中部署该容器时,该容器崩溃了并且正在重新启动。当我查看cloudwatch日志时,出现此错误,表明连接被拒绝。

Traceback (most recent call last):
File "./process-render-queue.py", line 96, in <module>
queues = beanstalk.Client(host=beanstalk.host)
File "/var/www/social-promos/scripts/echomany/beanstalk/client.py", 
line 20, in __init__
self._sock = socket.create_connection((host, port))
File "/usr/lib/python3.4/socket.py", line 509, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

Client.py

class Client:

def __init__(self,
             host: str = '127.0.0.1',
             port: int = 11300,
             encoding: str = 'utf-8',
             use = DEFAULT_TUBE,
             watch = DEFAULT_TUBE) -> None:
    self._sock = socket.create_connection((host, port))
    self._reader = self._sock.makefile('rb')
    self.encoding = encoding
    if self.encoding is None:
        raise TypeError("Unable to encode string with no encoding set")

    if use != DEFAULT_TUBE:
        self.use(use)

    if isinstance(watch, str):
        if watch != DEFAULT_TUBE:
            self.watch(watch)
            self.ignore(DEFAULT_TUBE)
    else:
        for tube in watch:
            self.watch(tube)
        if DEFAULT_TUBE not in watch:
            self.ignore(DEFAULT_TUBE)

def close(self) -> None:
    self._reader.close()
    self._sock.close()

Dockerfile

# Version: 0.1
FROM debian:jessie
MAINTAINER EchoMany Ltd "technical@redberrydigital.com"
SHELL ["/bin/bash", "-c"]
COPY python_command/install_*.sh /root/
RUN /root/install_dependencies.sh
RUN /root/install_natron_2.2.7.sh
RUN /root/install_ffmpeg.sh
COPY python_command/etc/ImageMagick-6 etc/ImageMagick-6
COPY external_sources/social-promos /var/www/social-promos
RUN /root/install_and_run_composer.sh
COPY python_command/start_command.sh /usr/bin/
RUN chmod -R 777 /var/www/social-promos
USER nobody
ENTRYPOINT ["/usr/bin/start_command.sh"]

我使用此部署脚本构建映像并部署到Ec2

deploy.py

def deploy_to(instance, containers, ssh_key, html_env, script_env):
    if os.environ["USER"] == "ec2-user":
        print("Instance: {} - IP: {}".format(instance.id, instance.private_ip_address))
        ip = instance.private_ip_address
    else:
        print("Instance: {} - IP: {}".format(instance.id, instance.public_ip_address))
        ip = instance.public_ip_address
    sshDeployCmd = ["ssh",
                    "-i", ssh_key,
                    "-q", "-o", "StrictHostKeyChecking no",
                    "ec2-user@{}".format(ip)]

    outLog = open("deploy_to_{}.out".format(instance.id), "wt")
    errLog = open("deploy_to_{}.err".format(instance.id), "wt")

    # copying new configuration files (if necessary)
    if html_env is not None:
        print(html_env,"html")
        scpCmd = ["scp",
                  "-i", ssh_key,
                  "-o", "StrictHostKeyChecking no",
                  html_env,
                  "ec2-user@{}:/home/ec2-user/html.env".format(ip)]
        print(scpCmd)
        subprocess.call(scpCmd, stdout=outLog, stderr=errLog)
    if script_env is not None:
        scpCmd = ["scp",
                  "-i", ssh_key,
                  "-o", "StrictHostKeyChecking no",
                  script_env,
                  "ec2-user@{}:/home/ec2-user/script.env".format(ip)]
        subprocess.call(scpCmd, stdout=outLog, stderr=errLog)

    # pulling new images
    images = []
    for container in containers:
        if container['image'] not in images:
            images.append(container['image'])
    for image in images:
        pullImage = "sudo docker pull {}".format(image)
        subprocess.call(sshDeployCmd + [pullImage], stdout=outLog, stderr=errLog)

    # start new containers
    for container in containers:
        # stop currently running containers
        subprocess.call(sshDeployCmd
                        + ["sudo docker stop $(sudo docker ps -a --filter name={} -q)".format(container['name_prefix'])],
                        stdout=outLog, stderr=errLog)
        subprocess.call(sshDeployCmd
                        + ["sudo docker rm $(sudo docker ps -a --filter name={} -q)".format(container['name_prefix'])],
                        stdout=outLog, stderr=errLog)
        # run the new one
        count = container['count'] if 'count' in container else 1
        for i in range(count):
            runContainer = "sudo docker run -d --name {name_prefix}_{istance_id}_{i} " \
                           "--restart=always " \
                           "--log-driver=awslogs --log-opt awslogs-region=eu-west-2 --log-opt awslogs-group={logs_group} " \
                           "-v /home/ec2-user/html.env:/var/www/social-promos/html/.env " \
                           "-v /home/ec2-user/script.env:/var/www/social-promos/scripts/.env " \
                           "{extra_params} {image} {command}"\
                           .format(name_prefix=container['name_prefix'], istance_id=instance.id,
                                   logs_group=container['logs_group'], extra_params=container['extra_params'],
                                   image=container['image'], command=container['command'], i=i)
            subprocess.call(sshDeployCmd + [runContainer], stdout=outLog, stderr=errLog)

    # clean up some space
    subprocess.call(sshDeployCmd + ["sudo docker images -q --filter 'dangling=true' | xargs sudo docker rmi"],
                    stdout=outLog, stderr=errLog)
    outLog.close()
    errLog.close()

我几乎没有看到类似的StackOverflow问题,但是那些答案并不能解决我的问题。我什至在安全组中为端口11300设置了入站规则,但仍然行不通。我不知道这里出了什么问题。

0 个答案:

没有答案