使用Ecs Ec2将Python应用程序与Redis连接 [英] Connect Python app with redis using Ecs Ec2

查看:74
本文介绍了使用Ecs Ec2将Python应用程序与Redis连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个dockerfile,我想使用Aws-ECS服务链接它们.我想使用Ecs Ec2链接它们.我应该记住哪些步骤来链接容器以及应该使用哪种网络模式.可以说我要在localhost:5000上公开容器.我已将Docker映像推送到ECR.我想知道应该分别对python和redis进行什么端口映射.

Dockerfile python:

  FROM python:3添加 ./代码WORKDIR/代码RUN pip install -r requirements.txtCMD python3 app.py 

Dockerfile重做:

 来自redis:latest 

解决方案

Redis和python都应属于同一任务定义,然后定义这两个容器之间的链接.

链接

link参数允许容器之间相互通信,而无需端口映射.仅当任务定义的网络模式设置为桥接时才受支持. name:internalName 构造类似于Docker链接中的 name:alias .最多255个字母(大写和小写),数字,连字符和下划线.

现在在应用程序的容器定义中添加一个链接

I have 2 dockerfile and I want to link them using Aws-ECS service. I want to link them using Ecs Ec2. What steps I should keep in mind to link the container and what network mode should I used. Lets say I want to expose the container on localhost:5000. I have pushed my docker images to ECR. I want to know what port mapping should I do to python and redis respectively.

Dockerfile python:

FROM python:3
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python3 app.py

Dockerfile redis:

From redis:latest

解决方案

Both Redis and python should be part of the same task definition and then define linking between these two containers.

links

The link parameter allows containers to communicate with each other without the need for port mappings. Only supported if the network mode of a task definition is set to bridge. The name:internalName construct is analogous to name:alias in Docker links. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed.

Linking in ECS task defintion

Add below in the python container task definition

   "links": [
        "redis"
      ]

where redis is Redis container name.

  "containerDefinitions": [
    {
      "portMappings": [
        {
          "hostPort": 6379,
          "protocol": "tcp",
          "containerPort": 6379
        }
      ],
      "cpu": 0,
      "memoryReservation": 400,
      "image": "123.dkr.ecr.us-west-2.amazonaws.com/redis",
      "name": "redis"
    },
    {
      "portMappings": [
        {
          "hostPort": 80,
          "protocol": "tcp",
          "containerPort": 80
        }
      ],
      "cpu": 0,
      "memoryReservation": 400,
      "image": "123.dkr.ecr.us-west-2.amazonaws.com/pythonapp",
    "links": [
        "redis"
      ],
      "name": "app"
    }

Now you can access Redis container by using redis:6379 inside app container.

From AWS console

Goto task definition and add container definition in signal task definition

  • App contianer
  • Redis container

Now add a link in the container definition of an app

这篇关于使用Ecs Ec2将Python应用程序与Redis连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆