简体   繁体   中英

AWS ECS Fargate and port mapping

I have two containers and they expose the same port. I want to run them in the same task as they are part of the same system. But I cannot do this with Fargate because there are no port mapping and the host port should be the same as container port for the awsvpc network mode (only supported by Fargate).

It's an essential feature of Docker and it's strange that it seems to be not supported by Fargate. Is there really no way to do this or I'm missing something?

Use application load balancer to your service and set your custom port in target group and host port should be set the same as container port. This is our tested solution.

The simplest way to solve this problem is to make the port of your Docker container configurable and then pass it to the container as an environment variable. Example:

Dockerfile

FROM python:3.10-slim-bullseye
ENV PORT 5000

# Do all your container setup
# ...

EXPOSE $PORT
ENTRYPOINT path/to/entrypoint.sh

In entrypoint.sh , you need to set the app itself to use the port provided in the PORT environment var.

Then in your task definition, set the port mapping to a different port per container and provide the port as an env var:

{
// ...
      "portMappings": [
        {
          "hostPort": 5001,
          "protocol": "tcp",
          "containerPort": 5001
        }
      ],
// ...
      "environment": [
        {
          "name": "PORT",
          "value": "5001"
        },
      ]
}

If you don't override the port via an environment variable, it will use the default port declared in the Dockerfile (in this case, 5000).

You will have to switch to ec2 based ecs instead of fargate. Also you can run on different port and use service discovery feature in fargate to communicate each other. Might need code changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM