问题
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
回答1:
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
来源:https://stackoverflow.com/questions/63077970/connect-python-app-with-redis-using-ecs-ec2