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

2024-06-13 05:09:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有python应用程序在EC2中的Docker容器中运行。当我在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

客户端.py

^{pr2}$

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

部署.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设置了入站规则,但仍然不起作用。我不知道这里有什么问题。在


Tags: instancedockerinipenvformatcontainerhtml